Skip to content

Commit 5b0672a

Browse files
avargitster
authored andcommitted
pickaxe -S: slightly optimize contains()
When the "log -S<pat>" switch counts occurrences of <pat> on the pre-image and post-image of a change. As soon as we know we had e.g. 1 before and 2 now we can stop, we don't need to keep counting past 2. With this change a diff between A and B may have different performance characteristics than between B and A. That's OK in this case, since we'll emit the same output, and the effect is to make one of them better. I'm picking a check of "one" first on the assumption that it's a more common case to have files grow over time than not. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5d35a95 commit 5b0672a

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

diffcore-pickaxe.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
6868
return ecbdata.hit;
6969
}
7070

71-
static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
71+
static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws,
72+
unsigned int limit)
7273
{
7374
unsigned int cnt = 0;
7475
unsigned long sz = mf->size;
@@ -88,6 +89,9 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
8889
sz--;
8990
}
9091
cnt++;
92+
93+
if (limit && cnt == limit)
94+
return cnt;
9195
}
9296

9397
} else { /* Classic exact string match */
@@ -99,6 +103,9 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
99103
sz -= offset + kwsm.size[0];
100104
data += offset + kwsm.size[0];
101105
cnt++;
106+
107+
if (limit && cnt == limit)
108+
return cnt;
102109
}
103110
}
104111
return cnt;
@@ -108,8 +115,8 @@ static int has_changes(mmfile_t *one, mmfile_t *two,
108115
struct diff_options *o,
109116
regex_t *regexp, kwset_t kws)
110117
{
111-
unsigned int c1 = one ? contains(one, regexp, kws) : 0;
112-
unsigned int c2 = two ? contains(two, regexp, kws) : 0;
118+
unsigned int c1 = one ? contains(one, regexp, kws, 0) : 0;
119+
unsigned int c2 = two ? contains(two, regexp, kws, c1 + 1) : 0;
113120
return c1 != c2;
114121
}
115122

0 commit comments

Comments
 (0)