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