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