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