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