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