Skip to content

Commit 82f4f31

Browse files
committed
Merge branch 'en/xdiff-cleanup-3' into pw/xdiff-shrink-memory-consumption
* en/xdiff-cleanup-3: xdiff/xdl_cleanup_records: simplify INVESTIGATE handling for clarity xdiff/xdl_cleanup_records: make setting action easier to follow xdiff/xdl_cleanup_records: make limits more clear xdiff/xdl_cleanup_records: use unambiguous types xdiff: use unambiguous types in xdl_bogo_sqrt() xdiff/xdl_cleanup_records: delete local recs pointer
2 parents 2565546 + 7ff1460 commit 82f4f31

4 files changed

Lines changed: 60 additions & 32 deletions

File tree

xdiff/xdiffi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
348348
kvdf += xe->xdf2.nreff + 1;
349349
kvdb += xe->xdf2.nreff + 1;
350350

351-
xenv.mxcost = xdl_bogosqrt(ndiags);
351+
xenv.mxcost = (long)xdl_bogosqrt((uint64_t)ndiags);
352352
if (xenv.mxcost < XDL_MAX_COST_MIN)
353353
xenv.mxcost = XDL_MAX_COST_MIN;
354354
xenv.snake_cnt = XDL_SNAKE_CNT;

xdiff/xprepare.c

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ void xdl_free_env(xdfenv_t *xe) {
197197
}
198198

199199

