##// END OF EJS Templates
bdiff: extend matches across popular lines...
Matt Mackall -
r29322:66dbdd3c stable
parent child Browse files
Show More
@@ -1,483 +1,490 b''
1 1 /*
2 2 bdiff.c - efficient binary diff extension for Mercurial
3 3
4 4 Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
5 5
6 6 This software may be used and distributed according to the terms of
7 7 the GNU General Public License, incorporated herein by reference.
8 8
9 9 Based roughly on Python difflib
10 10 */
11 11
12 12 #define PY_SSIZE_T_CLEAN
13 13 #include <Python.h>
14 14 #include <stdlib.h>
15 15 #include <string.h>
16 16 #include <limits.h>
17 17
18 18 #include "util.h"
19 19
20 20 struct line {
21 21 int hash, n, e;
22 22 Py_ssize_t len;
23 23 const char *l;
24 24 };
25 25
26 26 struct pos {
27 27 int pos, len;
28 28 };
29 29
30 30 struct hunk;
31 31 struct hunk {
32 32 int a1, a2, b1, b2;
33 33 struct hunk *next;
34 34 };
35 35
36 36 static int splitlines(const char *a, Py_ssize_t len, struct line **lr)
37 37 {
38 38 unsigned hash;
39 39 int i;
40 40 const char *p, *b = a;
41 41 const char * const plast = a + len - 1;
42 42 struct line *l;
43 43
44 44 /* count the lines */
45 45 i = 1; /* extra line for sentinel */
46 46 for (p = a; p < a + len; p++)
47 47 if (*p == '\n' || p == plast)
48 48 i++;
49 49
50 50 *lr = l = (struct line *)malloc(sizeof(struct line) * i);
51 51 if (!l)
52 52 return -1;
53 53
54 54 /* build the line array and calculate hashes */
55 55 hash = 0;
56 56 for (p = a; p < a + len; p++) {
57 57 /* Leonid Yuriev's hash */
58 58 hash = (hash * 1664525) + (unsigned char)*p + 1013904223;
59 59
60 60 if (*p == '\n' || p == plast) {
61 61 l->hash = hash;
62 62 hash = 0;
63 63 l->len = p - b + 1;
64 64 l->l = b;
65 65 l->n = INT_MAX;
66 66 l++;
67 67 b = p + 1;
68 68 }
69 69 }
70 70
71 71 /* set up a sentinel */
72 72 l->hash = 0;
73 73 l->len = 0;
74 74 l->l = a + len;
75 75 return i - 1;
76 76 }
77 77
78 78 static inline int cmp(struct line *a, struct line *b)
79 79 {
80 80 return a->hash != b->hash || a->len != b->len || memcmp(a->l, b->l, a->len);
81 81 }
82 82
83 83 static int equatelines(struct line *a, int an, struct line *b, int bn)
84 84 {
85 85 int i, j, buckets = 1, t, scale;
86 86 struct pos *h = NULL;
87 87
88 88 /* build a hash table of the next highest power of 2 */
89 89 while (buckets < bn + 1)
90 90 buckets *= 2;
91 91
92 92 /* try to allocate a large hash table to avoid collisions */
93 93 for (scale = 4; scale; scale /= 2) {
94 94 h = (struct pos *)malloc(scale * buckets * sizeof(struct pos));
95 95 if (h)
96 96 break;
97 97 }
98 98
99 99 if (!h)
100 100 return 0;
101 101
102 102 buckets = buckets * scale - 1;
103 103
104 104 /* clear the hash table */
105 105 for (i = 0; i <= buckets; i++) {
106 106 h[i].pos = -1;
107 107 h[i].len = 0;
108 108 }
109 109
110 110 /* add lines to the hash table chains */
111 111 for (i = 0; i < bn; i++) {
112 112 /* find the equivalence class */
113 113 for (j = b[i].hash & buckets; h[j].pos != -1;
114 114 j = (j + 1) & buckets)
115 115 if (!cmp(b + i, b + h[j].pos))
116 116 break;
117 117
118 118 /* add to the head of the equivalence class */
119 119 b[i].n = h[j].pos;
120 120 b[i].e = j;
121 121 h[j].pos = i;
122 122 h[j].len++; /* keep track of popularity */
123 123 }
124 124
125 125 /* compute popularity threshold */
126 126 t = (bn >= 31000) ? bn / 1000 : 1000000 / (bn + 1);
127 127
128 128 /* match items in a to their equivalence class in b */
129 129 for (i = 0; i < an; i++) {
130 130 /* find the equivalence class */
131 131 for (j = a[i].hash & buckets; h[j].pos != -1;
132 132 j = (j + 1) & buckets)
133 133 if (!cmp(a + i, b + h[j].pos))
134 134 break;
135 135
136 136 a[i].e = j; /* use equivalence class for quick compare */
137 137 if (h[j].len <= t)
138 138 a[i].n = h[j].pos; /* point to head of match list */
139 139 else
140 140 a[i].n = -1; /* too popular */
141 141 }
142 142
143 143 /* discard hash tables */
144 144 free(h);
145 145 return 1;
146 146 }
147 147
148 148 static int longest_match(struct line *a, struct line *b, struct pos *pos,
149 149 int a1, int a2, int b1, int b2, int *omi, int *omj)
150 150 {
151 151 int mi = a1, mj = b1, mk = 0, mb = 0, i, j, k, half;
152 152
153 153 /* window our search on large regions to better bound
154 154 worst-case performance. by choosing a window at the end, we
155 155 reduce skipping overhead on the b chains. */
156 156 if (a2 - a1 > 30000)
157 157 a1 = a2 - 30000;
158 158
159 159 half = (a1 + a2) / 2;
160 160
161 161 for (i = a1; i < a2; i++) {
162 162 /* skip all lines in b after the current block */
163 163 for (j = a[i].n; j >= b2; j = b[j].n)
164 164 ;
165 165
166 166 /* loop through all lines match a[i] in b */
167 167 for (; j >= b1; j = b[j].n) {
168 168 /* does this extend an earlier match? */
169 if (i > a1 && j > b1 && pos[j - 1].pos == i - 1)
170 k = pos[j - 1].len + 1;
171 else
172 k = 1;
169 for (k = 1; j - k >= b1 && i - k >= a1; k++) {
170 /* reached an earlier match? */
171 if (pos[j - k].pos == i - k) {
172 k += pos[j - k].len;
173 break;
174 }
175 /* previous line mismatch? */
176 if (a[i - k].e != b[j - k].e)
177 break;
178 }
179
173 180 pos[j].pos = i;
174 181 pos[j].len = k;
175 182
176 183 /* best match so far? we prefer matches closer
177 184 to the middle to balance recursion */
178 185 if (k > mk || (k == mk && (i <= mi || i < half))) {
179 186 mi = i;
180 187 mj = j;
181 188 mk = k;
182 189 }
183 190 }
184 191 }
185 192
186 193 if (mk) {
187 194 mi = mi - mk + 1;
188 195 mj = mj - mk + 1;
189 196 }
190 197
191 198 /* expand match to include neighboring popular lines */
192 199 while (mi - mb > a1 && mj - mb > b1 &&
193 200 a[mi - mb - 1].e == b[mj - mb - 1].e)
194 201 mb++;
195 202 while (mi + mk < a2 && mj + mk < b2 &&
196 203 a[mi + mk].e == b[mj + mk].e)
197 204 mk++;
198 205
199 206 *omi = mi - mb;
200 207 *omj = mj - mb;
201 208
202 209 return mk + mb;
203 210 }
204 211
205 212 static struct hunk *recurse(struct line *a, struct line *b, struct pos *pos,
206 213 int a1, int a2, int b1, int b2, struct hunk *l)
207 214 {
208 215 int i, j, k;
209 216
210 217 while (1) {
211 218 /* find the longest match in this chunk */
212 219 k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j);
213 220 if (!k)
214 221 return l;
215 222
216 223 /* and recurse on the remaining chunks on either side */
217 224 l = recurse(a, b, pos, a1, i, b1, j, l);
218 225 if (!l)
219 226 return NULL;
220 227
221 228 l->next = (struct hunk *)malloc(sizeof(struct hunk));
222 229 if (!l->next)
223 230 return NULL;
224 231
225 232 l = l->next;
226 233 l->a1 = i;
227 234 l->a2 = i + k;
228 235 l->b1 = j;
229 236 l->b2 = j + k;
230 237 l->next = NULL;
231 238
232 239 /* tail-recursion didn't happen, so do equivalent iteration */
233 240 a1 = i + k;
234 241 b1 = j + k;
235 242 }
236 243 }
237 244
238 245 static int diff(struct line *a, int an, struct line *b, int bn,
239 246 struct hunk *base)
240 247 {
241 248 struct hunk *curr;
242 249 struct pos *pos;
243 250 int t, count = 0;
244 251
245 252 /* allocate and fill arrays */
246 253 t = equatelines(a, an, b, bn);
247 254 pos = (struct pos *)calloc(bn ? bn : 1, sizeof(struct pos));
248 255
249 256 if (pos && t) {
250 257 /* generate the matching block list */
251 258
252 259 curr = recurse(a, b, pos, 0, an, 0, bn, base);
253 260 if (!curr)
254 261 return -1;
255 262
256 263 /* sentinel end hunk */
257 264 curr->next = (struct hunk *)malloc(sizeof(struct hunk));
258 265 if (!curr->next)
259 266 return -1;
260 267 curr = curr->next;
261 268 curr->a1 = curr->a2 = an;
262 269 curr->b1 = curr->b2 = bn;
263 270 curr->next = NULL;
264 271 }
265 272
266 273 free(pos);
267 274
268 275 /* normalize the hunk list, try to push each hunk towards the end */
269 276 for (curr = base->next; curr; curr = curr->next) {
270 277 struct hunk *next = curr->next;
271 278
272 279 if (!next)
273 280 break;
274 281
275 282 if (curr->a2 == next->a1 || curr->b2 == next->b1)
276 283 while (curr->a2 < an && curr->b2 < bn
277 284 && next->a1 < next->a2
278 285 && next->b1 < next->b2
279 286 && !cmp(a + curr->a2, b + curr->b2)) {
280 287 curr->a2++;
281 288 next->a1++;
282 289 curr->b2++;
283 290 next->b1++;
284 291 }
285 292 }
286 293
287 294 for (curr = base->next; curr; curr = curr->next)
288 295 count++;
289 296 return count;
290 297 }
291 298
292 299 static void freehunks(struct hunk *l)
293 300 {
294 301 struct hunk *n;
295 302 for (; l; l = n) {
296 303 n = l->next;
297 304 free(l);
298 305 }
299 306 }
300 307
301 308 static PyObject *blocks(PyObject *self, PyObject *args)
302 309 {
303 310 PyObject *sa, *sb, *rl = NULL, *m;
304 311 struct line *a, *b;
305 312 struct hunk l, *h;
306 313 int an, bn, count, pos = 0;
307 314
308 315 l.next = NULL;
309 316
310 317 if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb))
311 318 return NULL;
312 319
313 320 an = splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a);
314 321 bn = splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b);
315 322
316 323 if (!a || !b)
317 324 goto nomem;
318 325
319 326 count = diff(a, an, b, bn, &l);
320 327 if (count < 0)
321 328 goto nomem;
322 329
323 330 rl = PyList_New(count);
324 331 if (!rl)
325 332 goto nomem;
326 333
327 334 for (h = l.next; h; h = h->next) {
328 335 m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2);
329 336 PyList_SetItem(rl, pos, m);
330 337 pos++;
331 338 }
332 339
333 340 nomem:
334 341 free(a);
335 342 free(b);
336 343 freehunks(l.next);
337 344 return rl ? rl : PyErr_NoMemory();
338 345 }
339 346
340 347 static PyObject *bdiff(PyObject *self, PyObject *args)
341 348 {
342 349 char *sa, *sb, *rb;
343 350 PyObject *result = NULL;
344 351 struct line *al, *bl;
345 352 struct hunk l, *h;
346 353 int an, bn, count;
347 354 Py_ssize_t len = 0, la, lb;
348 355 PyThreadState *_save;
349 356
350 357 l.next = NULL;
351 358
352 359 if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb))
353 360 return NULL;
354 361
355 362 if (la > UINT_MAX || lb > UINT_MAX) {
356 363 PyErr_SetString(PyExc_ValueError, "bdiff inputs too large");
357 364 return NULL;
358 365 }
359 366
360 367 _save = PyEval_SaveThread();
361 368 an = splitlines(sa, la, &al);
362 369 bn = splitlines(sb, lb, &bl);
363 370 if (!al || !bl)
364 371 goto nomem;
365 372
366 373 count = diff(al, an, bl, bn, &l);
367 374 if (count < 0)
368 375 goto nomem;
369 376
370 377 /* calculate length of output */
371 378 la = lb = 0;
372 379 for (h = l.next; h; h = h->next) {
373 380 if (h->a1 != la || h->b1 != lb)
374 381 len += 12 + bl[h->b1].l - bl[lb].l;
375 382 la = h->a2;
376 383 lb = h->b2;
377 384 }
378 385 PyEval_RestoreThread(_save);
379 386 _save = NULL;
380 387
381 388 result = PyBytes_FromStringAndSize(NULL, len);
382 389
383 390 if (!result)
384 391 goto nomem;
385 392
386 393 /* build binary patch */
387 394 rb = PyBytes_AsString(result);
388 395 la = lb = 0;
389 396
390 397 for (h = l.next; h; h = h->next) {
391 398 if (h->a1 != la || h->b1 != lb) {
392 399 len = bl[h->b1].l - bl[lb].l;
393 400 putbe32((uint32_t)(al[la].l - al->l), rb);
394 401 putbe32((uint32_t)(al[h->a1].l - al->l), rb + 4);
395 402 putbe32((uint32_t)len, rb + 8);
396 403 memcpy(rb + 12, bl[lb].l, len);
397 404 rb += 12 + len;
398 405 }
399 406 la = h->a2;
400 407 lb = h->b2;
401 408 }
402 409
403 410 nomem:
404 411 if (_save)
405 412 PyEval_RestoreThread(_save);
406 413 free(al);
407 414 free(bl);
408 415 freehunks(l.next);
409 416 return result ? result : PyErr_NoMemory();
410 417 }
411 418
412 419 /*
413 420 * If allws != 0, remove all whitespace (' ', \t and \r). Otherwise,
414 421 * reduce whitespace sequences to a single space and trim remaining whitespace
415 422 * from end of lines.
416 423 */
417 424 static PyObject *fixws(PyObject *self, PyObject *args)
418 425 {
419 426 PyObject *s, *result = NULL;
420 427 char allws, c;
421 428 const char *r;
422 429 Py_ssize_t i, rlen, wlen = 0;
423 430 char *w;
424 431
425 432 if (!PyArg_ParseTuple(args, "Sb:fixws", &s, &allws))
426 433 return NULL;
427 434 r = PyBytes_AsString(s);
428 435 rlen = PyBytes_Size(s);
429 436
430 437 w = (char *)malloc(rlen ? rlen : 1);
431 438 if (!w)
432 439 goto nomem;
433 440
434 441 for (i = 0; i != rlen; i++) {
435 442 c = r[i];
436 443 if (c == ' ' || c == '\t' || c == '\r') {
437 444 if (!allws && (wlen == 0 || w[wlen - 1] != ' '))
438 445 w[wlen++] = ' ';
439 446 } else if (c == '\n' && !allws
440 447 && wlen > 0 && w[wlen - 1] == ' ') {
441 448 w[wlen - 1] = '\n';
442 449 } else {
443 450 w[wlen++] = c;
444 451 }
445 452 }
446 453
447 454 result = PyBytes_FromStringAndSize(w, wlen);
448 455
449 456 nomem:
450 457 free(w);
451 458 return result ? result : PyErr_NoMemory();
452 459 }
453 460
454 461
455 462 static char mdiff_doc[] = "Efficient binary diff.";
456 463
457 464 static PyMethodDef methods[] = {
458 465 {"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"},
459 466 {"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"},
460 467 {"fixws", fixws, METH_VARARGS, "normalize diff whitespaces\n"},
461 468 {NULL, NULL}
462 469 };
463 470
464 471 #ifdef IS_PY3K
465 472 static struct PyModuleDef bdiff_module = {
466 473 PyModuleDef_HEAD_INIT,
467 474 "bdiff",
468 475 mdiff_doc,
469 476 -1,
470 477 methods
471 478 };
472 479
473 480 PyMODINIT_FUNC PyInit_bdiff(void)
474 481 {
475 482 return PyModule_Create(&bdiff_module);
476 483 }
477 484 #else
478 485 PyMODINIT_FUNC initbdiff(void)
479 486 {
480 487 Py_InitModule3("bdiff", methods, mdiff_doc);
481 488 }
482 489 #endif
483 490
General Comments 0
You need to be logged in to leave comments. Login now