git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Fredrik Kuivinen <frekui@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>
Subject: [PATCH 4/5] Use kwset in pickaxe
Date: Sat, 13 Feb 2010 15:21:00 +0100	[thread overview]
Message-ID: <20100213142100.GE9543@fredrik-laptop> (raw)
In-Reply-To: <20100213141558.22851.13660.stgit@fredrik-laptop>

Best of five runs in the git repository:

before:

$ time git log -Sqwerty

real	0m32.517s
user	0m32.134s
sys	0m0.388s

after:

$ time git log -Sqwerty

real	0m24.299s
user	0m23.645s
sys	0m0.652s

So the kwset code is about 25% faster.

Signed-off-by: Fredrik Kuivinen <frekui@gmail.com>
---

 Makefile           |    2 ++
 diffcore-pickaxe.c |   34 +++++++++++++++++++++++-----------
 2 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/Makefile b/Makefile
index 7bf2fca..5687b99 100644
--- a/Makefile
+++ b/Makefile
@@ -479,6 +479,7 @@ LIB_H += unpack-trees.h
 LIB_H += userdiff.h
 LIB_H += utf8.h
 LIB_H += wt-status.h
+LIB_H += kwset.h
 
 LIB_OBJS += abspath.o
 LIB_OBJS += advice.o
@@ -591,6 +592,7 @@ LIB_OBJS += write_or_die.o
 LIB_OBJS += ws.o
 LIB_OBJS += wt-status.o
 LIB_OBJS += xdiff-interface.o
+LIB_OBJS += kwset.o
 
 BUILTIN_OBJS += builtin-add.o
 BUILTIN_OBJS += builtin-annotate.o
diff --git a/diffcore-pickaxe.c b/diffcore-pickaxe.c
index d0ef839..6563a4e 100644
--- a/diffcore-pickaxe.c
+++ b/diffcore-pickaxe.c
@@ -4,10 +4,11 @@
 #include "cache.h"
 #include "diff.h"
 #include "diffcore.h"
+#include "kwset.h"
 
 static unsigned int contains(struct diff_filespec *one,
 			     const char *needle, unsigned long len,
-			     regex_t *regexp)
+			     regex_t *regexp, kwset_t kws)
 {
 	unsigned int cnt;
 	unsigned long sz;
@@ -36,9 +37,12 @@ static unsigned int contains(struct diff_filespec *one,
 
 	} else { /* Classic exact string match */
 		while (sz) {
-			const char *found = memmem(data, sz, needle, len);
-			if (!found)
+			size_t offset = kwsexec(kws, data, sz, NULL);
+			const char *found;
+			if (offset == -1)
 				break;
+			else
+				found = data + offset;
 			sz -= found - data + len;
 			data = found + len;
 			cnt++;
@@ -54,6 +58,7 @@ void diffcore_pickaxe(const char *needle, int opts)
 	unsigned long len = strlen(needle);
 	int i, has_changes;
 	regex_t regex, *regexp = NULL;
+	kwset_t kws = NULL;
 	struct diff_queue_struct outq;
 	outq.queue = NULL;
 	outq.nr = outq.alloc = 0;
@@ -69,6 +74,10 @@ void diffcore_pickaxe(const char *needle, int opts)
 			die("invalid pickaxe regex: %s", errbuf);
 		}
 		regexp = &regex;
+	} else {
+		kws = kwsalloc(NULL);
+		kwsincr(kws, needle, len);
+		kwsprep(kws);
 	}
 
 	if (opts & DIFF_PICKAXE_ALL) {
@@ -79,16 +88,16 @@ void diffcore_pickaxe(const char *needle, int opts)
 				if (!DIFF_FILE_VALID(p->two))
 					continue; /* ignore unmerged */
 				/* created */
-				if (contains(p->two, needle, len, regexp))
+				if (contains(p->two, needle, len, regexp, kws))
 					has_changes++;
 			}
 			else if (!DIFF_FILE_VALID(p->two)) {
-				if (contains(p->one, needle, len, regexp))
+				if (contains(p->one, needle, len, regexp, kws))
 					has_changes++;
 			}
 			else if (!diff_unmodified_pair(p) &&
-				 contains(p->one, needle, len, regexp) !=
-				 contains(p->two, needle, len, regexp))
+				contains(p->one, needle, len, regexp, kws) !=
+				contains(p->two, needle, len, regexp, kws))
 				has_changes++;
 		}
 		if (has_changes)
@@ -111,16 +120,17 @@ void diffcore_pickaxe(const char *needle, int opts)
 				if (!DIFF_FILE_VALID(p->two))
 					; /* ignore unmerged */
 				/* created */
-				else if (contains(p->two, needle, len, regexp))
+				else if (contains(p->two, needle, len, regexp,
+						  kws))
 					has_changes = 1;
 			}
 			else if (!DIFF_FILE_VALID(p->two)) {
-				if (contains(p->one, needle, len, regexp))
+				if (contains(p->one, needle, len, regexp, kws))
 					has_changes = 1;
 			}
 			else if (!diff_unmodified_pair(p) &&
-				 contains(p->one, needle, len, regexp) !=
-				 contains(p->two, needle, len, regexp))
+				contains(p->one, needle, len, regexp, kws) !=
+				contains(p->two, needle, len, regexp, kws))
 				has_changes = 1;
 
 			if (has_changes)
@@ -131,6 +141,8 @@ void diffcore_pickaxe(const char *needle, int opts)
 
 	if (opts & DIFF_PICKAXE_REGEX) {
 		regfree(&regex);
+	} else {
+		kwsfree(kws);
 	}
 
 	free(q->queue);

  parent reply	other threads:[~2010-02-13 14:21 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20100213141558.22851.13660.stgit@fredrik-laptop>
2010-02-13 14:20 ` [PATCH 1/5] Add obstack.[ch] from EGLIBC 2.10 Fredrik Kuivinen
2010-02-13 14:20 ` [PATCH 2/5] Add string search routines from GNU grep Fredrik Kuivinen
2010-02-13 15:49   ` Dmitry Potapov
2010-02-13 15:56   ` Paolo Bonzini
2010-02-14 16:52     ` Fredrik Kuivinen
2010-02-13 14:20 ` [PATCH 3/5] Adapt the kwset code to Git Fredrik Kuivinen
2010-02-13 14:21 ` Fredrik Kuivinen [this message]
2010-02-13 14:21 ` [PATCH 5/5] Use kwset in grep Fredrik Kuivinen
2010-02-13 15:58   ` Paolo Bonzini
2010-02-13 17:38   ` Paolo Bonzini
2010-02-14 16:51     ` Fredrik Kuivinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100213142100.GE9543@fredrik-laptop \
    --to=frekui@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).