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