A set of patches has been proposed for inclusion in the Linux kernel with an optimized implementation of the memchr() function, used for finding a character in an array. Unlike the old version, which used byte-by-byte comparison, the proposed implementation is built with the full utilization of 64- and 32-bit CPU registers. Instead of bytes, comparison is performed using machine words, allowing for at least 4 bytes to be compared at a time.
When searching in large strings, the new version turned out to be about 4 times faster than the old one (for example, for strings of 1000 characters). For small strings, the efficiency of the new implementation isn't as significant, but it is still higher compared to the original version. In the Linux kernel, the size of strings processed in memchr() reaches 512 bytes. The performance gain for 512-byte strings, in situations where the searched character is located at the end of the string, is 20%.
Testing the 5.18 kernel with the new memchr() variant for 32- and 64-bit architectures did not reveal any issues. The overall performance increase of the kernel subsystems when using the optimized memchr() variant 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 code and file systems).
Źródło: opennet.ru
