Skip to content

Commit dadccae

Browse files
phillipwoodgitster
authored andcommitted
xdiff: reduce size of action arrays
When the myers algorithm is selected the input files are pre-processed to remove any common prefix and suffix. Then any lines that appear only in one side of the diff are marked as changed and frequently occurring lines are marked as changed if they are adjacent to a changed line. This step requires a couple of temporary arrays. As as the common prefix and suffix have already been removed, the arrays only need to be big enough to hold the lines between them, not the whole file. Reduce the size of the arrays and adjust the loops that use them accordingly while taking care to keep indexing the arrays in xdfile_t with absolute line numbers. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 82f4f31 commit dadccae

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

xdiff/xprepare.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,16 +273,19 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
273273
uint8_t *action1 = NULL, *action2 = NULL;
274274
bool need_min = !!(cf->flags & XDF_NEED_MINIMAL);
275275
int ret = 0;
276+
ptrdiff_t off = xdf1->dstart;
277+
ptrdiff_t len1 = xdf1->dend - off + 1;
278+
ptrdiff_t len2 = xdf2->dend - off + 1;
276279

277280
/*
278281
* Create temporary arrays that will help us decide if
279282
* changed[i] should remain false, or become true.
280283
*/
281-
if (!XDL_CALLOC_ARRAY(action1, xdf1->nrec + 1)) {
284+
if (!XDL_CALLOC_ARRAY(action1, len1)) {
282285
ret = -1;
283286
goto cleanup;
284287
}
285-
if (!XDL_CALLOC_ARRAY(action2, xdf2->nrec + 1)) {
288+
if (!XDL_CALLOC_ARRAY(action2, len2)) {
286289
ret = -1;
287290
goto cleanup;
288291
}
@@ -299,8 +302,8 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
299302
/*
300303
* Initialize temporary arrays with DISCARD, KEEP, or INVESTIGATE.
301304
*/
302-
for (i = xdf1->dstart; i <= xdf1->dend; i++) {
303-
size_t mph1 = xdf1->recs[i].minimal_perfect_hash;
305+
for (i = 0; i < len1; i++) {
306+
size_t mph1 = xdf1->recs[i + off].minimal_perfect_hash;
304307
rcrec = cf->rcrecs[mph1];
305308
nm = rcrec ? rcrec->len2 : 0;
306309
if (nm == 0)
@@ -311,8 +314,8 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
311314
action1[i] = INVESTIGATE;
312315
}
313316

314-
for (i = xdf2->dstart; i <= xdf2->dend; i++) {
315-
size_t mph2 = xdf2->recs[i].minimal_perfect_hash;
317+
for (i = 0; i < len2; i++) {
318+
size_t mph2 = xdf2->recs[i + off].minimal_perfect_hash;
316319
rcrec = cf->rcrecs[mph2];
317320
nm = rcrec ? rcrec->len1 : 0;
318321
if (nm == 0)
@@ -328,37 +331,37 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
328331
* false, or become true.
329332
*/
330333
xdf1->nreff = 0;
331-
for (i = xdf1->dstart; i <= xdf1->dend; i++) {
334+
for (i = 0; i < len1; i++) {
332335
if (action1[i] == INVESTIGATE) {
333-
if (!xdl_clean_mmatch(action1, i, xdf1->dstart, xdf1->dend))
336+
if (!xdl_clean_mmatch(action1, i, 0, len1 - 1))
334337
action1[i] = KEEP;
335338
else
336339
action1[i] = DISCARD;
337340
}
338341

339342
if (action1[i] == KEEP) {
340-
xdf1->reference_index[xdf1->nreff++] = i;
343+
xdf1->reference_index[xdf1->nreff++] = i + off;
341344
/* changed[i] remains false */
342345
} else if (action1[i] == DISCARD)
343-
xdf1->changed[i] = true;
346+
xdf1->changed[i + off] = true;
344347
else
345348
BUG("Illegal state for action1[i]");
346349
}
347350

348351
xdf2->nreff = 0;
349-
for (i = xdf2->dstart; i <= xdf2->dend; i++) {
352+
for (i = 0; i < len2; i++) {
350353
if (action2[i] == INVESTIGATE) {
351-
if (!xdl_clean_mmatch(action2, i, xdf2->dstart, xdf2->dend))
354+
if (!xdl_clean_mmatch(action2, i, 0, len2 - 1))
352355
action2[i] = KEEP;
353356
else
354357
action2[i] = DISCARD;
355358
}
356359

357360
if (action2[i] == KEEP) {
358-
xdf2->reference_index[xdf2->nreff++] = i;
361+
xdf2->reference_index[xdf2->nreff++] = i + off;
359362
/* changed[i] remains false */
360363
} else if (action2[i] == DISCARD)
361-
xdf2->changed[i] = true;
364+
xdf2->changed[i + off] = true;
362365
else
363366
BUG("Illegal state for action2[i]");
364367
}

0 commit comments

Comments
 (0)