200-
static bool xdl_clean_mmatch(uint8_t const *action, long i, long s, long e) {
201-
long r, rdis0, rpdis0, rdis1, rpdis1;
200+
static bool xdl_clean_mmatch(uint8_t const *action, ptrdiff_t i, ptrdiff_t s, ptrdiff_t e) {
201+
ptrdiff_t r, rdis0, rpdis0, rdis1, rpdis1;
202202

203203
/*
204204
* Limits the window that is examined during the similar-lines
@@ -268,8 +268,7 @@ static bool xdl_clean_mmatch(uint8_t const *action, long i, long s, long e) {
268268
* might be potentially discarded if they appear in a run of discardable.
269269
*/
270270
static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
271-
long i, nm, mlim;
272-
xrecord_t *recs;
271+
ptrdiff_t i, nm, mlim1, mlim2;
273272
xdlclass_t *rcrec;
274273
uint8_t *action1 = NULL, *action2 = NULL;
275274
bool need_min = !!(cf->flags & XDF_NEED_MINIMAL);
@@ -288,51 +287,80 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
288287
goto cleanup;
289288
}
290289

290+
if (need_min) {
291+
/* i.e. infinity */
292+
mlim1 = PTRDIFF_MAX;
293+
mlim2 = PTRDIFF_MAX;
294+
} else {
295+
mlim1 = XDL_MIN(xdl_bogosqrt(xdf1->nrec), XDL_MAX_EQLIMIT);
296+
mlim2 = XDL_MIN(xdl_bogosqrt(xdf2->nrec), XDL_MAX_EQLIMIT);
297+
}
298+
291299
/*
292300
* Initialize temporary arrays with DISCARD, KEEP, or INVESTIGATE.
293301
*/
294-
if ((mlim = xdl_bogosqrt((long)xdf1->nrec)) > XDL_MAX_EQLIMIT)
295-
mlim = XDL_MAX_EQLIMIT;
296-
for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
297-
rcrec = cf->rcrecs[recs->minimal_perfect_hash];
302+
for (i = xdf1->dstart; i <= xdf1->dend; i++) {
303+
size_t mph1 = xdf1->recs[i].minimal_perfect_hash;
304+
rcrec = cf->rcrecs[mph1];
298305
nm = rcrec ? rcrec->len2 : 0;
299-
action1[i] = (nm == 0) ? DISCARD: (nm >= mlim && !need_min) ? INVESTIGATE: KEEP;
306+
if (nm == 0)
307+
action1[i] = DISCARD;
308+
else if (nm < mlim1)
309+
action1[i] = KEEP;
310+
else /* nm >= mlim1 */
311+
action1[i] = INVESTIGATE;
300312
}
301313

302-
if ((mlim = xdl_bogosqrt((long)xdf2->nrec)) > XDL_MAX_EQLIMIT)
303-
mlim = XDL_MAX_EQLIMIT;
304-
for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
305-
rcrec = cf->rcrecs[recs->minimal_perfect_hash];
314+
for (i = xdf2->dstart; i <= xdf2->dend; i++) {
315+
size_t mph2 = xdf2->recs[i].minimal_perfect_hash;
316+
rcrec = cf->rcrecs[mph2];
306317
nm = rcrec ? rcrec->len1 : 0;
307-
action2[i] = (nm == 0) ? DISCARD: (nm >= mlim && !need_min) ? INVESTIGATE: KEEP;
318+
if (nm == 0)
319+
action2[i] = DISCARD;
320+
else if (nm < mlim2)
321+
action2[i] = KEEP;
322+
else /* nm >= mlim2 */
323+
action2[i] = INVESTIGATE;
308324
}
309325

310326
/*
311327
* Use temporary arrays to decide if changed[i] should remain
312328
* false, or become true.
313329
*/
314330
xdf1->nreff = 0;
315-
for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
316-
i <= xdf1->dend; i++, recs++) {
317-
if (action1[i] == KEEP ||
318-
(action1[i] == INVESTIGATE && !xdl_clean_mmatch(action1, i, xdf1->dstart, xdf1->dend))) {
331+
for (i = xdf1->dstart; i <= xdf1->dend; i++) {
332+
if (action1[i] == INVESTIGATE) {
333+
if (!xdl_clean_mmatch(action1, i, xdf1->dstart, xdf1->dend))
334+
action1[i] = KEEP;
335+
else
336+
action1[i] = DISCARD;
337+
}
338+
339+
if (action1[i] == KEEP) {
319340
xdf1->reference_index[xdf1->nreff++] = i;
320-
/* changed[i] remains false, i.e. keep */
321-
} else
341+
/* changed[i] remains false */
342+
} else if (action1[i] == DISCARD)
322343
xdf1->changed[i] = true;
323-
/* i.e. discard */
344+
else
345+
BUG("Illegal state for action1[i]");
324346
}
325347

326348
xdf2->nreff = 0;
327-
for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
328-
i <= xdf2->dend; i++, recs++) {
329-
if (action2[i] == KEEP ||
330-
(action2[i] == INVESTIGATE && !xdl_clean_mmatch(action2, i, xdf2->dstart, xdf2->dend))) {
349+
for (i = xdf2->dstart; i <= xdf2->dend; i++) {
350+
if (action2[i] == INVESTIGATE) {
351+
if (!xdl_clean_mmatch(action2, i, xdf2->dstart, xdf2->dend))
352+
action2[i] = KEEP;
353+
else
354+
action2[i] = DISCARD;
355+
}
356+
357+
if (action2[i] == KEEP) {
331358
xdf2->reference_index[xdf2->nreff++] = i;
332-
/* changed[i] remains false, i.e. keep */
333-
} else
359+
/* changed[i] remains false */
360+
} else if (action2[i] == DISCARD)
334361
xdf2->changed[i] = true;
335-
/* i.e. discard */
362+
else
363+
BUG("Illegal state for action2[i]");
336364
}
337365

338366
cleanup:

xdiff/xutils.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
#include "xinclude.h"
2424

2525

26-
long xdl_bogosqrt(long n) {
27-
long i;
26+
uint64_t xdl_bogosqrt(uint64_t n) {
27+
uint64_t i;
2828

2929
/*
3030
* Classical integer square root approximation using shifts.

xdiff/xutils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727

28-
long xdl_bogosqrt(long n);
28+
uint64_t xdl_bogosqrt(uint64_t n);
2929
int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize,
3030
xdemitcb_t *ecb);
3131
int xdl_cha_init(chastore_t *cha, long isize, long icount);

0 commit comments

Comments
 (0)