[PATCH] Improve strstr performance of short needles

Gabriel F. T. Gomes gabriel@inconstante.eti.br
Fri Sep 28 21:30:00 GMT 2018


On Wed, 05 Sep 2018, Wilco Dijkstra wrote:

>Improve strstr performance for the common case of short needles.  For 2-4 
>characters a small loop is typically fastest.  On large strings the speedup
>with a needle size of 4 is about 65%.

I ran some tests on a powerpc64le machine and I noticed that, as the
haystack becomes larger, the search for a needle that is *not* in the
haystack, became slower with this change.

For instance, when the haystack is 32 chars long, searching for a 4 chars
long needle that is not in the haystack [1] took ~11% to ~22% longer to
finish, when compared to the pristine code.  When the needle *is* in the
haystack, that did not happen and the performance was usually 30% better.

In my tests, this is also true for needles 2 and 3 chars long.


Have you observed similar results in your tests?


[1] Tests with the summary similar to this:
      Length   32/4, alignment 0/ 0, fail:



More information about the Libc-alpha mailing list