Skip to content

Commit ca557af

Browse files
davidelJunio C Hamano
authored andcommitted
Clean-up trivially redundant diff.
Also corrects the line numbers in unified output when using zero lines context.
1 parent fc9957b commit ca557af

File tree

4 files changed

+63
-25
lines changed

4 files changed

+63
-25
lines changed

xdiff/xdiffi.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,7 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
349349
kvdf += xe->xdf2.nreff + 1;
350350
kvdb += xe->xdf2.nreff + 1;
351351

352-
/*
353-
* Classical integer square root approximation using shifts.
354-
*/
355-
xenv.mxcost = 1;
356-
for (; ndiags; ndiags >>= 2)
357-
xenv.mxcost <<= 1;
352+
xenv.mxcost = xdl_bogosqrt(ndiags);
358353
if (xenv.mxcost < XDL_MAX_COST_MIN)
359354
xenv.mxcost = XDL_MAX_COST_MIN;
360355
xenv.snake_cnt = XDL_SNAKE_CNT;

xdiff/xprepare.c

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626

2727
#define XDL_KPDIS_RUN 4
28+
#define XDL_MAX_EQLIMIT 1024
2829

2930

3031

@@ -305,26 +306,48 @@ void xdl_free_env(xdfenv_t *xe) {
305306

306307

307308
static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
308-
long r, rdis, rpdis;
309-
310-
for (r = 1, rdis = 0, rpdis = 1; (i - r) >= s; r++) {
309+
long r, rdis0, rpdis0, rdis1, rpdis1;
310+
311+
/*
312+
* Scans the lines before 'i' to find a run of lines that either
313+
* have no match (dis[j] == 0) or have multiple matches (dis[j] > 1).
314+
* Note that we always call this function with dis[i] > 1, so the
315+
* current line (i) is already a multimatch line.
316+
*/
317+
for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) {
311318
if (!dis[i - r])
312-
rdis++;
319+
rdis0++;
313320
else if (dis[i - r] == 2)
314-
rpdis++;
321+
rpdis0++;
315322
else
316323
break;
317324
}
318-
for (r = 1; (i + r) <= e; r++) {
325+
/*
326+
* If the run before the line 'i' found only multimatch lines, we
327+
* return 0 and hence we don't make the current line (i) discarded.
328+
* We want to discard multimatch lines only when they appear in the
329+
* middle of runs with nomatch lines (dis[j] == 0).
330+
*/
331+
if (rdis0 == 0)
332+
return 0;
333+
for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) {
319334
if (!dis[i + r])
320-
rdis++;
335+
rdis1++;
321336
else if (dis[i + r] == 2)
322-
rpdis++;
337+
rpdis1++;
323338
else
324339
break;
325340
}
326-
327-
return rpdis * XDL_KPDIS_RUN < (rpdis + rdis);
341+
/*
342+
* If the run after the line 'i' found only multimatch lines, we
343+
* return 0 and hence we don't make the current line (i) discarded.
344+
*/
345+
if (rdis1 == 0)
346+
return 0;
347+
rdis1 += rdis0;
348+
rpdis1 += rpdis0;
349+
350+
return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
328351
}
329352

330353

@@ -334,34 +357,40 @@ static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
334357
* might be potentially discarded if they happear in a run of discardable.
335358
*/
336359
static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2) {
337-
long i, rhi, nreff;
360+
long i, nm, rhi, nreff, mlim;
338361
unsigned long hav;
339362
xrecord_t **recs;
340363
xrecord_t *rec;
341364
char *dis, *dis1, *dis2;
342365

343-
if (!(dis = (char *) xdl_malloc((xdf1->nrec + xdf2->nrec + 2) * sizeof(char)))) {
366+
if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) {
344367

345368
return -1;
346369
}
347-
memset(dis, 0, (xdf1->nrec + xdf2->nrec + 2) * sizeof(char));
370+
memset(dis, 0, xdf1->nrec + xdf2->nrec + 2);
348371
dis1 = dis;
349372
dis2 = dis1 + xdf1->nrec + 1;
350373

374+
if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT)
375+
mlim = XDL_MAX_EQLIMIT;
351376
for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
352377
hav = (*recs)->ha;
353378
rhi = (long) XDL_HASHLONG(hav, xdf2->hbits);
354-
for (rec = xdf2->rhash[rhi]; rec; rec = rec->next)
355-
if (rec->ha == hav && ++dis1[i] == 2)
379+
for (nm = 0, rec = xdf2->rhash[rhi]; rec; rec = rec->next)
380+
if (rec->ha == hav && ++nm == mlim)
356381
break;
382+
dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
357383
}
358384

385+
if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT)
386+
mlim = XDL_MAX_EQLIMIT;
359387
for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
360388
hav = (*recs)->ha;
361389
rhi = (long) XDL_HASHLONG(hav, xdf1->hbits);
362-
for (rec = xdf1->rhash[rhi]; rec; rec = rec->next)
363-
if (rec->ha == hav && ++dis2[i] == 2)
390+
for (nm = 0, rec = xdf1->rhash[rhi]; rec; rec = rec->next)
391+
if (rec->ha == hav && ++nm == mlim)
364392
break;
393+
dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
365394
}
366395

367396
for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];

xdiff/xutils.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@
2929

3030

3131

32+
long xdl_bogosqrt(long n) {
33+
long i;
34+
35+
/*
36+
* Classical integer square root approximation using shifts.
37+
*/
38+
for (i = 1; n > 0; n >>= 2)
39+
i <<= 1;
40+
41+
return i;
42+
}
43+
44+
3245
int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize,
3346
xdemitcb_t *ecb) {
3447
mmbuffer_t mb[3];
@@ -244,7 +257,7 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2,
244257
memcpy(buf, "@@ -", 4);
245258
nb += 4;
246259

247-
nb += xdl_num_out(buf + nb, c1 ? s1: 0);
260+
nb += xdl_num_out(buf + nb, c1 ? s1: s1 - 1);
248261

249262
if (c1 != 1) {
250263
memcpy(buf + nb, ",", 1);
@@ -256,7 +269,7 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2,
256269
memcpy(buf + nb, " +", 2);
257270
nb += 2;
258271

259-
nb += xdl_num_out(buf + nb, c2 ? s2: 0);
272+
nb += xdl_num_out(buf + nb, c2 ? s2: s2 - 1);
260273

261274
if (c2 != 1) {
262275
memcpy(buf + nb, ",", 1);

xdiff/xutils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#define XUTILS_H
2525

2626

27+
long xdl_bogosqrt(long n);
2728
int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize,
2829
xdemitcb_t *ecb);
2930
int xdl_cha_init(chastore_t *cha, long isize, long icount);

0 commit comments

Comments
 (0)