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