Skip to content

Commit b84f166

Browse files
committed
Merge branch 'pw/xdiff-shrink-memory-consumption' into seen
Shrink wasted memory in Myers diff that does not account for common prefix and suffix removal. * pw/xdiff-shrink-memory-consumption: xdiff: reduce the size of array xprepare: simplify error handling xdiff: cleanup xdl_clean_mmatch() xdiff: reduce size of action arrays
2 parents 664f45d + dca97e7 commit b84f166

1 file changed

Lines changed: 22 additions & 24 deletions

File tree

xdiff/xprepare.c

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,6 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
171171
if (!XDL_CALLOC_ARRAY(xdf->changed, xdf->nrec + 2))
172172
goto abort;
173173

174-
if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
175-
(XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF)) {
176-
if (!XDL_ALLOC_ARRAY(xdf->reference_index, xdf->nrec + 1))
177-
goto abort;
178-
}
179-
180174
xdf->changed += 1;
181175
xdf->nreff = 0;
182176
xdf->dstart = 0;
@@ -197,8 +191,9 @@ void xdl_free_env(xdfenv_t *xe) {
197191
}
198192

199193

200-
static bool xdl_clean_mmatch(uint8_t const *action, ptrdiff_t i, ptrdiff_t s, ptrdiff_t e) {
194+
static bool xdl_clean_mmatch(uint8_t const *action, ptrdiff_t i, ptrdiff_t len) {
201195
ptrdiff_t r, rdis0, rpdis0, rdis1, rpdis1;
196+
ptrdiff_t s = 0, e = len - 1;
202197

203198
/*
204199
* Limits the window that is examined during the similar-lines
@@ -273,16 +268,19 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
273268
uint8_t *action1 = NULL, *action2 = NULL;
274269
bool need_min = !!(cf->flags & XDF_NEED_MINIMAL);
275270
int ret = 0;
271+
ptrdiff_t off = xdf1->dstart;
272+
ptrdiff_t len1 = xdf1->dend - off + 1;
273+
ptrdiff_t len2 = xdf2->dend - off + 1;
276274

277275
/*
278276
* Create temporary arrays that will help us decide if
279277
* changed[i] should remain false, or become true.
280278
*/
281-
if (!XDL_CALLOC_ARRAY(action1, xdf1->nrec + 1)) {
282-
ret = -1;
283-
goto cleanup;
284-
}
285-
if (!XDL_CALLOC_ARRAY(action2, xdf2->nrec + 1)) {
279+
if (!XDL_CALLOC_ARRAY(action1, len1) ||
280+
!XDL_CALLOC_ARRAY(action2, len2) ||
281+
!XDL_ALLOC_ARRAY(xdf1->reference_index, len1) ||
282+
!XDL_ALLOC_ARRAY(xdf2->reference_index, len2))
283+
{
286284
ret = -1;
287285
goto cleanup;
288286
}
@@ -298,8 +296,8 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
298296
if (mlim1 > XDL_MAX_EQLIMIT)
299297
mlim1 = XDL_MAX_EQLIMIT;
300298
}
301-
for (i = xdf1->dstart; i <= xdf1->dend; i++) {
302-
size_t mph1 = xdf1->recs[i].minimal_perfect_hash;
299+
for (i = 0; i < len1; i++) {
300+
size_t mph1 = xdf1->recs[i + off].minimal_perfect_hash;
303301
rcrec = cf->rcrecs[mph1];
304302
nm = rcrec ? rcrec->len2 : 0;
305303
if (nm == 0)
@@ -318,8 +316,8 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
318316
if (mlim2 > XDL_MAX_EQLIMIT)
319317
mlim2 = XDL_MAX_EQLIMIT;
320318
}
321-
for (i = xdf2->dstart; i <= xdf2->dend; i++) {
322-
size_t mph2 = xdf2->recs[i].minimal_perfect_hash;
319+
for (i = 0; i < len2; i++) {
320+
size_t mph2 = xdf2->recs[i + off].minimal_perfect_hash;
323321
rcrec = cf->rcrecs[mph2];
324322
nm = rcrec ? rcrec->len1 : 0;
325323
if (nm == 0)
@@ -335,42 +333,42 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
335333
* false, or become true.
336334
*/
337335
xdf1->nreff = 0;
338-
for (i = xdf1->dstart; i <= xdf1->dend; i++) {
336+
for (i = 0; i < len1; i++) {
339337
uint8_t action = action1[i];
340338

341339
if (action == INVESTIGATE) {
342-
if (!xdl_clean_mmatch(action1, i, xdf1->dstart, xdf1->dend))
340+
if (!xdl_clean_mmatch(action1, i, len1))
343341
action = KEEP;
344342
else
345343
action = DISCARD;
346344
}
347345

348346
if (action == KEEP) {
349-
xdf1->reference_index[xdf1->nreff++] = i;
347+
xdf1->reference_index[xdf1->nreff++] = i + off;
350348
/* changed[i] remains false */
351349
} else if (action == DISCARD) {
352-
xdf1->changed[i] = true;
350+
xdf1->changed[i + off] = true;
353351
} else {
354352
BUG("Illegal state for action");
355353
}
356354
}
357355

358356
xdf2->nreff = 0;
359-
for (i = xdf2->dstart; i <= xdf2->dend; i++) {
357+
for (i = 0; i < len2; i++) {
360358
uint8_t action = action2[i];
361359

362360
if (action == INVESTIGATE) {
363-
if (!xdl_clean_mmatch(action2, i, xdf2->dstart, xdf2->dend))
361+
if (!xdl_clean_mmatch(action2, i, len2))
364362
action = KEEP;
365363
else
366364
action = DISCARD;
367365
}
368366

369367
if (action == KEEP) {
370-
xdf2->reference_index[xdf2->nreff++] = i;
368+
xdf2->reference_index[xdf2->nreff++] = i + off;
371369
/* changed[i] remains false */
372370
} else if (action == DISCARD) {
373-
xdf2->changed[i] = true;
371+
xdf2->changed[i + off] = true;
374372
} else {
375373
BUG("Illegal state for action");
376374
}

0 commit comments

Comments
 (0)