##// END OF EJS Templates
bdiff: improve worst case behavior by 100x....
Vadim Gelfer -
r2577:fa76c5d6 default
parent child Browse files
Show More
@@ -1,362 +1,371
1 1 /*
2 2 bdiff.c - efficient binary diff extension for Mercurial
3 3
4 4 Copyright 2005 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
16 16 #ifdef __hpux
17 17 #define inline
18 18 #endif
19 19
20 20 #ifdef __SUNPRO_C
21 21 # define inline
22 22 #endif
23 23
24 24 #ifdef _WIN32
25 25 #ifdef _MSC_VER
26 26 #define inline __inline
27 27 typedef unsigned long uint32_t;
28 28 #else
29 29 #include <stdint.h>
30 30 #endif
31 31 static uint32_t htonl(uint32_t x)
32 32 {
33 33 return ((x & 0x000000ffUL) << 24) |
34 34 ((x & 0x0000ff00UL) << 8) |
35 35 ((x & 0x00ff0000UL) >> 8) |
36 36 ((x & 0xff000000UL) >> 24);
37 37 }
38 38 #else
39 39 #include <sys/types.h>
40 40 #include <arpa/inet.h>
41 41 #include <inttypes.h>
42 42 #endif
43 43
44 44 struct line {
45 45 int h, len, n, e;
46 46 const char *l;
47 47 };
48 48
49 49 struct pos {
50 50 int pos, len;
51 51 };
52 52
53 53 struct hunk {
54 54 int a1, a2, b1, b2;
55 55 };
56 56
57 57 struct hunklist {
58 58 struct hunk *base, *head;
59 59 };
60 60
61 61 static inline uint32_t rol32(uint32_t word, unsigned int shift)
62 62 {
63 63 return (word << shift) | (word >> (32 - shift));
64 64 }
65 65
66 66 int splitlines(const char *a, int len, struct line **lr)
67 67 {
68 int h, i;
68 int g, h, i;
69 69 const char *p, *b = a;
70 70 struct line *l;
71 71
72 72 /* count the lines */
73 73 i = 1; /* extra line for sentinel */
74 74 for (p = a; p < a + len; p++)
75 75 if (*p == '\n' || p == a + len - 1)
76 76 i++;
77 77
78 78 *lr = l = (struct line *)malloc(sizeof(struct line) * i);
79 79 if (!l)
80 80 return -1;
81 81
82 82 /* build the line array and calculate hashes */
83 83 h = 0;
84 84 for (p = a; p < a + len; p++) {
85 h = *p + rol32(h, 7); /* a simple hash from GNU diff */
85 /*
86 * a simple hash from GNU diff, with better collision
87 * resistance from hashpjw. this slows down common
88 * case by 10%, but speeds up worst case by 100x.
89 */
90 h = *p + rol32(h, 7);
91 if ((g = h & 0xf0000000)) {
92 h ^= g >> 24;
93 h ^= g;
94 }
86 95 if (*p == '\n' || p == a + len - 1) {
87 96 l->len = p - b + 1;
88 97 l->h = h * l->len;
89 98 l->l = b;
90 99 l->n = -1;
91 100 l++;
92 101 b = p + 1;
93 102 h = 0;
94 103 }
95 104 }
96 105
97 106 /* set up a sentinel */
98 107 l->h = l->len = 0;
99 108 l->l = a + len;
100 109 return i - 1;
101 110 }
102 111
103 112 int inline cmp(struct line *a, struct line *b)
104 113 {
105 114 return a->h != b->h || a->len != b->len || memcmp(a->l, b->l, a->len);
106 115 }
107 116
108 117 static int equatelines(struct line *a, int an, struct line *b, int bn)
109 118 {
110 119 int i, j, buckets = 1, t;
111 120 struct pos *h;
112 121
113 122 /* build a hash table of the next highest power of 2 */
114 123 while (buckets < bn + 1)
115 124 buckets *= 2;
116 125
117 126 h = (struct pos *)malloc(buckets * sizeof(struct pos));
118 127 buckets = buckets - 1;
119 128 if (!h)
120 129 return 0;
121 130
122 131 /* clear the hash table */
123 132 for (i = 0; i <= buckets; i++) {
124 133 h[i].pos = -1;
125 134 h[i].len = 0;
126 135 }
127 136
128 137 /* add lines to the hash table chains */
129 138 for (i = bn - 1; i >= 0; i--) {
130 139 /* find the equivalence class */
131 140 for (j = b[i].h & buckets; h[j].pos != -1;
132 141 j = (j + 1) & buckets)
133 142 if (!cmp(b + i, b + h[j].pos))
134 143 break;
135 144
136 145 /* add to the head of the equivalence class */
137 146 b[i].n = h[j].pos;
138 147 b[i].e = j;
139 148 h[j].pos = i;
140 149 h[j].len++; /* keep track of popularity */
141 150 }
142 151
143 152 /* compute popularity threshold */
144 153 t = (bn >= 200) ? bn / 100 : bn + 1;
145 154
146 155 /* match items in a to their equivalence class in b */
147 156 for (i = 0; i < an; i++) {
148 157 /* find the equivalence class */
149 158 for (j = a[i].h & buckets; h[j].pos != -1;
150 159 j = (j + 1) & buckets)
151 160 if (!cmp(a + i, b + h[j].pos))
152 161 break;
153 162
154 163 a[i].e = j; /* use equivalence class for quick compare */
155 164 if (h[j].len <= t)
156 165 a[i].n = h[j].pos; /* point to head of match list */
157 166 else
158 167 a[i].n = -1; /* too popular */
159 168 }
160 169
161 170 /* discard hash tables */
162 171 free(h);
163 172 return 1;
164 173 }
165 174
166 175 static int longest_match(struct line *a, struct line *b, struct pos *pos,
167 176 int a1, int a2, int b1, int b2, int *omi, int *omj)
168 177 {
169 178 int mi = a1, mj = b1, mk = 0, mb = 0, i, j, k;
170 179
171 180 for (i = a1; i < a2; i++) {
172 181 /* skip things before the current block */
173 182 for (j = a[i].n; j != -1 && j < b1; j = b[j].n)
174 183 ;
175 184
176 185 /* loop through all lines match a[i] in b */
177 186 for (; j != -1 && j < b2; j = b[j].n) {
178 187 /* does this extend an earlier match? */
179 188 if (i > a1 && j > b1 && pos[j - 1].pos == i - 1)
180 189 k = pos[j - 1].len + 1;
181 190 else
182 191 k = 1;
183 192 pos[j].pos = i;
184 193 pos[j].len = k;
185 194
186 195 /* best match so far? */
187 196 if (k > mk) {
188 197 mi = i;
189 198 mj = j;
190 199 mk = k;
191 200 }
192 201 }
193 202 }
194 203
195 204 if (mk) {
196 205 mi = mi - mk + 1;
197 206 mj = mj - mk + 1;
198 207 }
199 208
200 209 /* expand match to include neighboring popular lines */
201 210 while (mi - mb > a1 && mj - mb > b1 &&
202 211 a[mi - mb - 1].e == b[mj - mb - 1].e)
203 212 mb++;
204 213 while (mi + mk < a2 && mj + mk < b2 &&
205 214 a[mi + mk].e == b[mj + mk].e)
206 215 mk++;
207 216
208 217 *omi = mi - mb;
209 218 *omj = mj - mb;
210 219 return mk + mb;
211 220 }
212 221
213 222 static void recurse(struct line *a, struct line *b, struct pos *pos,
214 223 int a1, int a2, int b1, int b2, struct hunklist *l)
215 224 {
216 225 int i, j, k;
217 226
218 227 /* find the longest match in this chunk */
219 228 k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j);
220 229 if (!k)
221 230 return;
222 231
223 232 /* and recurse on the remaining chunks on either side */
224 233 recurse(a, b, pos, a1, i, b1, j, l);
225 234 l->head->a1 = i;
226 235 l->head->a2 = i + k;
227 236 l->head->b1 = j;
228 237 l->head->b2 = j + k;
229 238 l->head++;
230 239 recurse(a, b, pos, i + k, a2, j + k, b2, l);
231 240 }
232 241
233 242 static struct hunklist diff(struct line *a, int an, struct line *b, int bn)
234 243 {
235 244 struct hunklist l;
236 245 struct pos *pos;
237 246 int t;
238 247
239 248 /* allocate and fill arrays */
240 249 t = equatelines(a, an, b, bn);
241 250 pos = (struct pos *)calloc(bn, sizeof(struct pos));
242 251 /* we can't have more matches than lines in the shorter file */
243 252 l.head = l.base = (struct hunk *)malloc(sizeof(struct hunk) *
244 253 ((an<bn ? an:bn) + 1));
245 254
246 255 if (pos && l.base && t) {
247 256 /* generate the matching block list */
248 257 recurse(a, b, pos, 0, an, 0, bn, &l);
249 258 l.head->a1 = an;
250 259 l.head->b1 = bn;
251 260 l.head++;
252 261 }
253 262
254 263 free(pos);
255 264 return l;
256 265 }
257 266
258 267 static PyObject *blocks(PyObject *self, PyObject *args)
259 268 {
260 269 PyObject *sa, *sb, *rl = NULL, *m;
261 270 struct line *a, *b;
262 271 struct hunklist l = {NULL, NULL};
263 272 struct hunk *h;
264 273 int an, bn, pos = 0;
265 274
266 275 if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb))
267 276 return NULL;
268 277
269 278 an = splitlines(PyString_AsString(sa), PyString_Size(sa), &a);
270 279 bn = splitlines(PyString_AsString(sb), PyString_Size(sb), &b);
271 280 if (!a || !b)
272 281 goto nomem;
273 282
274 283 l = diff(a, an, b, bn);
275 284 rl = PyList_New(l.head - l.base);
276 285 if (!l.head || !rl)
277 286 goto nomem;
278 287
279 288 for (h = l.base; h != l.head; h++) {
280 289 m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2);
281 290 PyList_SetItem(rl, pos, m);
282 291 pos++;
283 292 }
284 293
285 294 nomem:
286 295 free(a);
287 296 free(b);
288 297 free(l.base);
289 298 return rl ? rl : PyErr_NoMemory();
290 299 }
291 300
292 301 static PyObject *bdiff(PyObject *self, PyObject *args)
293 302 {
294 303 PyObject *sa, *sb, *result = NULL;
295 304 struct line *al, *bl;
296 305 struct hunklist l = {NULL, NULL};
297 306 struct hunk *h;
298 307 char encode[12], *rb;
299 308 int an, bn, len = 0, la = 0, lb = 0;
300 309
301 310 if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb))
302 311 return NULL;
303 312
304 313 an = splitlines(PyString_AsString(sa), PyString_Size(sa), &al);
305 314 bn = splitlines(PyString_AsString(sb), PyString_Size(sb), &bl);
306 315 if (!al || !bl)
307 316 goto nomem;
308 317
309 318 l = diff(al, an, bl, bn);
310 319 if (!l.head)
311 320 goto nomem;
312 321
313 322 /* calculate length of output */
314 323 for (h = l.base; h != l.head; h++) {
315 324 if (h->a1 != la || h->b1 != lb)
316 325 len += 12 + bl[h->b1].l - bl[lb].l;
317 326 la = h->a2;
318 327 lb = h->b2;
319 328 }
320 329
321 330 result = PyString_FromStringAndSize(NULL, len);
322 331 if (!result)
323 332 goto nomem;
324 333
325 334 /* build binary patch */
326 335 rb = PyString_AsString(result);
327 336 la = lb = 0;
328 337
329 338 for (h = l.base; h != l.head; h++) {
330 339 if (h->a1 != la || h->b1 != lb) {
331 340 len = bl[h->b1].l - bl[lb].l;
332 341 *(uint32_t *)(encode) = htonl(al[la].l - al->l);
333 342 *(uint32_t *)(encode + 4) = htonl(al[h->a1].l - al->l);
334 343 *(uint32_t *)(encode + 8) = htonl(len);
335 344 memcpy(rb, encode, 12);
336 345 memcpy(rb + 12, bl[lb].l, len);
337 346 rb += 12 + len;
338 347 }
339 348 la = h->a2;
340 349 lb = h->b2;
341 350 }
342 351
343 352 nomem:
344 353 free(al);
345 354 free(bl);
346 355 free(l.base);
347 356 return result ? result : PyErr_NoMemory();
348 357 }
349 358
350 359 static char mdiff_doc[] = "Efficient binary diff.";
351 360
352 361 static PyMethodDef methods[] = {
353 362 {"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"},
354 363 {"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"},
355 364 {NULL, NULL}
356 365 };
357 366
358 367 PyMODINIT_FUNC initbdiff(void)
359 368 {
360 369 Py_InitModule3("bdiff", methods, mdiff_doc);
361 370 }
362 371
General Comments 0
You need to be logged in to leave comments. Login now