For inclusion in the Linux kernel, a set of patches has been proposed featuring an optimized implementation of the memchr() function, used for character searching in arrays. Unlike the older version, which employed byte-by-byte comparison, the proposed implementation is designed to fully utilize 64- and 32-bit CPU registers. Instead of bytes, comparison is made using machine words, allowing for the comparison of at least 4 bytes at once.
When searching in large strings, the new variant has proven to be about 4 times faster than the old one (for example, with strings of 1000 characters). For smaller strings, the efficiency of the new implementation isn't as significant but is still higher compared to the original. In the Linux kernel, the size of strings processed in memchr() can reach 512 bytes. The performance gain for 512-byte strings, in cases where the sought character is located at the end of the string, is about 20%.
Testing of kernel 5.18 with the new version of 'memchr()' for 32- and 64-bit architectures did not reveal any issues. The overall performance increase of kernel subsystems when using the optimized version of 'memchr()' has not yet been assessed, nor has the feasibility of replacing the implementation (the memchr() function is called 129 times in the kernel code, including in the code of drivers and file systems).
Kilde: opennet.ru
