A set of patches has been proposed for inclusion in the Linux kernel, featuring an optimized implementation of the memchr() function used for searching a character in an array. Unlike the previous version, which utilized byte-by-byte comparison, the proposed implementation is designed to fully utilize 64-bit and 32-bit CPU registers. Instead of bytes, comparison is performed using machine words, allowing for the comparison of at least 4 bytes at once.
When searching through large strings, the new version 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 is not as significant, yet remains higher compared to the original version. In the Linux kernel, the size of strings processed by memchr() can reach up to 512 bytes. The performance increase for 512-byte strings, in cases where the searched character is located at the end of the string, is 20%.
Testing of kernel 5.18 with the new version of memchr() for both 32-bit and 64-bit architectures has not revealed any issues. The overall performance improvement of the kernel subsystems using the optimized memchr() variant has not yet been assessed, nor has the feasibility of replacing the implementation been analyzed (in the kernel code, the memchr() function is called 129 times, including in driver code and file systems).
Source: opennet.ru
