Skip to content

Commit ce163c7

Browse files
René Scharfegitster
authored andcommitted
diffcore-pickaxe: use memmem()
Use memmem() instead of open-coding it. The system libraries usually have a much faster version than the memcmp()-loop here. Even our own fall-back in compat/, which is used on Windows, is slightly faster. 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 Ubuntu 8.10 x64, before (v1.6.2-rc2): 8.09user 0.04system 0:08.14elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (0major+30952minor)pagefaults 0swaps And with the patch: 1.50user 0.04system 0:01.54elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (0major+30645minor)pagefaults 0swaps On Fedora 10 x64, before: 8.34user 0.05system 0:08.39elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (0major+29268minor)pagefaults 0swaps And with the patch: 1.15user 0.05system 0:01.20elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (0major+32253minor)pagefaults 0swaps On Windows Vista x64, before: real 0m9.204s user 0m0.000s sys 0m0.000s And with the patch: real 0m8.470s user 0m0.000s sys 0m0.000s Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f474c52 commit ce163c7

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

diffcore-pickaxe.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static unsigned int contains(struct diff_filespec *one,
1010
regex_t *regexp)
1111
{
1212
unsigned int cnt;
13-
unsigned long offset, sz;
13+
unsigned long sz;
1414
const char *data;
1515
if (diff_populate_filespec(one, 0))
1616
return 0;
@@ -33,15 +33,13 @@ static unsigned int contains(struct diff_filespec *one,
3333
}
3434

3535
} else { /* Classic exact string match */
36-
/* Yes, I've heard of strstr(), but the thing is *data may
37-
* not be NUL terminated. Sue me.
38-
*/
39-
for (offset = 0; offset + len <= sz; offset++) {
40-
/* we count non-overlapping occurrences of needle */
41-
if (!memcmp(needle, data + offset, len)) {
42-
offset += len - 1;
43-
cnt++;
44-
}
36+
while (sz) {
37+
const char *found = memmem(data, sz, needle, len);
38+
if (!found)
39+
break;
40+
sz -= found - data + len;
41+
data = found + len;
42+
cnt++;
4543
}
4644
}
4745
diff_free_filespec_data(one);

0 commit comments

Comments
 (0)