Speedup first memmem match
authorRajalakshmi Srinivasaraghavan <raji@linux.vnet.ibm.com>
Tue, 28 Aug 2018 07:12:19 +0000 (12:42 +0530)
committerRajalakshmi Srinivasaraghavan <raji@linux.vnet.ibm.com>
Tue, 28 Aug 2018 07:12:19 +0000 (12:42 +0530)
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.

ChangeLog
string/memmem.c

index 8419d4671c431141b4fb9c21219809ca8089b0f1..2a250970e431ea7b0a76597f3e94855b7d402a32 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2018-08-28  Rajalakshmi Srinivasaraghavan  <raji@linux.vnet.ibm.com>
+
+       * string/memmem.c: Use memcmp for first match.
+
 2018-08-28  Rafal Luzynski  <digitalfreak@lingonborough.com>
 
        [BZ #17426]
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