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>

Phillip Wood committed May 4, 2026 at 15:06 UTC a81411253323208e1e8d3591247c27fefa8a2045
1 file changed +17 -14
xdiff/xprepare.c
+17 -14
@@ -273,16 +273,19 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
273 uint8_t *action1 = NULL, *action2 = NULL;
274 bool need_min = !!(cf->flags & XDF_NEED_MINIMAL);
275 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;
279
280 /*
281 * Create temporary arrays that will help us decide if
282 * changed[i] should remain false, or become true.
283 */
281 - if (!XDL_CALLOC_ARRAY(action1, xdf1->nrec + 1)) {
284 + if (!XDL_CALLOC_ARRAY(action1, len1)) {
285 ret = -1;
286 goto cleanup;
287 }
285 - if (!XDL_CALLOC_ARRAY(action2, xdf2->nrec + 1)) {
288 + if (!XDL_CALLOC_ARRAY(action2, len2)) {
289 ret = -1;
290 goto cleanup;
291 }
@@ -298,8 +301,8 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
301 if (mlim1 > XDL_MAX_EQLIMIT)
302 mlim1 = XDL_MAX_EQLIMIT;
303 }
301 - for (i = xdf1->dstart; i <= xdf1->dend; i++) {
302 - size_t mph1 = xdf1->recs[i].minimal_perfect_hash;
304 + for (i = 0; i < len1; i++) {
305 + size_t mph1 = xdf1->recs[i + off].minimal_perfect_hash;
306 rcrec = cf->rcrecs[mph1];
307 nm = rcrec ? rcrec->len2 : 0;
308 if (nm == 0)
@@ -318,8 +321,8 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
321 if (mlim2 > XDL_MAX_EQLIMIT)
322 mlim2 = XDL_MAX_EQLIMIT;
323 }
321 - for (i = xdf2->dstart; i <= xdf2->dend; i++) {
322 - size_t mph2 = xdf2->recs[i].minimal_perfect_hash;
324 + for (i = 0; i < len2; i++) {
325 + size_t mph2 = xdf2->recs[i + off].minimal_perfect_hash;
326 rcrec = cf->rcrecs[mph2];
327 nm = rcrec ? rcrec->len1 : 0;
328 if (nm == 0)
@@ -335,42 +338,42 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
338 * false, or become true.
339 */
340 xdf1->nreff = 0;
338 - for (i = xdf1->dstart; i <= xdf1->dend; i++) {
341 + for (i = 0; i < len1; i++) {
342 uint8_t action = action1[i];
343
344 if (action == INVESTIGATE) {
342 - if (!xdl_clean_mmatch(action1, i, xdf1->dstart, xdf1->dend))
345 + if (!xdl_clean_mmatch(action1, i, 0, len1 - 1))
346 action = KEEP;
347 else
348 action = DISCARD;
349 }
350
351 if (action == KEEP) {
349 - xdf1->reference_index[xdf1->nreff++] = i;
352 + xdf1->reference_index[xdf1->nreff++] = i + off;
353 /* changed[i] remains false */
354 } else if (action == DISCARD) {
352 - xdf1->changed[i] = true;
355 + xdf1->changed[i + off] = true;
356 } else {
357 BUG("Illegal state for action");
358 }
359 }
360
361 xdf2->nreff = 0;
359 - for (i = xdf2->dstart; i <= xdf2->dend; i++) {
362 + for (i = 0; i < len2; i++) {
363 uint8_t action = action2[i];
364
365 if (action == INVESTIGATE) {
363 - if (!xdl_clean_mmatch(action2, i, xdf2->dstart, xdf2->dend))
366 + if (!xdl_clean_mmatch(action2, i, 0, len2 - 1))
367 action = KEEP;
368 else
369 action = DISCARD;
370 }
371
372 if (action == KEEP) {
370 - xdf2->reference_index[xdf2->nreff++] = i;
373 + xdf2->reference_index[xdf2->nreff++] = i + off;
374 /* changed[i] remains false */
375 } else if (action == DISCARD) {
373 - xdf2->changed[i] = true;
376 + xdf2->changed[i + off] = true;
377 } else {
378 BUG("Illegal state for action");
379 }