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