about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRajalakshmi Srinivasaraghavan <raji@linux.vnet.ibm.com>2018-08-28 12:42:19 +0530
committerWilco Dijkstra <wdijkstr@arm.com>2019-09-13 16:40:20 +0100
commitceeba1d73c84f1a551677149ce3b3ed3372fb3ec (patch)
tree9ac8c4d916f9894e5b71f4f0f714200736792a91
parentc60bf879b21aefedaf632f585b9c39af8532bc71 (diff)
downloadglibc-ceeba1d73c84f1a551677149ce3b3ed3372fb3ec.tar.gz
glibc-ceeba1d73c84f1a551677149ce3b3ed3372fb3ec.tar.xz
glibc-ceeba1d73c84f1a551677149ce3b3ed3372fb3ec.zip
Speedup first memmem match
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)
-rw-r--r--ChangeLog4
-rw-r--r--string/memmem.c4
2 files changed, 8 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 22012fb458..12e7d27e49 100644
--- 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.
diff --git a/string/memmem.c b/string/memmem.c
index 34299b8864..645b6d1a26 100644
--- a/string/memmem.c
+++ b/string/memmem.c
@@ -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