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