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