xdiff/xprepare: improve O(n*m) performance in xdl_cleanup_records()
[git/git.git] / xdiff / xprepare.c
CommitLineData
3443546f
LT
1/*
2 * LibXDiff by Davide Libenzi ( File Differential Library )
3 * Copyright (C) 2003 Davide Libenzi
4 *
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.
9 *
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.
14 *
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
18 *
19 * Davide Libenzi <davidel@xmailserver.org>
20 *
21 */
22
23#include "xinclude.h"
24
25
3443546f 26#define XDL_KPDIS_RUN 4
ca557aff 27#define XDL_MAX_EQLIMIT 1024
9b28d554 28#define XDL_SIMSCAN_WINDOW 100
3443546f
LT
29
30
31typedef struct s_xdlclass {
32 struct s_xdlclass *next;
33 unsigned long ha;
34 char const *line;
35 long size;
36 long idx;
27af01d5 37 long len1, len2;
3443546f
LT
38} xdlclass_t;
39
40typedef struct s_xdlclassifier {
41 unsigned int hbits;
42 long hsize;
43 xdlclass_t **rchash;
44 chastore_t ncha;
27af01d5
TRC
45 xdlclass_t **rcrecs;
46 long alloc;
3443546f 47 long count;
0d21efa5 48 long flags;
3443546f
LT
49} xdlclassifier_t;
50
51
52
53
0d21efa5 54static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags);
3443546f 55static void xdl_free_classifier(xdlclassifier_t *cf);
27af01d5
TRC
56static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
57 unsigned int hbits, xrecord_t *rec);
58static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
3443546f
LT
59 xdlclassifier_t *cf, xdfile_t *xdf);
60static void xdl_free_ctx(xdfile_t *xdf);
61static int xdl_clean_mmatch(char const *dis, long i, long s, long e);
27af01d5 62static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
3443546f 63static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2);
27af01d5 64static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
3443546f
LT
65
66
67
68
0d21efa5 69static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
3443546f
LT
70 long i;
71
0d21efa5
JS
72 cf->flags = flags;
73
3443546f
LT
74 cf->hbits = xdl_hashbits((unsigned int) size);
75 cf->hsize = 1 << cf->hbits;
76
77 if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) {
78
79 return -1;
80 }
81 if (!(cf->rchash = (xdlclass_t **) xdl_malloc(cf->hsize * sizeof(xdlclass_t *)))) {
82
83 xdl_cha_free(&cf->ncha);
84 return -1;
85 }
86 for (i = 0; i < cf->hsize; i++)
87 cf->rchash[i] = NULL;
88
27af01d5
TRC
89 cf->alloc = size;
90 if (!(cf->rcrecs = (xdlclass_t **) xdl_malloc(cf->alloc * sizeof(xdlclass_t *)))) {
91
92 xdl_free(cf->rchash);
93 xdl_cha_free(&cf->ncha);
94 return -1;
95 }
96
3443546f
LT
97 cf->count = 0;
98
99 return 0;
100}
101
102
103static void xdl_free_classifier(xdlclassifier_t *cf) {
104
27af01d5 105 xdl_free(cf->rcrecs);
3443546f
LT
106 xdl_free(cf->rchash);
107 xdl_cha_free(&cf->ncha);
108}
109
110
27af01d5
TRC
111static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
112 unsigned int hbits, xrecord_t *rec) {
3443546f
LT
113 long hi;
114 char const *line;
115 xdlclass_t *rcrec;
27af01d5 116 xdlclass_t **rcrecs;
3443546f
LT
117
118 line = rec->ptr;
119 hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
120 for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
0d21efa5
JS
121 if (rcrec->ha == rec->ha &&
122 xdl_recmatch(rcrec->line, rcrec->size,
123 rec->ptr, rec->size, cf->flags))
3443546f
LT
124 break;
125
126 if (!rcrec) {
127 if (!(rcrec = xdl_cha_alloc(&cf->ncha))) {
128
129 return -1;
130 }
131 rcrec->idx = cf->count++;
27af01d5
TRC
132 if (cf->count > cf->alloc) {
133 cf->alloc *= 2;
134 if (!(rcrecs = (xdlclass_t **) xdl_realloc(cf->rcrecs, cf->alloc * sizeof(xdlclass_t *)))) {
135
136 return -1;
137 }
138 cf->rcrecs = rcrecs;
139 }
140 cf->rcrecs[rcrec->idx] = rcrec;
3443546f
LT
141 rcrec->line = line;
142 rcrec->size = rec->size;
143 rcrec->ha = rec->ha;
27af01d5 144 rcrec->len1 = rcrec->len2 = 0;
3443546f
LT
145 rcrec->next = cf->rchash[hi];
146 cf->rchash[hi] = rcrec;
147 }
148
27af01d5
TRC
149 (pass == 1) ? rcrec->len1++ : rcrec->len2++;
150
3443546f
LT
151 rec->ha = (unsigned long) rcrec->idx;
152
153 hi = (long) XDL_HASHLONG(rec->ha, hbits);
154 rec->next = rhash[hi];
155 rhash[hi] = rec;
156
157 return 0;
158}
159
160
27af01d5 161static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
3443546f
LT
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;
173
174 if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) {
175
176 return -1;
177 }
178 if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) {
179
180 xdl_cha_free(&xdf->rcha);
181 return -1;
182 }
183
184 hbits = xdl_hashbits((unsigned int) narec);
185 hsize = 1 << hbits;
186 if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) {
187
188 xdl_free(recs);
189 xdl_cha_free(&xdf->rcha);
190 return -1;
191 }
192 for (i = 0; i < hsize; i++)
193 rhash[i] = NULL;
194
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;
202 }
203 prev = cur;
0d21efa5 204 hav = xdl_hash_record(&cur, top, xpp->flags);
3443546f
LT
205 if (nrec >= narec) {
206 narec *= 2;
207 if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) {
208
209 xdl_free(rhash);
210 xdl_free(recs);
211 xdl_cha_free(&xdf->rcha);
212 return -1;
213 }
214 recs = rrecs;
215 }
216 if (!(crec = xdl_cha_alloc(&xdf->rcha))) {
217
218 xdl_free(rhash);
219 xdl_free(recs);
220 xdl_cha_free(&xdf->rcha);
221 return -1;
222 }
223 crec->ptr = prev;
224 crec->size = (long) (cur - prev);
225 crec->ha = hav;
226 recs[nrec++] = crec;
227
27af01d5 228 if (xdl_classify_record(pass, cf, rhash, hbits, crec) < 0) {
3443546f
LT
229
230 xdl_free(rhash);
231 xdl_free(recs);
232 xdl_cha_free(&xdf->rcha);
233 return -1;
234 }
235 }
236 }
237
238 if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) {
239
240 xdl_free(rhash);
241 xdl_free(recs);
242 xdl_cha_free(&xdf->rcha);
243 return -1;
244 }
245 memset(rchg, 0, (nrec + 2) * sizeof(char));
246
247 if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) {
248
249 xdl_free(rchg);
250 xdl_free(rhash);
251 xdl_free(recs);
252 xdl_cha_free(&xdf->rcha);
253 return -1;
254 }
255 if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) {
256
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;
263 }
264
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;
275
276 return 0;
277}
278
279
280static void xdl_free_ctx(xdfile_t *xdf) {
281
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);
288}
289
290
291int 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;
295
296 enl1 = xdl_guess_lines(mf1) + 1;
297 enl2 = xdl_guess_lines(mf2) + 1;
298
0d21efa5 299 if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0) {
3443546f
LT
300
301 return -1;
302 }
303
27af01d5 304 if (xdl_prepare_ctx(1, mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
3443546f
LT
305
306 xdl_free_classifier(&cf);
307 return -1;
308 }
27af01d5 309 if (xdl_prepare_ctx(2, mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
3443546f
LT
310
311 xdl_free_ctx(&xe->xdf1);
312 xdl_free_classifier(&cf);
313 return -1;
314 }
315
92b7de93 316 if (!(xpp->flags & XDF_PATIENCE_DIFF) &&
27af01d5 317 xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) {
3443546f
LT
318
319 xdl_free_ctx(&xe->xdf2);
320 xdl_free_ctx(&xe->xdf1);
321 return -1;
322 }
323
27af01d5
TRC
324 xdl_free_classifier(&cf);
325
3443546f
LT
326 return 0;
327}
328
329
330void xdl_free_env(xdfenv_t *xe) {
331
332 xdl_free_ctx(&xe->xdf2);
333 xdl_free_ctx(&xe->xdf1);
334}
335
336
337static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
ca557aff
DL
338 long r, rdis0, rpdis0, rdis1, rpdis1;
339
9b28d554
DL
340 /*
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.
346 */
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;
351
ca557aff
DL
352 /*
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.
357 */
358 for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) {
3443546f 359 if (!dis[i - r])
ca557aff 360 rdis0++;
3443546f 361 else if (dis[i - r] == 2)
ca557aff 362 rpdis0++;
3443546f
LT
363 else
364 break;
365 }
ca557aff
DL
366 /*
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).
371 */
372 if (rdis0 == 0)
373 return 0;
374 for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) {
3443546f 375 if (!dis[i + r])
ca557aff 376 rdis1++;
3443546f 377 else if (dis[i + r] == 2)
ca557aff 378 rpdis1++;
3443546f
LT
379 else
380 break;
381 }
ca557aff
DL
382 /*
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.
385 */
386 if (rdis1 == 0)
387 return 0;
388 rdis1 += rdis0;
389 rpdis1 += rpdis0;
390
391 return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
3443546f
LT
392}
393
394
395/*
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.
399 */
27af01d5
TRC
400static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
401 long i, nm, nreff;
3443546f 402 xrecord_t **recs;
27af01d5 403 xdlclass_t *rcrec;
3443546f
LT
404 char *dis, *dis1, *dis2;
405
ca557aff 406 if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) {
3443546f
LT
407
408 return -1;
409 }
ca557aff 410 memset(dis, 0, xdf1->nrec + xdf2->nrec + 2);
3443546f
LT
411 dis1 = dis;
412 dis2 = dis1 + xdf1->nrec + 1;
413
414 for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
27af01d5
TRC
415 rcrec = cf->rcrecs[(*recs)->ha];
416 nm = rcrec ? rcrec->len2 : 0;
417 dis1[i] = (nm == 0) ? 0: 1;
3443546f
LT
418 }
419
420 for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
27af01d5
TRC
421 rcrec = cf->rcrecs[(*recs)->ha];
422 nm = rcrec ? rcrec->len1 : 0;
423 dis2[i] = (nm == 0) ? 0: 1;
3443546f
LT
424 }
425
426 for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
427 i <= xdf1->dend; i++, recs++) {
428 if (dis1[i] == 1 ||
429 (dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) {
430 xdf1->rindex[nreff] = i;
431 xdf1->ha[nreff] = (*recs)->ha;
432 nreff++;
433 } else
434 xdf1->rchg[i] = 1;
435 }
436 xdf1->nreff = nreff;
437
438 for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
439 i <= xdf2->dend; i++, recs++) {
440 if (dis2[i] == 1 ||
441 (dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) {
442 xdf2->rindex[nreff] = i;
443 xdf2->ha[nreff] = (*recs)->ha;
444 nreff++;
445 } else
446 xdf2->rchg[i] = 1;
447 }
448 xdf2->nreff = nreff;
449
450 xdl_free(dis);
451
452 return 0;
453}
454
455
456/*
457 * Early trim initial and terminal matching records.
458 */
459static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
460 long i, lim;
461 xrecord_t **recs1, **recs2;
462
463 recs1 = xdf1->recs;
464 recs2 = xdf2->recs;
465 for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
466 i++, recs1++, recs2++)
467 if ((*recs1)->ha != (*recs2)->ha)
468 break;
469
470 xdf1->dstart = xdf2->dstart = i;
471
472 recs1 = xdf1->recs + xdf1->nrec - 1;
473 recs2 = xdf2->recs + xdf2->nrec - 1;
474 for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)
475 if ((*recs1)->ha != (*recs2)->ha)
476 break;
477
478 xdf1->dend = xdf1->nrec - i - 1;
479 xdf2->dend = xdf2->nrec - i - 1;
480
481 return 0;
482}
483
484
27af01d5 485static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
3443546f
LT
486
487 if (xdl_trim_ends(xdf1, xdf2) < 0 ||
27af01d5 488 xdl_cleanup_records(cf, xdf1, xdf2) < 0) {
3443546f
LT
489
490 return -1;
491 }
492
493 return 0;
494}