Skip to content

Optimize memchr()

Yu-Jen Chang lkml llvm 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest2 patches 17 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2lib/string.c: Add a macro for memchr_inv()lib/string.c+24 -100 replies
2/2lib/string.c: Optimize memchr()lib/string.c+21 -715 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/2] Optimize memchr()

*** BLURB HERE ***
This patche series optimized "memchr()" and add a macro for "memchr_inv()" so that both funtions can use it to generate bit mask.

The original implementaion of "memchr()" is based on byte-wise comparison, which do not fully use 64-bit or 32-bit register in CPU. We implement a word-wise comparison so that at least 4 bytes can be compared at the same time. The optimized "memchr()" is nearly 4x faster than the original one for long strings. In Linux Kernel, we find that the length of the string searched by "memchr()" is up to 512 bytes in drivers/misc/lkdtm/heap.c. In our test, the optimized version is about 20% faster if the target character is at the end of the string when going through a 512-byte string.

We recompile the 5.18 kernel with optimized "memchr()" in 32-bit and 64-bit. They run correctly.

Yu-Jen Chang (2):
lib/string.c: Add a macro for memchr_inv()
lib/string.c: Optimize memchr()

 lib/string.c | 62 ++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 45 insertions(+), 17 deletions(-)
Signature · 1 line
2.25.1