Skip to content

Commit c099789

Browse files
René Scharfegitster
authored andcommitted
diff: avoid repeated scanning while looking for funcname
For each hunk, xdl_find_func searches the preimage for a function name until the beginning of the file. If the file does not contain any function names, this search has complexity O(n^2) in the number of hunks n. Instead, inline xdl_find_func() and keep track of up to which line we have scanned already and the contents of the last funcname line that we have found. Noticed and a different approach proposed by Clemens Buchacher. This alternative solution was done by René Scharfe. Signed-off-by: Junio C Hamano <[email protected]>
1 parent ad14b45 commit c099789

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

xdiff/xemit.c

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,6 @@ static long def_ff(const char *rec, long len, char *buf, long sz, void *priv)
8585
return -1;
8686
}
8787

88-
static void xdl_find_func(xdfile_t *xf, long i, char *buf, long sz, long *ll,
89-
find_func_t ff, void *ff_priv) {
90-
91-
/*
92-
* Be quite stupid about this for now. Find a line in the old file
93-
* before the start of the hunk (and context) which starts with a
94-
* plausible character.
95-
*/
96-
97-
const char *rec;
98-
long len;
99-
100-
while (i-- > 0) {
101-
len = xdl_get_rec(xf, i, &rec);
102-
if ((*ll = ff(rec, len, buf, sz, ff_priv)) >= 0)
103-
return;
104-
}
105-
*ll = 0;
106-
}
107-
108-
10988
static int xdl_emit_common(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
11089
xdemitconf_t const *xecfg) {
11190
xdfile_t *xdf = &xe->xdf1;
@@ -127,6 +106,7 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
127106
xdchange_t *xch, *xche;
128107
char funcbuf[80];
129108
long funclen = 0;
109+
long funclineprev = -1;
130110
find_func_t ff = xecfg->find_func ? xecfg->find_func : def_ff;
131111

132112
if (xecfg->flags & XDL_EMIT_COMMON)
@@ -150,9 +130,19 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
150130
*/
151131

152132
if (xecfg->flags & XDL_EMIT_FUNCNAMES) {
153-
xdl_find_func(&xe->xdf1, s1, funcbuf,
154-
sizeof(funcbuf), &funclen,
155-
ff, xecfg->find_func_priv);
133+
long l;
134+
for (l = s1 - 1; l >= 0 && l > funclineprev; l--) {
135+
const char *rec;
136+
long reclen = xdl_get_rec(&xe->xdf1, l, &rec);
137+
long newfunclen = ff(rec, reclen, funcbuf,
138+
sizeof(funcbuf),
139+
xecfg->find_func_priv);
140+
if (newfunclen >= 0) {
141+
funclen = newfunclen;
142+
break;
143+
}
144+
}
145+
funclineprev = s1 - 1;
156146
}
157147
if (xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2,
158148
funcbuf, funclen, ecb) < 0)

0 commit comments

Comments
 (0)