aboutsummaryrefslogtreecommitdiff
path: root/compat
diff options
context:
space:
mode:
authorRené Scharfe <rene.scharfe@lsrfire.ath.cx>2009-03-03 00:19:30 +0100
committerJunio C Hamano <gitster@pobox.com>2009-03-02 18:28:06 -0800
commit56384e61ead8d41c39bfafb535eedcf67ef4fcc3 (patch)
treee3aab3049a0fb58821be2a9adba740bb74fb89b9 /compat
parentce163c793d2058056ccc57563c350fd6415397ca (diff)
downloadgit-56384e61ead8d41c39bfafb535eedcf67ef4fcc3.tar.gz
git-56384e61ead8d41c39bfafb535eedcf67ef4fcc3.tar.xz
optimize compat/ memmem()
When memmem() was imported from glibc 2.2 into compat/, an optimization was dropped in the process, in order to make the code smaller and simpler. It was OK because memmem() wasn't used in performance-critical code. Now the situation has changed and we can benefit from this optimization. The trick is to avoid calling memcmp() if the first character of the needle already doesn't match. Checking one character directly is much cheaper than the function call overhead. We keep the first character of the needle in the variable named point and the rest in the one named tail. The following commands were run in a Linux kernel repository and timed, the best of five results is shown: $ STRING='Ensure that the real time constraints are schedulable.' $ git log -S"$STRING" HEAD -- kernel/sched.c >/dev/null On Windows Vista x64, before: real 0m8.470s user 0m0.000s sys 0m0.000s And after the patch: real 0m1.887s user 0m0.000s sys 0m0.000s Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'compat')
-rw-r--r--compat/memmem.c5
1 files changed, 4 insertions, 1 deletions
diff --git a/compat/memmem.c b/compat/memmem.c
index cd0d87736..56bcb4277 100644
--- a/compat/memmem.c
+++ b/compat/memmem.c
@@ -5,6 +5,8 @@ void *gitmemmem(const void *haystack, size_t haystack_len,
{
const char *begin = haystack;
const char *last_possible = begin + haystack_len - needle_len;
+ const char *tail = needle;
+ char point;
/*
* The first occurrence of the empty string is deemed to occur at
@@ -20,8 +22,9 @@ void *gitmemmem(const void *haystack, size_t haystack_len,
if (haystack_len < needle_len)
return NULL;
+ point = *tail++;
for (; begin <= last_possible; begin++) {
- if (!memcmp(begin, needle, needle_len))
+ if (*begin == point && !memcmp(begin + 1, tail, needle_len - 1))
return (void *)begin;
}