From patchwork Fri Aug 24 11:28:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rajalakshmi S X-Patchwork-Id: 29037 X-Patchwork-Delegate: fweimer@redhat.com Received: (qmail 89159 invoked by alias); 24 Aug 2018 11:28:58 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 89142 invoked by uid 89); 24 Aug 2018 11:28:57 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 spammy= X-HELO: mx0a-001b2d01.pphosted.com From: Rajalakshmi Srinivasaraghavan To: libc-alpha@sourceware.org Cc: Rajalakshmi Srinivasaraghavan Subject: [PATCH] Speedup first memmem match Date: Fri, 24 Aug 2018 16:58:46 +0530 x-cbid: 18082411-0028-0000-0000-000002EF2B26 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18082411-0029-0000-0000-000023A874AB Message-Id: <1535110126-18000-1-git-send-email-raji@linux.vnet.ibm.com> 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 * string/memmem.c: Use memcmp for first match. --- string/memmem.c | 4 ++++ 1 file changed, 4 insertions(+) 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