Skip to content

Commit 9232a7a

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 c97ac79 commit 9232a7a

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,25 +331,25 @@ 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] == KEEP ||
333-
(action1[i] == INVESTIGATE && !xdl_clean_mmatch(action1, i, xdf1->dstart, xdf1->dend))) {
334-
xdf1->reference_index[xdf1->nreff++] = i;
336+
(action1[i] == INVESTIGATE && !xdl_clean_mmatch(action1, i, 0, len1 - 1))) {
337+
xdf1->reference_index[xdf1->nreff++] = i + off;
335338
/* changed[i] remains false, i.e. keep */
336339
} else {
337-
xdf1->changed[i] = true;
340+
xdf1->changed[i + off] = true;
338341
/* i.e. discard */
339342
}
340343
}
341344

342345
xdf2->nreff = 0;
343-
for (i = xdf2->dstart; i <= xdf2->dend; i++) {
346+
for (i = 0; i < len2; i++) {
344347
if (action2[i] == KEEP ||
345-
(action2[i] == INVESTIGATE && !xdl_clean_mmatch(action2, i, xdf2->dstart, xdf2->dend))) {
346-
xdf2->reference_index[xdf2->nreff++] = i;
348+
(action2[i] == INVESTIGATE && !xdl_clean_mmatch(action2, i, 0, len2 - 1))) {
349+
xdf2->reference_index[xdf2->nreff++] = i + off;
347350
/* changed[i] remains false, i.e. keep */
348351
} else {
349-
xdf2->changed[i] = true;
352+
xdf2->changed[i + off] = true;
350353
/* i.e. discard */
351354
}
352355
}

0 commit comments

Comments
 (0)