Skip to content

Commit 951e080

Browse files
committed
Merge branch 'pw/xdiff-shrink-memory-consumption' into jch
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 7d373b3 + 40c92ff commit 951e080

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
}
@@ -299,8 +297,8 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
299297
/*
300298
* Initialize temporary arrays with DISCARD, KEEP, or INVESTIGATE.
301299
*/
302-
for (i = xdf1->dstart; i <= xdf1->dend; i++) {
303-
size_t mph1 = xdf1->recs[i].minimal_perfect_hash;
300+
for (i = 0; i < len1; i++) {
301+
size_t mph1 = xdf1->recs[i + off].minimal_perfect_hash;
304302
rcrec = cf->rcrecs[mph1];
305303
nm = rcrec ? rcrec->len2 : 0;
306304
if (nm == 0)
@@ -311,8 +309,8 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
311309
action1[i] = INVESTIGATE;
312310
}
313311

314-
for (i = xdf2->dstart; i <= xdf2->dend; i++) {
315-
size_t mph2 = xdf2->recs[i].minimal_perfect_hash;
312+
for (i = 0; i < len2; i++) {
313+
size_t mph2 = xdf2->recs[i + off].minimal_perfect_hash;
316314
rcrec = cf->rcrecs[mph2];
317315
nm = rcrec ? rcrec->len1 : 0;
318316
if (nm == 0)
@@ -328,25 +326,25 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
328326
* false, or become true.
329327
*/
330328
xdf1->nreff = 0;
331-
for (i = xdf1->dstart; i <= xdf1->dend; i++) {
329+
for (i = 0; i < len1; i++) {
332330
if (action1[i] == KEEP ||
333-
(action1[i] == INVESTIGATE && !xdl_clean_mmatch(action1, i, xdf1->dstart, xdf1->dend))) {
334-
xdf1->reference_index[xdf1->nreff++] = i;
331+
(action1[i] == INVESTIGATE && !xdl_clean_mmatch(action1, i, len1))) {
332+
xdf1->reference_index[xdf1->nreff++] = i + off;
335333
/* changed[i] remains false, i.e. keep */
336334
} else {
337-
xdf1->changed[i] = true;
335+
xdf1->changed[i + off] = true;
338336
/* i.e. discard */
339337
}
340338
}
341339

342340
xdf2->nreff = 0;
343-
for (i = xdf2->dstart; i <= xdf2->dend; i++) {
341+
for (i = 0; i < len2; i++) {
344342
if (action2[i] == KEEP ||
345-
(action2[i] == INVESTIGATE && !xdl_clean_mmatch(action2, i, xdf2->dstart, xdf2->dend))) {
346-
xdf2->reference_index[xdf2->nreff++] = i;
343+
(action2[i] == INVESTIGATE && !xdl_clean_mmatch(action2, i, len2))) {
344+
xdf2->reference_index[xdf2->nreff++] = i + off;
347345
/* changed[i] remains false, i.e. keep */
348346
} else {
349-
xdf2->changed[i] = true;
347+
xdf2->changed[i + off] = true;
350348
/* i.e. discard */
351349
}
352350
}

0 commit comments

Comments
 (0)