From: "René Scharfe" <rene.scharfe@lsrfire.ath.cx>
To: Git Mailing List <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>
Subject: [PATCH 4/6] grep: handle pre context lines on demand
Date: Thu, 02 Jul 2009 00:05:17 +0200 [thread overview]
Message-ID: <4A4BDD9D.8090407@lsrfire.ath.cx> (raw)
In-Reply-To: <4A4BDC65.80504@lsrfire.ath.cx>
Factor out pre context line handling into the new function
show_pre_context() and change the algorithm to rewind by looking for
newline characters and roll forward again, instead of maintaining an
array of line beginnings and ends.
This is slower for hits, but the cost for non-matching lines becomes
zero. Normally, there are far more non-matching lines, so the time
spent in total decreases.
Before this patch (current Linux kernel repo, best of five runs):
$ time git grep --no-ext-grep -B1 memset >/dev/null
real 0m2.134s
user 0m1.932s
sys 0m0.196s
$ time git grep --no-ext-grep -B1000 memset >/dev/null
real 0m12.059s
user 0m11.837s
sys 0m0.224s
The same with this patch:
$ time git grep --no-ext-grep -B1 memset >/dev/null
real 0m2.117s
user 0m1.892s
sys 0m0.228s
$ time git grep --no-ext-grep -B1000 memset >/dev/null
real 0m2.986s
user 0m2.696s
sys 0m0.288s
Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
grep.c | 61 ++++++++++++++++++++++++++++++++-----------------------------
1 files changed, 32 insertions(+), 29 deletions(-)
diff --git a/grep.c b/grep.c
index 4bca759..9b9d2e3 100644
--- a/grep.c
+++ b/grep.c
@@ -531,16 +531,42 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
printf("%.*s\n", rest, bol);
}
+static void show_pre_context(struct grep_opt *opt, const char *name, char *buf,
+ char *bol, unsigned lno)
+{
+ unsigned cur = lno, from = 1;
+
+ if (opt->pre_context < lno)
+ from = lno - opt->pre_context;
+ if (from <= opt->last_shown)
+ from = opt->last_shown + 1;
+
+ /* Rewind. */
+ while (bol > buf && cur > from) {
+ bol--;
+ while (bol > buf && bol[-1] != '\n')
+ bol--;
+ cur--;
+ }
+
+ /* Back forward. */
+ while (cur < lno) {
+ char *eol = bol;
+
+ while (*eol != '\n')
+ eol++;
+ show_line(opt, bol, eol, name, cur, '-');
+ bol = eol + 1;
+ cur++;
+ }
+}
+
static int grep_buffer_1(struct grep_opt *opt, const char *name,
char *buf, unsigned long size, int collect_hits)
{
char *bol = buf;
unsigned long left = size;
unsigned lno = 1;
- struct pre_context_line {
- char *bol;
- char *eol;
- } *prev = NULL, *pcl;
unsigned last_hit = 0;
int binary_match_only = 0;
unsigned count = 0;
@@ -561,9 +587,6 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
}
}
- if (opt->pre_context)
- prev = xcalloc(opt->pre_context, sizeof(*prev));
-
while (left) {
char *eol, ch;
int hit;
@@ -610,21 +633,8 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
* the context which is nonsense, but the user
* deserves to get that ;-).
*/
- if (opt->pre_context) {
- unsigned from;
- if (opt->pre_context < lno)
- from = lno - opt->pre_context;
- else
- from = 1;
- if (from <= opt->last_shown)
- from = opt->last_shown + 1;
- while (from < lno) {
- pcl = &prev[lno-from-1];
- show_line(opt, pcl->bol, pcl->eol,
- name, from, '-');
- from++;
- }
- }
+ if (opt->pre_context)
+ show_pre_context(opt, name, buf, bol, lno);
if (!opt->count)
show_line(opt, bol, eol, name, lno, ':');
last_hit = lno;
@@ -636,12 +646,6 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
*/
show_line(opt, bol, eol, name, lno, '-');
}
- if (opt->pre_context) {
- memmove(prev+1, prev,
- (opt->pre_context-1) * sizeof(*prev));
- prev->bol = bol;
- prev->eol = eol;
- }
next_line:
bol = eol + 1;
@@ -651,7 +655,6 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
lno++;
}
- free(prev);
if (collect_hits)
return 0;
--
1.6.3.3
next prev parent reply other threads:[~2009-07-01 22:05 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-07-01 22:00 [PATCH 0/6] grep: add option -p/--show-function, similar to diff's René Scharfe
2009-07-01 22:01 ` [PATCH 1/6] userdiff: add xdiff_clear_find_func() René Scharfe
2009-07-01 22:02 ` [PATCH 2/6] grep: move context hunk mark handling into show_line() René Scharfe
2009-07-01 22:55 ` Junio C Hamano
2009-07-02 3:15 ` René Scharfe
2009-07-02 5:24 ` Junio C Hamano
2009-07-01 22:03 ` [PATCH 3/6] grep: print context hunk marks between files René Scharfe
2009-07-01 22:05 ` René Scharfe [this message]
2009-07-01 22:06 ` [PATCH 5/6] grep: add option -p/--show-function René Scharfe
2009-07-02 2:35 ` Junio C Hamano
2009-07-02 4:38 ` René Scharfe
2009-07-02 5:27 ` Junio C Hamano
2009-07-02 6:16 ` René Scharfe
2009-07-02 15:42 ` René Scharfe
2009-07-01 22:07 ` [PATCH 6/6] grep -p: support user defined regular expressions René Scharfe
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=4A4BDD9D.8090407@lsrfire.ath.cx \
--to=rene.scharfe@lsrfire.ath.cx \
--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).