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