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