Skip to content

Commit c97ac79

Browse files
committed
Merge branch 'en/xdiff-cleanup-3' into pw/xdiff-shrink-memory-consumption
* en/xdiff-cleanup-3: xdiff/xdl_cleanup_records: put braces around the else clause 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 + 0ee3c64 commit c97ac79

4 files changed

Lines changed: 40 additions & 24 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: 36 additions & 20 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,68 @@ 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++) {
331+
for (i = xdf1->dstart; i <= xdf1->dend; i++) {
317332
if (action1[i] == KEEP ||
318333
(action1[i] == INVESTIGATE && !xdl_clean_mmatch(action1, i, xdf1->dstart, xdf1->dend))) {
319334
xdf1->reference_index[xdf1->nreff++] = i;
320335
/* changed[i] remains false, i.e. keep */
321-
} else
336+
} else {
322337
xdf1->changed[i] = true;
323338
/* i.e. discard */
339+
}
324340
}
325341

326342
xdf2->nreff = 0;
327-
for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
328-
i <= xdf2->dend; i++, recs++) {
343+
for (i = xdf2->dstart; i <= xdf2->dend; i++) {
329344
if (action2[i] == KEEP ||
330345
(action2[i] == INVESTIGATE && !xdl_clean_mmatch(action2, i, xdf2->dstart, xdf2->dend))) {
331346
xdf2->reference_index[xdf2->nreff++] = i;
332347
/* changed[i] remains false, i.e. keep */
333-
} else
348+
} else {
334349
xdf2->changed[i] = true;
335350
/* i.e. discard */
351+
}
336352
}
337353

338354
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)