Speedup first memmem match

Message ID 1535110126-18000-1-git-send-email-raji@linux.vnet.ibm.com
State Committed
Delegated to: Florian Weimer
Headers

Commit Message

Rajalakshmi S Aug. 24, 2018, 11:28 a.m. UTC
  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
more than 40% for first match.

Tested on powerpc64le and x86_64.

2018-08-24  Rajalakshmi Srinivasaraghavan  <raji@linux.vnet.ibm.com>

	* string/memmem.c: Use memcmp for first match.
---
 string/memmem.c | 4 ++++
 1 file changed, 4 insertions(+)
  

Comments

Florian Weimer Aug. 26, 2018, 12:22 p.m. UTC | #1
* Rajalakshmi Srinivasaraghavan:

> 2018-08-24  Rajalakshmi Srinivasaraghavan  <raji@linux.vnet.ibm.com>
>
> 	* string/memmem.c: Use memcmp for first match.

Change looks goot to me.  Thanks.
  

Patch

diff --git a/string/memmem.c b/string/memmem.c
index 43efaa3fb7..d72b8249e6 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