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