Up to 4 times faster implementation of memchr function proposed for Linux kernel

A set of patches with an optimized implementation of the memchr() function used to search for a character in an array is proposed for inclusion in the Linux kernel. Unlike the old version, which used a byte-by-byte comparison, the proposed implementation is built taking into account the full use of 64- and 32-bit CPU registers. Instead of bytes, the comparison is performed using machine words, which allows 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 is not as significant, but still higher than the original version. In the Linux kernel, the size of the strings processed in memchr() reaches 512 bytes. The performance gain for 512 byte strings, in a situation where the search character is at the end of the string, is 20%.

Testing the 5.18 kernel with the new "memchr()" variant for 32-bit and 64-bit architectures did not reveal any issues. The overall performance gain of kernel subsystems when using the optimized "memchr()" variant has not yet been evaluated, nor has the expediency of replacing the implementation been analyzed (the memchr() function call occurs 129 times in the kernel code, including in the code of drivers and file systems).

Source: opennet.ru

Add a comment