]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Speedup first memmem match
authorRajalakshmi Srinivasaraghavan <raji@linux.vnet.ibm.com>
Tue, 28 Aug 2018 07:12:19 +0000 (12:42 +0530)
committerWilco Dijkstra <wdijkstr@arm.com>
Fri, 13 Sep 2019 14:17:56 +0000 (15:17 +0100)
As done in commit 284f42bc778e487dfd5dff5c01959f93b9e0c4f5, memcmp
can be used after memchr to avoid the initialization overhead of the
two-way algorithm for the first match.  This has shown improvement
>40% for first match.

(cherry picked from commit c8dd67e7c958de04c3783cbea7c384431707b5f8)

ChangeLog
string/memmem.c

index 14553100a089fdf1c71a2eef9f2089f91f7572bf..182e0bacd5f44face348caeb71a542e637b728ec 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2019-09-13  Rajalakshmi Srinivasaraghavan  <raji@linux.vnet.ibm.com>
+
+       * string/memmem.c: Use memcmp for first match.
+
 2019-09-13  Wilco Dijkstra  <wdijkstr@arm.com>
 
        * string/strcasestr.c (STRCASESTR): Simplify and speedup first match.
index 43efaa3fb718e7a22c3b4122c278720768644d0f..d72b8249e62a744c2d031c9ccb0157f141df641f 100644 (file)
@@ -70,6 +70,10 @@ __memmem (const void *haystack_start, size_t haystack_len,
       haystack_len -= haystack - (const unsigned char *) haystack_start;
       if (haystack_len < needle_len)
        return NULL;
+      /* Check whether we have a match.  This improves performance since we
+        avoid the initialization overhead of the two-way algorithm.  */
+      if (memcmp (haystack, needle, needle_len) == 0)
+       return (void *) haystack;
       return two_way_short_needle (haystack, haystack_len, needle, needle_len);
     }
   else