A Linux kernel patchset has been proposed with an optimized implementation of the memchr() function, used for searching a character in an array. Unlike the old version, which used byte-by-byte comparison, the proposed implementation fully utilizes 64- and 32-bit CPU registers. Instead of comparing bytes, it compares using machine words, allowing at least 4 bytes to be compared at once.
When searching in large strings, the new version has been found to be approximately 4 times faster than the old one (for instance, with strings of 1000 characters). For smaller strings, the efficiency of the new implementation is not as significant but still higher compared to the original version. In the Linux kernel, the size of the strings processed in memchr() can reach 512 bytes. The performance gain for 512-byte strings, especially when the sought character is located at the end of the string, is 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 gain of the core subsystems with the optimized version of memchr() has not yet been evaluated, nor has the feasibility of replacing the implementation been analyzed (the memchr() function is called 129 times in the kernel code, including in driver and filesystem code).
Forrás: opennet.ru
