##// END OF EJS Templates
bdiff: rearrange the "better longest match" code...
Mads Kiilerich -
r30430:5c4e2636 default
parent child Browse files
Show More
@@ -1,301 +1,311 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 <stdlib.h>
13 13 #include <string.h>
14 14 #include <limits.h>
15 15
16 16 #include "compat.h"
17 17 #include "bitmanipulation.h"
18 18 #include "bdiff.h"
19 19
20 20 /* Hash implementation from diffutils */
21 21 #define ROL(v, n) ((v) << (n) | (v) >> (sizeof(v) * CHAR_BIT - (n)))
22 22 #define HASH(h, c) ((c) + ROL(h ,7))
23 23
24 24 struct pos {
25 25 int pos, len;
26 26 };
27 27
28 28 int bdiff_splitlines(const char *a, ssize_t len, struct bdiff_line **lr)
29 29 {
30 30 unsigned hash;
31 31 int i;
32 32 const char *p, *b = a;
33 33 const char * const plast = a + len - 1;
34 34 struct bdiff_line *l;
35 35
36 36 /* count the lines */
37 37 i = 1; /* extra line for sentinel */
38 38 for (p = a; p < plast; p++)
39 39 if (*p == '\n')
40 40 i++;
41 41 if (p == plast)
42 42 i++;
43 43
44 44 *lr = l = (struct bdiff_line *)malloc(sizeof(struct bdiff_line) * i);
45 45 if (!l)
46 46 return -1;
47 47
48 48 /* build the line array and calculate hashes */
49 49 hash = 0;
50 50 for (p = a; p < a + len; p++) {
51 51 hash = HASH(hash, *p);
52 52
53 53 if (*p == '\n' || p == plast) {
54 54 l->hash = hash;
55 55 hash = 0;
56 56 l->len = p - b + 1;
57 57 l->l = b;
58 58 l->n = INT_MAX;
59 59 l++;
60 60 b = p + 1;
61 61 }
62 62 }
63 63
64 64 /* set up a sentinel */
65 65 l->hash = 0;
66 66 l->len = 0;
67 67 l->l = a + len;
68 68 return i - 1;
69 69 }
70 70
71 71 static inline int cmp(struct bdiff_line *a, struct bdiff_line *b)
72 72 {
73 73 return a->hash != b->hash || a->len != b->len || memcmp(a->l, b->l, a->len);
74 74 }
75 75
76 76 static int equatelines(struct bdiff_line *a, int an, struct bdiff_line *b,
77 77 int bn)
78 78 {
79 79 int i, j, buckets = 1, t, scale;
80 80 struct pos *h = NULL;
81 81
82 82 /* build a hash table of the next highest power of 2 */
83 83 while (buckets < bn + 1)
84 84 buckets *= 2;
85 85
86 86 /* try to allocate a large hash table to avoid collisions */
87 87 for (scale = 4; scale; scale /= 2) {
88 88 h = (struct pos *)malloc(scale * buckets * sizeof(struct pos));
89 89 if (h)
90 90 break;
91 91 }
92 92
93 93 if (!h)
94 94 return 0;
95 95
96 96 buckets = buckets * scale - 1;
97 97
98 98 /* clear the hash table */
99 99 for (i = 0; i <= buckets; i++) {
100 100 h[i].pos = -1;
101 101 h[i].len = 0;
102 102 }
103 103
104 104 /* add lines to the hash table chains */
105 105 for (i = 0; i < bn; i++) {
106 106 /* find the equivalence class */
107 107 for (j = b[i].hash & buckets; h[j].pos != -1;
108 108 j = (j + 1) & buckets)
109 109 if (!cmp(b + i, b + h[j].pos))
110 110 break;
111 111
112 112 /* add to the head of the equivalence class */
113 113 b[i].n = h[j].pos;
114 114 b[i].e = j;
115 115 h[j].pos = i;
116 116 h[j].len++; /* keep track of popularity */
117 117 }
118 118
119 119 /* compute popularity threshold */
120 120 t = (bn >= 31000) ? bn / 1000 : 1000000 / (bn + 1);
121 121
122 122 /* match items in a to their equivalence class in b */
123 123 for (i = 0; i < an; i++) {
124 124 /* find the equivalence class */
125 125 for (j = a[i].hash & buckets; h[j].pos != -1;
126 126 j = (j + 1) & buckets)
127 127 if (!cmp(a + i, b + h[j].pos))
128 128 break;
129 129
130 130 a[i].e = j; /* use equivalence class for quick compare */
131 131 if (h[j].len <= t)
132 132 a[i].n = h[j].pos; /* point to head of match list */
133 133 else
134 134 a[i].n = -1; /* too popular */
135 135 }
136 136
137 137 /* discard hash tables */
138 138 free(h);
139 139 return 1;
140 140 }
141 141
142 142 static int longest_match(struct bdiff_line *a, struct bdiff_line *b,
143 143 struct pos *pos,
144 144 int a1, int a2, int b1, int b2, int *omi, int *omj)
145 145 {
146 146 int mi = a1, mj = b1, mk = 0, i, j, k, half;
147 147
148 148 /* window our search on large regions to better bound
149 149 worst-case performance. by choosing a window at the end, we
150 150 reduce skipping overhead on the b chains. */
151 151 if (a2 - a1 > 30000)
152 152 a1 = a2 - 30000;
153 153
154 154 half = (a1 + a2 - 1) / 2;
155 155
156 156 for (i = a1; i < a2; i++) {
157 157 /* skip all lines in b after the current block */
158 158 for (j = a[i].n; j >= b2; j = b[j].n)
159 159 ;
160 160
161 161 /* loop through all lines match a[i] in b */
162 162 for (; j >= b1; j = b[j].n) {
163 163 /* does this extend an earlier match? */
164 164 for (k = 1; j - k >= b1 && i - k >= a1; k++) {
165 165 /* reached an earlier match? */
166 166 if (pos[j - k].pos == i - k) {
167 167 k += pos[j - k].len;
168 168 break;
169 169 }
170 170 /* previous line mismatch? */
171 171 if (a[i - k].e != b[j - k].e)
172 172 break;
173 173 }
174 174
175 175 pos[j].pos = i;
176 176 pos[j].len = k;
177 177
178 178 /* best match so far? we prefer matches closer
179 179 to the middle to balance recursion */
180 if (k > mk || (k == mk && (i <= mi || i <= half))) {
180 if (k > mk) {
181 /* a longer match */
181 182 mi = i;
182 183 mj = j;
183 184 mk = k;
185 } else if (k == mk) {
186 if (i > mi && i <= half) {
187 /* same match but closer to half */
188 mi = i;
189 mj = j;
190 } else if (i == mi) {
191 /* same i but earlier j */
192 mj = j;
193 }
184 194 }
185 195 }
186 196 }
187 197
188 198 if (mk) {
189 199 mi = mi - mk + 1;
190 200 mj = mj - mk + 1;
191 201 }
192 202
193 203 /* expand match to include subsequent popular lines */
194 204 while (mi + mk < a2 && mj + mk < b2 &&
195 205 a[mi + mk].e == b[mj + mk].e)
196 206 mk++;
197 207
198 208 *omi = mi;
199 209 *omj = mj;
200 210
201 211 return mk;
202 212 }
203 213
204 214 static struct bdiff_hunk *recurse(struct bdiff_line *a, struct bdiff_line *b,
205 215 struct pos *pos,
206 216 int a1, int a2, int b1, int b2, struct bdiff_hunk *l)
207 217 {
208 218 int i, j, k;
209 219
210 220 while (1) {
211 221 /* find the longest match in this chunk */
212 222 k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j);
213 223 if (!k)
214 224 return l;
215 225
216 226 /* and recurse on the remaining chunks on either side */
217 227 l = recurse(a, b, pos, a1, i, b1, j, l);
218 228 if (!l)
219 229 return NULL;
220 230
221 231 l->next = (struct bdiff_hunk *)malloc(sizeof(struct bdiff_hunk));
222 232 if (!l->next)
223 233 return NULL;
224 234
225 235 l = l->next;
226 236 l->a1 = i;
227 237 l->a2 = i + k;
228 238 l->b1 = j;
229 239 l->b2 = j + k;
230 240 l->next = NULL;
231 241
232 242 /* tail-recursion didn't happen, so do equivalent iteration */
233 243 a1 = i + k;
234 244 b1 = j + k;
235 245 }
236 246 }
237 247
238 248 int bdiff_diff(struct bdiff_line *a, int an, struct bdiff_line *b,
239 249 int bn, struct bdiff_hunk *base)
240 250 {
241 251 struct bdiff_hunk *curr;
242 252 struct pos *pos;
243 253 int t, count = 0;
244 254
245 255 /* allocate and fill arrays */
246 256 t = equatelines(a, an, b, bn);
247 257 pos = (struct pos *)calloc(bn ? bn : 1, sizeof(struct pos));
248 258
249 259 if (pos && t) {
250 260 /* generate the matching block list */
251 261
252 262 curr = recurse(a, b, pos, 0, an, 0, bn, base);
253 263 if (!curr)
254 264 return -1;
255 265
256 266 /* sentinel end hunk */
257 267 curr->next = (struct bdiff_hunk *)malloc(sizeof(struct bdiff_hunk));
258 268 if (!curr->next)
259 269 return -1;
260 270 curr = curr->next;
261 271 curr->a1 = curr->a2 = an;
262 272 curr->b1 = curr->b2 = bn;
263 273 curr->next = NULL;
264 274 }
265 275
266 276 free(pos);
267 277
268 278 /* normalize the hunk list, try to push each hunk towards the end */
269 279 for (curr = base->next; curr; curr = curr->next) {
270 280 struct bdiff_hunk *next = curr->next;
271 281
272 282 if (!next)
273 283 break;
274 284
275 285 if (curr->a2 == next->a1 || curr->b2 == next->b1)
276 286 while (curr->a2 < an && curr->b2 < bn
277 287 && next->a1 < next->a2
278 288 && next->b1 < next->b2
279 289 && !cmp(a + curr->a2, b + curr->b2)) {
280 290 curr->a2++;
281 291 next->a1++;
282 292 curr->b2++;
283 293 next->b1++;
284 294 }
285 295 }
286 296
287 297 for (curr = base->next; curr; curr = curr->next)
288 298 count++;
289 299 return count;
290 300 }
291 301
292 302 void bdiff_freehunks(struct bdiff_hunk *l)
293 303 {
294 304 struct bdiff_hunk *n;
295 305 for (; l; l = n) {
296 306 n = l->next;
297 307 free(l);
298 308 }
299 309 }
300 310
301 311
General Comments 0
You need to be logged in to leave comments. Login now