##// END OF EJS Templates
Minor speed improvements for bdiff...
mpm@selenic.com -
r474:b2ae8283 default
parent child Browse files
Show More
@@ -30,6 +30,10 struct line {
30 30 const char *l;
31 31 };
32 32
33 struct pos {
34 int pos, len;
35 };
36
33 37 struct hunk {
34 38 int a1, a2, b1, b2;
35 39 };
@@ -87,36 +91,37 int inline cmp(struct line *a, struct li
87 91
88 92 static int equatelines(struct line *a, int an, struct line *b, int bn)
89 93 {
90 int i, j, buckets = 1, t, *h, *l;
94 int i, j, buckets = 1, t;
95 struct pos *h;
91 96
92 97 /* build a hash table of the next highest power of 2 */
93 98 while (buckets < bn + 1)
94 99 buckets *= 2;
95 100
96 h = malloc(buckets * sizeof(int));
97 l = calloc(buckets, sizeof(int));
101 h = malloc(buckets * sizeof(struct pos));
98 102 buckets = buckets - 1;
99 if (!h || !l) {
100 free(h);
103 if (!h)
101 104 return 0;
102 }
103 105
104 106 /* clear the hash table */
105 for (i = 0; i <= buckets; i++)
106 h[i] = -1;
107 for (i = 0; i <= buckets; i++) {
108 h[i].pos = -1;
109 h[i].len = 0;
110 }
107 111
108 112 /* add lines to the hash table chains */
109 113 for (i = bn - 1; i >= 0; i--) {
110 114 /* find the equivalence class */
111 for (j = b[i].h & buckets; h[j] != -1; j = (j + 1) & buckets)
112 if (!cmp(b + i, b + h[j]))
115 for (j = b[i].h & buckets; h[j].pos != -1;
116 j = (j + 1) & buckets)
117 if (!cmp(b + i, b + h[j].pos))
113 118 break;
114 119
115 120 /* add to the head of the equivalence class */
116 b[i].n = h[j];
121 b[i].n = h[j].pos;
117 122 b[i].e = j;
118 h[j] = i;
119 l[j]++; /* keep track of popularity */
123 h[j].pos = i;
124 h[j].len++; /* keep track of popularity */
120 125 }
121 126
122 127 /* compute popularity threshold */
@@ -125,24 +130,24 static int equatelines(struct line *a, i
125 130 /* match items in a to their equivalence class in b */
126 131 for (i = 0; i < an; i++) {
127 132 /* find the equivalence class */
128 for (j = a[i].h & buckets; h[j] != -1; j = (j + 1) & buckets)
129 if (!cmp(a + i, b + h[j]))
133 for (j = a[i].h & buckets; h[j].pos != -1;
134 j = (j + 1) & buckets)
135 if (!cmp(a + i, b + h[j].pos))
130 136 break;
131 137
132 138 a[i].e = j; /* use equivalence class for quick compare */
133 if(l[j] <= t)
134 a[i].n = h[j]; /* point to head of match list */
139 if(h[j].len <= t)
140 a[i].n = h[j].pos; /* point to head of match list */
135 141 else
136 142 a[i].n = -1; /* too popular */
137 143 }
138 144
139 145 /* discard hash tables */
140 146 free(h);
141 free(l);
142 147 return 1;
143 148 }
144 149
145 static int longest_match(struct line *a, struct line *b, int *jpos, int *jlen,
150 static int longest_match(struct line *a, struct line *b, struct pos *pos,
146 151 int a1, int a2, int b1, int b2, int *omi, int *omj)
147 152 {
148 153 int mi = a1, mj = b1, mk = 0, mb = 0, i, j, k;
@@ -155,12 +160,12 static int longest_match(struct line *a,
155 160 /* loop through all lines match a[i] in b */
156 161 for (; j != -1 && j < b2; j = b[j].n) {
157 162 /* does this extend an earlier match? */
158 if (i > a1 && j > b1 && jpos[j - 1] == i - 1)
159 k = jlen[j - 1] + 1;
163 if (i > a1 && j > b1 && pos[j - 1].pos == i - 1)
164 k = pos[j - 1].len + 1;
160 165 else
161 166 k = 1;
162 jpos[j] = i;
163 jlen[j] = k;
167 pos[j].pos = i;
168 pos[j].len = k;
164 169
165 170 /* best match so far? */
166 171 if (k > mk) {
@@ -189,47 +194,46 static int longest_match(struct line *a,
189 194 return mk + mb;
190 195 }
191 196
192 static void recurse(struct line *a, struct line *b, int *jpos, int *jlen,
197 static void recurse(struct line *a, struct line *b, struct pos *pos,
193 198 int a1, int a2, int b1, int b2, struct hunklist *l)
194 199 {
195 200 int i, j, k;
196 201
197 202 /* find the longest match in this chunk */
198 k = longest_match(a, b, jpos, jlen, a1, a2, b1, b2, &i, &j);
203 k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j);
199 204 if (!k)
200 205 return;
201 206
202 207 /* and recurse on the remaining chunks on either side */
203 recurse(a, b, jpos, jlen, a1, i, b1, j, l);
208 recurse(a, b, pos, a1, i, b1, j, l);
204 209 l->head->a1 = i;
205 210 l->head->a2 = i + k;
206 211 l->head->b1 = j;
207 212 l->head->b2 = j + k;
208 213 l->head++;
209 recurse(a, b, jpos, jlen, i + k, a2, j + k, b2, l);
214 recurse(a, b, pos, i + k, a2, j + k, b2, l);
210 215 }
211 216
212 217 static struct hunklist diff(struct line *a, int an, struct line *b, int bn)
213 218 {
214 219 struct hunklist l;
215 int *jpos, *jlen, t;
220 struct pos *pos;
221 int t;
216 222
217 223 /* allocate and fill arrays */
218 224 t = equatelines(a, an, b, bn);
219 jpos = calloc(bn, sizeof(int));
220 jlen = calloc(bn, sizeof(int));
225 pos = calloc(bn, sizeof(struct pos));
221 226 l.head = l.base = malloc(sizeof(struct hunk) * ((an + bn) / 4 + 2));
222 227
223 if (jpos && jlen && l.base && t) {
228 if (pos && l.base && t) {
224 229 /* generate the matching block list */
225 recurse(a, b, jpos, jlen, 0, an, 0, bn, &l);
230 recurse(a, b, pos, 0, an, 0, bn, &l);
226 231 l.head->a1 = an;
227 232 l.head->b1 = bn;
228 233 l.head++;
229 234 }
230 235
231 free(jpos);
232 free(jlen);
236 free(pos);
233 237 return l;
234 238 }
235 239
General Comments 0
You need to be logged in to leave comments. Login now