git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] diffcore-pickaxe: use memmem()

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
Mar 2, 2009, 23:00 UTC
Message-ID
<49AC6527.5010808@lsrfire.ath.cx>
In-Reply-To
<cover.1235629933.git.gitster@pobox.com>

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 <rene.scharfe@lsrfire.ath.cx>
---
 diffcore-pickaxe.c |   18 ++++++++----------
 1 files changed, 8 insertions(+), 10 deletions(-)
diff --git a/diffcore-pickaxe.c b/diffcore-pickaxe.c
index af9fffe..574b3e8 100644
--- a/diffcore-pickaxe.c
+++ b/diffcore-pickaxe.c
@@ -10,7 +10,7 @@ static unsigned int contains(struct diff_filespec *one,
 			     regex_t *regexp)
 {
 	unsigned int cnt;
-	unsigned long offset, sz;
+	unsigned long sz;
 	const char *data;
 	if (diff_populate_filespec(one, 0))
 		return 0;
@@ -33,15 +33,13 @@ static unsigned int contains(struct diff_filespec *one,
 		}
 
 	} else { /* Classic exact string match */
-		/* Yes, I've heard of strstr(), but the thing is *data may
-		 * not be NUL terminated.  Sue me.
-		 */
-		for (offset = 0; offset + len <= sz; offset++) {
-			/* we count non-overlapping occurrences of needle */
-			if (!memcmp(needle, data + offset, len)) {
-				offset += len - 1;
-				cnt++;
-			}
+		while (sz) {
+			const char *found = memmem(data, sz, needle, len);
+			if (!found)
+				break;
+			sz -= found - data + len;
+			data = found + len;
+			cnt++;
 		}
 	}
 	diff_free_filespec_data(one);
-- 
1.6.2.rc2
Previous: Junio C HamanoNext: René Scharfe
Message 22 of 23 in “Pickaxe search clean-up and optimization”
  1. 0/4 Pickaxe search clean-up and optimizationJunio C Hamano, Feb 26, 2009
  2. 1/4 diffcore-pickaxe: refactor diffcore_pickaxe()Junio C Hamano, Feb 26, 2009
  3. René ScharfeFeb 27, 2009
  4. 2/4 diffcore-pickaxe: micro-optimize has_match() functionJunio C Hamano, Feb 26, 2009
  5. 3/4 diffcore-pickaxe: further refactor count_match()Junio C Hamano, Feb 26, 2009
  6. Kjetil BarvikFeb 26, 2009
  7. René ScharfeFeb 28, 2009
  8. Junio C HamanoFeb 28, 2009
  9. Junio C HamanoFeb 28, 2009
  10. René ScharfeFeb 28, 2009
  11. Junio C HamanoFeb 28, 2009
  12. René ScharfeFeb 28, 2009
  13. import memmem() with linear complexity from GnulibRené Scharfe, Feb 28, 2009
  14. Mike HommeyFeb 28, 2009
  15. Jeff KingMar 1, 2009
  16. René ScharfeMar 1, 2009
  17. René ScharfeMar 1, 2009
  18. Junio C HamanoMar 1, 2009
  19. René ScharfeMar 1, 2009
  20. 4/4 diffcore-pickaxe: optimize by trimming common initial and trailing partsJunio C Hamano, Feb 26, 2009
  21. Junio C HamanoFeb 26, 2009
  22. 1/2 diffcore-pickaxe: use memmem()René Scharfe, Mar 2, 2009
  23. 2/2 optimize compat/ memmem()René Scharfe, Mar 2, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.