Revert removal of multi-match discard heuristic in 27af01
[git/mjg.git] / xdiff / xprepare.c
blob4c447ca6d2aa26f4a88690a2cf97376a83d1ffad
1 /*
2 * LibXDiff by Davide Libenzi ( File Differential Library )
3 * Copyright (C) 2003 Davide Libenzi
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Davide Libenzi <davidel@xmailserver.org>
23 #include "xinclude.h"
26 #define XDL_KPDIS_RUN 4
27 #define XDL_MAX_EQLIMIT 1024
28 #define XDL_SIMSCAN_WINDOW 100
31 typedef struct s_xdlclass {
32 struct s_xdlclass *next;
33 unsigned long ha;
34 char const *line;
35 long size;
36 long idx;
37 long len1, len2;
38 } xdlclass_t;
40 typedef struct s_xdlclassifier {
41 unsigned int hbits;
42 long hsize;
43 xdlclass_t **rchash;
44 chastore_t ncha;
45 xdlclass_t **rcrecs;
46 long alloc;
47 long count;
48 long flags;
49 } xdlclassifier_t;
54 static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags);
55 static void xdl_free_classifier(xdlclassifier_t *cf);
56 static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
57 unsigned int hbits, xrecord_t *rec);
58 static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
59 xdlclassifier_t *cf, xdfile_t *xdf);
60 static void xdl_free_ctx(xdfile_t *xdf);
61 static int xdl_clean_mmatch(char const *dis, long i, long s, long e);
62 static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
63 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2);
64 static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
69 static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
70 long i;
72 cf->flags = flags;
74 cf->hbits = xdl_hashbits((unsigned int) size);
75 cf->hsize = 1 << cf->hbits;
77 if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) {
79 return -1;
81 if (!(cf->rchash = (xdlclass_t **) xdl_malloc(cf->hsize * sizeof(xdlclass_t *)))) {
83 xdl_cha_free(&cf->ncha);
84 return -1;
86 for (i = 0; i < cf->hsize; i++)
87 cf->rchash[i] = NULL;
89 cf->alloc = size;
90 if (!(cf->rcrecs = (xdlclass_t **) xdl_malloc(cf->alloc * sizeof(xdlclass_t *)))) {
92 xdl_free(cf->rchash);
93 xdl_cha_free(&cf->ncha);
94 return -1;
97 cf->count = 0;
99 return 0;
103 static void xdl_free_classifier(xdlclassifier_t *cf) {
105 xdl_free(cf->rcrecs);
106 xdl_free(cf->rchash);
107 xdl_cha_free(&cf->ncha);
111 static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
112 unsigned int hbits, xrecord_t *rec) {
113 long hi;
114 char const *line;
115 xdlclass_t *rcrec;
116 xdlclass_t **rcrecs;
118 line = rec->ptr;
119 hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
120 for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
121 if (rcrec->ha == rec->ha &&
122 xdl_recmatch(rcrec->line, rcrec->size,
123 rec->ptr, rec->size, cf->flags))
124 break;
126 if (!rcrec) {
127 if (!(rcrec = xdl_cha_alloc(&cf->ncha))) {
129 return -1;
131 rcrec->idx = cf->count++;
132 if (cf->count > cf->alloc) {
133 cf->alloc *= 2;
134 if (!(rcrecs = (xdlclass_t **) xdl_realloc(cf->rcrecs, cf->alloc * sizeof(xdlclass_t *)))) {
136 return -1;
138 cf->rcrecs = rcrecs;
140 cf->rcrecs[rcrec->idx] = rcrec;
141 rcrec->line = line;
142 rcrec->size = rec->size;
143 rcrec->ha = rec->ha;
144 rcrec->len1 = rcrec->len2 = 0;
145 rcrec->next = cf->rchash[hi];
146 cf->rchash[hi] = rcrec;
149 (pass == 1) ? rcrec->len1++ : rcrec->len2++;
151 rec->ha = (unsigned long) rcrec->idx;
153 hi = (long) XDL_HASHLONG(rec->ha, hbits);
154 rec->next = rhash[hi];
155 rhash[hi] = rec;
157 return 0;
161 static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
162 xdlclassifier_t *cf, xdfile_t *xdf) {
163 unsigned int hbits;
164 long i, nrec, hsize, bsize;
165 unsigned long hav;
166 char const *blk, *cur, *top, *prev;
167 xrecord_t *crec;
168 xrecord_t **recs, **rrecs;
169 xrecord_t **rhash;
170 unsigned long *ha;
171 char *rchg;
172 long *rindex;
174 if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) {
176 return -1;
178 if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) {
180 xdl_cha_free(&xdf->rcha);
181 return -1;
184 hbits = xdl_hashbits((unsigned int) narec);
185 hsize = 1 << hbits;
186 if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) {
188 xdl_free(recs);
189 xdl_cha_free(&xdf->rcha);
190 return -1;
192 for (i = 0; i < hsize; i++)
193 rhash[i] = NULL;
195 nrec = 0;
196 if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) {
197 for (top = blk + bsize;;) {
198 if (cur >= top) {
199 if (!(cur = blk = xdl_mmfile_next(mf, &bsize)))
200 break;
201 top = blk + bsize;
203 prev = cur;
204 hav = xdl_hash_record(&cur, top, xpp->flags);
205 if (nrec >= narec) {
206 narec *= 2;
207 if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) {
209 xdl_free(rhash);
210 xdl_free(recs);
211 xdl_cha_free(&xdf->rcha);
212 return -1;
214 recs = rrecs;
216 if (!(crec = xdl_cha_alloc(&xdf->rcha))) {
218 xdl_free(rhash);
219 xdl_free(recs);
220 xdl_cha_free(&xdf->rcha);
221 return -1;
223 crec->ptr = prev;
224 crec->size = (long) (cur - prev);
225 crec->ha = hav;
226 recs[nrec++] = crec;
228 if (xdl_classify_record(pass, cf, rhash, hbits, crec) < 0) {
230 xdl_free(rhash);
231 xdl_free(recs);
232 xdl_cha_free(&xdf->rcha);
233 return -1;
238 if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) {
240 xdl_free(rhash);
241 xdl_free(recs);
242 xdl_cha_free(&xdf->rcha);
243 return -1;
245 memset(rchg, 0, (nrec + 2) * sizeof(char));
247 if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) {
249 xdl_free(rchg);
250 xdl_free(rhash);
251 xdl_free(recs);
252 xdl_cha_free(&xdf->rcha);
253 return -1;
255 if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) {
257 xdl_free(rindex);
258 xdl_free(rchg);
259 xdl_free(rhash);
260 xdl_free(recs);
261 xdl_cha_free(&xdf->rcha);
262 return -1;
265 xdf->nrec = nrec;
266 xdf->recs = recs;
267 xdf->hbits = hbits;
268 xdf->rhash = rhash;
269 xdf->rchg = rchg + 1;
270 xdf->rindex = rindex;
271 xdf->nreff = 0;
272 xdf->ha = ha;
273 xdf->dstart = 0;
274 xdf->dend = nrec - 1;
276 return 0;
280 static void xdl_free_ctx(xdfile_t *xdf) {
282 xdl_free(xdf->rhash);
283 xdl_free(xdf->rindex);
284 xdl_free(xdf->rchg - 1);
285 xdl_free(xdf->ha);
286 xdl_free(xdf->recs);
287 xdl_cha_free(&xdf->rcha);
291 int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
292 xdfenv_t *xe) {
293 long enl1, enl2;
294 xdlclassifier_t cf;
296 enl1 = xdl_guess_lines(mf1) + 1;
297 enl2 = xdl_guess_lines(mf2) + 1;
299 if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0) {
301 return -1;
304 if (xdl_prepare_ctx(1, mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
306 xdl_free_classifier(&cf);
307 return -1;
309 if (xdl_prepare_ctx(2, mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
311 xdl_free_ctx(&xe->xdf1);
312 xdl_free_classifier(&cf);
313 return -1;
316 if (!(xpp->flags & XDF_PATIENCE_DIFF) &&
317 xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) {
319 xdl_free_ctx(&xe->xdf2);
320 xdl_free_ctx(&xe->xdf1);
321 return -1;
324 xdl_free_classifier(&cf);
326 return 0;
330 void xdl_free_env(xdfenv_t *xe) {
332 xdl_free_ctx(&xe->xdf2);
333 xdl_free_ctx(&xe->xdf1);
337 static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
338 long r, rdis0, rpdis0, rdis1, rpdis1;
341 * Limits the window the is examined during the similar-lines
342 * scan. The loops below stops when dis[i - r] == 1 (line that
343 * has no match), but there are corner cases where the loop
344 * proceed all the way to the extremities by causing huge
345 * performance penalties in case of big files.
347 if (i - s > XDL_SIMSCAN_WINDOW)
348 s = i - XDL_SIMSCAN_WINDOW;
349 if (e - i > XDL_SIMSCAN_WINDOW)
350 e = i + XDL_SIMSCAN_WINDOW;
353 * Scans the lines before 'i' to find a run of lines that either
354 * have no match (dis[j] == 0) or have multiple matches (dis[j] > 1).
355 * Note that we always call this function with dis[i] > 1, so the
356 * current line (i) is already a multimatch line.
358 for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) {
359 if (!dis[i - r])
360 rdis0++;
361 else if (dis[i - r] == 2)
362 rpdis0++;
363 else
364 break;
367 * If the run before the line 'i' found only multimatch lines, we
368 * return 0 and hence we don't make the current line (i) discarded.
369 * We want to discard multimatch lines only when they appear in the
370 * middle of runs with nomatch lines (dis[j] == 0).
372 if (rdis0 == 0)
373 return 0;
374 for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) {
375 if (!dis[i + r])
376 rdis1++;
377 else if (dis[i + r] == 2)
378 rpdis1++;
379 else
380 break;
383 * If the run after the line 'i' found only multimatch lines, we
384 * return 0 and hence we don't make the current line (i) discarded.
386 if (rdis1 == 0)
387 return 0;
388 rdis1 += rdis0;
389 rpdis1 += rpdis0;
391 return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
396 * Try to reduce the problem complexity, discard records that have no
397 * matches on the other file. Also, lines that have multiple matches
398 * might be potentially discarded if they happear in a run of discardable.
400 static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
401 long i, nm, nreff, mlim;
402 xrecord_t **recs;
403 xdlclass_t *rcrec;
404 char *dis, *dis1, *dis2;
406 if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) {
408 return -1;
410 memset(dis, 0, xdf1->nrec + xdf2->nrec + 2);
411 dis1 = dis;
412 dis2 = dis1 + xdf1->nrec + 1;
414 if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT)
415 mlim = XDL_MAX_EQLIMIT;
416 for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
417 rcrec = cf->rcrecs[(*recs)->ha];
418 nm = rcrec ? rcrec->len2 : 0;
419 dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
422 if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT)
423 mlim = XDL_MAX_EQLIMIT;
424 for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
425 rcrec = cf->rcrecs[(*recs)->ha];
426 nm = rcrec ? rcrec->len1 : 0;
427 dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
430 for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
431 i <= xdf1->dend; i++, recs++) {
432 if (dis1[i] == 1 ||
433 (dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) {
434 xdf1->rindex[nreff] = i;
435 xdf1->ha[nreff] = (*recs)->ha;
436 nreff++;
437 } else
438 xdf1->rchg[i] = 1;
440 xdf1->nreff = nreff;
442 for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
443 i <= xdf2->dend; i++, recs++) {
444 if (dis2[i] == 1 ||
445 (dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) {
446 xdf2->rindex[nreff] = i;
447 xdf2->ha[nreff] = (*recs)->ha;
448 nreff++;
449 } else
450 xdf2->rchg[i] = 1;
452 xdf2->nreff = nreff;
454 xdl_free(dis);
456 return 0;
461 * Early trim initial and terminal matching records.
463 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
464 long i, lim;
465 xrecord_t **recs1, **recs2;
467 recs1 = xdf1->recs;
468 recs2 = xdf2->recs;
469 for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
470 i++, recs1++, recs2++)
471 if ((*recs1)->ha != (*recs2)->ha)
472 break;
474 xdf1->dstart = xdf2->dstart = i;
476 recs1 = xdf1->recs + xdf1->nrec - 1;
477 recs2 = xdf2->recs + xdf2->nrec - 1;
478 for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)
479 if ((*recs1)->ha != (*recs2)->ha)
480 break;
482 xdf1->dend = xdf1->nrec - i - 1;
483 xdf2->dend = xdf2->nrec - i - 1;
485 return 0;
489 static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
491 if (xdl_trim_ends(xdf1, xdf2) < 0 ||
492 xdl_cleanup_records(cf, xdf1, xdf2) < 0) {
494 return -1;
497 return 0;