]> git.ipfire.org Git - thirdparty/git.git/blame - xdiff/xprepare.c
xdiff: introduce XDL_ALLOC_ARRAY()
[thirdparty/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
48425792
TZ
16 * License along with this library; if not, see
17 * <http://www.gnu.org/licenses/>.
3443546f
LT
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
86abba80
TRC
29#define XDL_GUESS_NLINES1 256
30#define XDL_GUESS_NLINES2 20
3443546f
LT
31
32
33typedef struct s_xdlclass {
34 struct s_xdlclass *next;
35 unsigned long ha;
36 char const *line;
37 long size;
38 long idx;
27af01d5 39 long len1, len2;
3443546f
LT
40} xdlclass_t;
41
42typedef struct s_xdlclassifier {
43 unsigned int hbits;
44 long hsize;
45 xdlclass_t **rchash;
46 chastore_t ncha;
27af01d5
TRC
47 xdlclass_t **rcrecs;
48 long alloc;
3443546f 49 long count;
0d21efa5 50 long flags;
3443546f
LT
51} xdlclassifier_t;
52
53
54
55
0d21efa5 56static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags);
3443546f 57static void xdl_free_classifier(xdlclassifier_t *cf);
27af01d5
TRC
58static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
59 unsigned int hbits, xrecord_t *rec);
60static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
3443546f
LT
61 xdlclassifier_t *cf, xdfile_t *xdf);
62static void xdl_free_ctx(xdfile_t *xdf);
63static int xdl_clean_mmatch(char const *dis, long i, long s, long e);
27af01d5 64static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
3443546f 65static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2);
27af01d5 66static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
3443546f
LT
67
68
69
70
0d21efa5 71static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
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 }
452f4fa5 86 memset(cf->rchash, 0, cf->hsize * sizeof(xdlclass_t *));
3443546f 87
27af01d5 88 cf->alloc = size;
abf04bda 89 if (!XDL_ALLOC_ARRAY(cf->rcrecs, cf->alloc)) {
27af01d5
TRC
90
91 xdl_free(cf->rchash);
92 xdl_cha_free(&cf->ncha);
93 return -1;
94 }
95
3443546f
LT
96 cf->count = 0;
97
98 return 0;
99}
100
101
102static void xdl_free_classifier(xdlclassifier_t *cf) {
103
27af01d5 104 xdl_free(cf->rcrecs);
3443546f
LT
105 xdl_free(cf->rchash);
106 xdl_cha_free(&cf->ncha);
107}
108
109
27af01d5
TRC
110static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
111 unsigned int hbits, xrecord_t *rec) {
3443546f
LT
112 long hi;
113 char const *line;
114 xdlclass_t *rcrec;
27af01d5 115 xdlclass_t **rcrecs;
3443546f
LT
116
117 line = rec->ptr;
118 hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
119 for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
0d21efa5
JS
120 if (rcrec->ha == rec->ha &&
121 xdl_recmatch(rcrec->line, rcrec->size,
122 rec->ptr, rec->size, cf->flags))
3443546f
LT
123 break;
124
125 if (!rcrec) {
126 if (!(rcrec = xdl_cha_alloc(&cf->ncha))) {
127
128 return -1;
129 }
130 rcrec->idx = cf->count++;
27af01d5
TRC
131 if (cf->count > cf->alloc) {
132 cf->alloc *= 2;
133 if (!(rcrecs = (xdlclass_t **) xdl_realloc(cf->rcrecs, cf->alloc * sizeof(xdlclass_t *)))) {
134
135 return -1;
136 }
137 cf->rcrecs = rcrecs;
138 }
139 cf->rcrecs[rcrec->idx] = rcrec;
3443546f
LT
140 rcrec->line = line;
141 rcrec->size = rec->size;
142 rcrec->ha = rec->ha;
27af01d5 143 rcrec->len1 = rcrec->len2 = 0;
3443546f
LT
144 rcrec->next = cf->rchash[hi];
145 cf->rchash[hi] = rcrec;
146 }
147
27af01d5
TRC
148 (pass == 1) ? rcrec->len1++ : rcrec->len2++;
149
3443546f
LT
150 rec->ha = (unsigned long) rcrec->idx;
151
152 hi = (long) XDL_HASHLONG(rec->ha, hbits);
153 rec->next = rhash[hi];
154 rhash[hi] = rec;
155
156 return 0;
157}
158
159
27af01d5 160static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
3443546f
LT
161 xdlclassifier_t *cf, xdfile_t *xdf) {
162 unsigned int hbits;
452f4fa5 163 long nrec, hsize, bsize;
3443546f
LT
164 unsigned long hav;
165 char const *blk, *cur, *top, *prev;
166 xrecord_t *crec;
167 xrecord_t **recs, **rrecs;
168 xrecord_t **rhash;
169 unsigned long *ha;
170 char *rchg;
171 long *rindex;
172
159607a8
TRC
173 ha = NULL;
174 rindex = NULL;
175 rchg = NULL;
176 rhash = NULL;
177 recs = NULL;
3443546f 178
159607a8
TRC
179 if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0)
180 goto abort;
abf04bda 181 if (!XDL_ALLOC_ARRAY(recs, narec))
159607a8 182 goto abort;
3443546f 183
663c5ad0
PW
184 hbits = xdl_hashbits((unsigned int) narec);
185 hsize = 1 << hbits;
186 if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *))))
187 goto abort;
188 memset(rhash, 0, hsize * sizeof(xrecord_t *));
3443546f
LT
189
190 nrec = 0;
afe8a907 191 if ((cur = blk = xdl_mmfile_first(mf, &bsize))) {
739864b1 192 for (top = blk + bsize; cur < top; ) {
3443546f 193 prev = cur;
0d21efa5 194 hav = xdl_hash_record(&cur, top, xpp->flags);
3443546f
LT
195 if (nrec >= narec) {
196 narec *= 2;
159607a8
TRC
197 if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *))))
198 goto abort;
3443546f
LT
199 recs = rrecs;
200 }
159607a8
TRC
201 if (!(crec = xdl_cha_alloc(&xdf->rcha)))
202 goto abort;
3443546f
LT
203 crec->ptr = prev;
204 crec->size = (long) (cur - prev);
205 crec->ha = hav;
206 recs[nrec++] = crec;
663c5ad0 207 if (xdl_classify_record(pass, cf, rhash, hbits, crec) < 0)
159607a8 208 goto abort;
3443546f
LT
209 }
210 }
211
159607a8
TRC
212 if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char))))
213 goto abort;
3443546f
LT
214 memset(rchg, 0, (nrec + 2) * sizeof(char));
215
b82dd3f7
PW
216 if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
217 (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF)) {
abf04bda 218 if (!XDL_ALLOC_ARRAY(rindex, nrec + 1))
b82dd3f7 219 goto abort;
abf04bda 220 if (!XDL_ALLOC_ARRAY(ha, nrec + 1))
b82dd3f7
PW
221 goto abort;
222 }
3443546f
LT
223
224 xdf->nrec = nrec;
225 xdf->recs = recs;
226 xdf->hbits = hbits;
227 xdf->rhash = rhash;
228 xdf->rchg = rchg + 1;
229 xdf->rindex = rindex;
230 xdf->nreff = 0;
231 xdf->ha = ha;
232 xdf->dstart = 0;
233 xdf->dend = nrec - 1;
234
235 return 0;
159607a8
TRC
236
237abort:
238 xdl_free(ha);
239 xdl_free(rindex);
240 xdl_free(rchg);
241 xdl_free(rhash);
242 xdl_free(recs);
243 xdl_cha_free(&xdf->rcha);
244 return -1;
3443546f
LT
245}
246
247
248static void xdl_free_ctx(xdfile_t *xdf) {
249
250 xdl_free(xdf->rhash);
251 xdl_free(xdf->rindex);
252 xdl_free(xdf->rchg - 1);
253 xdl_free(xdf->ha);
254 xdl_free(xdf->recs);
255 xdl_cha_free(&xdf->rcha);
256}
257
258
259int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
260 xdfenv_t *xe) {
86abba80 261 long enl1, enl2, sample;
3443546f
LT
262 xdlclassifier_t cf;
263
2738bc3f
TRC
264 memset(&cf, 0, sizeof(cf));
265
86abba80
TRC
266 /*
267 * For histogram diff, we can afford a smaller sample size and
268 * thus a poorer estimate of the number of lines, as the hash
269 * table (rhash) won't be filled up/grown. The number of lines
270 * (nrecs) will be updated correctly anyway by
271 * xdl_prepare_ctx().
272 */
307ab20b
JH
273 sample = (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF
274 ? XDL_GUESS_NLINES2 : XDL_GUESS_NLINES1);
86abba80
TRC
275
276 enl1 = xdl_guess_lines(mf1, sample) + 1;
277 enl2 = xdl_guess_lines(mf2, sample) + 1;
3443546f 278
663c5ad0 279 if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0)
3443546f 280 return -1;
3443546f 281
27af01d5 282 if (xdl_prepare_ctx(1, mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
3443546f
LT
283
284 xdl_free_classifier(&cf);
285 return -1;
286 }
27af01d5 287 if (xdl_prepare_ctx(2, mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
3443546f
LT
288
289 xdl_free_ctx(&xe->xdf1);
290 xdl_free_classifier(&cf);
291 return -1;
292 }
293
307ab20b
JH
294 if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
295 (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF) &&
296 xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) {
3443546f
LT
297
298 xdl_free_ctx(&xe->xdf2);
299 xdl_free_ctx(&xe->xdf1);
87f16258 300 xdl_free_classifier(&cf);
3443546f
LT
301 return -1;
302 }
303
663c5ad0 304 xdl_free_classifier(&cf);
27af01d5 305
3443546f
LT
306 return 0;
307}
308
309
310void xdl_free_env(xdfenv_t *xe) {
311
312 xdl_free_ctx(&xe->xdf2);
313 xdl_free_ctx(&xe->xdf1);
314}
315
316
317static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
ca557aff
DL
318 long r, rdis0, rpdis0, rdis1, rpdis1;
319
9b28d554
DL
320 /*
321 * Limits the window the is examined during the similar-lines
322 * scan. The loops below stops when dis[i - r] == 1 (line that
323 * has no match), but there are corner cases where the loop
324 * proceed all the way to the extremities by causing huge
325 * performance penalties in case of big files.
326 */
327 if (i - s > XDL_SIMSCAN_WINDOW)
328 s = i - XDL_SIMSCAN_WINDOW;
329 if (e - i > XDL_SIMSCAN_WINDOW)
330 e = i + XDL_SIMSCAN_WINDOW;
331
ca557aff
DL
332 /*
333 * Scans the lines before 'i' to find a run of lines that either
334 * have no match (dis[j] == 0) or have multiple matches (dis[j] > 1).
335 * Note that we always call this function with dis[i] > 1, so the
336 * current line (i) is already a multimatch line.
337 */
338 for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) {
3443546f 339 if (!dis[i - r])
ca557aff 340 rdis0++;
3443546f 341 else if (dis[i - r] == 2)
ca557aff 342 rpdis0++;
3443546f
LT
343 else
344 break;
345 }
ca557aff
DL
346 /*
347 * If the run before the line 'i' found only multimatch lines, we
348 * return 0 and hence we don't make the current line (i) discarded.
349 * We want to discard multimatch lines only when they appear in the
350 * middle of runs with nomatch lines (dis[j] == 0).
351 */
352 if (rdis0 == 0)
353 return 0;
354 for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) {
3443546f 355 if (!dis[i + r])
ca557aff 356 rdis1++;
3443546f 357 else if (dis[i + r] == 2)
ca557aff 358 rpdis1++;
3443546f
LT
359 else
360 break;
361 }
ca557aff
DL
362 /*
363 * If the run after the line 'i' found only multimatch lines, we
364 * return 0 and hence we don't make the current line (i) discarded.
365 */
366 if (rdis1 == 0)
367 return 0;
368 rdis1 += rdis0;
369 rpdis1 += rpdis0;
370
371 return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
3443546f
LT
372}
373
374
375/*
376 * Try to reduce the problem complexity, discard records that have no
377 * matches on the other file. Also, lines that have multiple matches
378 * might be potentially discarded if they happear in a run of discardable.
379 */
27af01d5 380static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
c5aa9068 381 long i, nm, nreff, mlim;
3443546f 382 xrecord_t **recs;
27af01d5 383 xdlclass_t *rcrec;
3443546f
LT
384 char *dis, *dis1, *dis2;
385
ca557aff 386 if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) {
3443546f
LT
387
388 return -1;
389 }
ca557aff 390 memset(dis, 0, xdf1->nrec + xdf2->nrec + 2);
3443546f
LT
391 dis1 = dis;
392 dis2 = dis1 + xdf1->nrec + 1;
393
c5aa9068
RS
394 if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT)
395 mlim = XDL_MAX_EQLIMIT;
3443546f 396 for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
27af01d5
TRC
397 rcrec = cf->rcrecs[(*recs)->ha];
398 nm = rcrec ? rcrec->len2 : 0;
c5aa9068 399 dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
3443546f
LT
400 }
401
c5aa9068
RS
402 if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT)
403 mlim = XDL_MAX_EQLIMIT;
3443546f 404 for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
27af01d5
TRC
405 rcrec = cf->rcrecs[(*recs)->ha];
406 nm = rcrec ? rcrec->len1 : 0;
c5aa9068 407 dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
3443546f
LT
408 }
409
410 for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
411 i <= xdf1->dend; i++, recs++) {
412 if (dis1[i] == 1 ||
413 (dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) {
414 xdf1->rindex[nreff] = i;
415 xdf1->ha[nreff] = (*recs)->ha;
416 nreff++;
417 } else
418 xdf1->rchg[i] = 1;
419 }
420 xdf1->nreff = nreff;
421
422 for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
423 i <= xdf2->dend; i++, recs++) {
424 if (dis2[i] == 1 ||
425 (dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) {
426 xdf2->rindex[nreff] = i;
427 xdf2->ha[nreff] = (*recs)->ha;
428 nreff++;
429 } else
430 xdf2->rchg[i] = 1;
431 }
432 xdf2->nreff = nreff;
433
434 xdl_free(dis);
435
436 return 0;
437}
438
439
440/*
441 * Early trim initial and terminal matching records.
442 */
443static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
444 long i, lim;
445 xrecord_t **recs1, **recs2;
446
447 recs1 = xdf1->recs;
448 recs2 = xdf2->recs;
449 for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
450 i++, recs1++, recs2++)
451 if ((*recs1)->ha != (*recs2)->ha)
452 break;
453
454 xdf1->dstart = xdf2->dstart = i;
455
456 recs1 = xdf1->recs + xdf1->nrec - 1;
457 recs2 = xdf2->recs + xdf2->nrec - 1;
458 for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)
459 if ((*recs1)->ha != (*recs2)->ha)
460 break;
461
462 xdf1->dend = xdf1->nrec - i - 1;
463 xdf2->dend = xdf2->nrec - i - 1;
464
465 return 0;
466}
467
468
27af01d5 469static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
3443546f
LT
470
471 if (xdl_trim_ends(xdf1, xdf2) < 0 ||
27af01d5 472 xdl_cleanup_records(cf, xdf1, xdf2) < 0) {
3443546f
LT
473
474 return -1;
475 }
476
477 return 0;
478}