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