]>
Commit | Line | Data |
---|---|---|
65416758 JH |
1 | #include "cache.h" |
2 | #include "diff.h" | |
3 | #include "diffcore.h" | |
ba23bbc8 JH |
4 | |
5 | /* | |
6 | * Idea here is very simple. | |
7 | * | |
af3abef9 JH |
8 | * Almost all data we are interested in are text, but sometimes we have |
9 | * to deal with binary data. So we cut them into chunks delimited by | |
10 | * LF byte, or 64-byte sequence, whichever comes first, and hash them. | |
ba23bbc8 | 11 | * |
af3abef9 JH |
12 | * For those chunks, if the source buffer has more instances of it |
13 | * than the destination buffer, that means the difference are the | |
14 | * number of bytes not copied from source to destination. If the | |
15 | * counts are the same, everything was copied from source to | |
16 | * destination. If the destination has more, everything was copied, | |
17 | * and destination added more. | |
ba23bbc8 JH |
18 | * |
19 | * We are doing an approximation so we do not really have to waste | |
20 | * memory by actually storing the sequence. We just hash them into | |
21 | * somewhere around 2^16 hashbuckets and count the occurrences. | |
ba23bbc8 JH |
22 | */ |
23 | ||
c06c7966 | 24 | /* Wild guess at the initial hash size */ |
2821104d | 25 | #define INITIAL_HASH_SIZE 9 |
fc66d213 | 26 | |
2821104d JH |
27 | /* We leave more room in smaller hash but do not let it |
28 | * grow to have unused hole too much. | |
29 | */ | |
30 | #define INITIAL_FREE(sz_log2) ((1<<(sz_log2))*(sz_log2-3)/(sz_log2)) | |
ba23bbc8 | 31 | |
fc66d213 JH |
32 | /* A prime rather carefully chosen between 2^16..2^17, so that |
33 | * HASHBASE < INITIAL_FREE(17). We want to keep the maximum hashtable | |
34 | * size under the current 2<<17 maximum, which can hold this many | |
35 | * different values before overflowing to hashtable of size 2<<18. | |
36 | */ | |
37 | #define HASHBASE 107927 | |
38 | ||
c06c7966 | 39 | struct spanhash { |
e31c9f24 LT |
40 | unsigned int hashval; |
41 | unsigned int cnt; | |
c06c7966 JH |
42 | }; |
43 | struct spanhash_top { | |
44 | int alloc_log2; | |
45 | int free; | |
46 | struct spanhash data[FLEX_ARRAY]; | |
47 | }; | |
48 | ||
c06c7966 JH |
49 | static struct spanhash_top *spanhash_rehash(struct spanhash_top *orig) |
50 | { | |
51 | struct spanhash_top *new; | |
52 | int i; | |
53 | int osz = 1 << orig->alloc_log2; | |
54 | int sz = osz << 1; | |
55 | ||
50a6c8ef JK |
56 | new = xmalloc(st_add(sizeof(*orig), |
57 | st_mult(sizeof(struct spanhash), sz))); | |
c06c7966 | 58 | new->alloc_log2 = orig->alloc_log2 + 1; |
2821104d | 59 | new->free = INITIAL_FREE(new->alloc_log2); |
c06c7966 JH |
60 | memset(new->data, 0, sizeof(struct spanhash) * sz); |
61 | for (i = 0; i < osz; i++) { | |
62 | struct spanhash *o = &(orig->data[i]); | |
63 | int bucket; | |
64 | if (!o->cnt) | |
65 | continue; | |
66 | bucket = o->hashval & (sz - 1); | |
67 | while (1) { | |
68 | struct spanhash *h = &(new->data[bucket++]); | |
69 | if (!h->cnt) { | |
70 | h->hashval = o->hashval; | |
71 | h->cnt = o->cnt; | |
72 | new->free--; | |
73 | break; | |
74 | } | |
75 | if (sz <= bucket) | |
76 | bucket = 0; | |
77 | } | |
78 | } | |
79 | free(orig); | |
80 | return new; | |
81 | } | |
82 | ||
83 | static struct spanhash_top *add_spanhash(struct spanhash_top *top, | |
e31c9f24 | 84 | unsigned int hashval, int cnt) |
c06c7966 JH |
85 | { |
86 | int bucket, lim; | |
87 | struct spanhash *h; | |
88 | ||
89 | lim = (1 << top->alloc_log2); | |
90 | bucket = hashval & (lim - 1); | |
91 | while (1) { | |
92 | h = &(top->data[bucket++]); | |
93 | if (!h->cnt) { | |
94 | h->hashval = hashval; | |
3c7ceba4 | 95 | h->cnt = cnt; |
c06c7966 JH |
96 | top->free--; |
97 | if (top->free < 0) | |
98 | return spanhash_rehash(top); | |
99 | return top; | |
100 | } | |
101 | if (h->hashval == hashval) { | |
3c7ceba4 | 102 | h->cnt += cnt; |
c06c7966 JH |
103 | return top; |
104 | } | |
105 | if (lim <= bucket) | |
106 | bucket = 0; | |
107 | } | |
108 | } | |
109 | ||
eb4d0e3f LT |
110 | static int spanhash_cmp(const void *a_, const void *b_) |
111 | { | |
112 | const struct spanhash *a = a_; | |
113 | const struct spanhash *b = b_; | |
114 | ||
115 | /* A count of zero compares at the end.. */ | |
116 | if (!a->cnt) | |
117 | return !b->cnt ? 0 : 1; | |
118 | if (!b->cnt) | |
119 | return -1; | |
120 | return a->hashval < b->hashval ? -1 : | |
121 | a->hashval > b->hashval ? 1 : 0; | |
122 | } | |
123 | ||
b9905fed | 124 | static struct spanhash_top *hash_chars(struct diff_filespec *one) |
65416758 | 125 | { |
3c7ceba4 | 126 | int i, n; |
e31c9f24 | 127 | unsigned int accum1, accum2, hashval; |
c06c7966 | 128 | struct spanhash_top *hash; |
b9905fed JH |
129 | unsigned char *buf = one->data; |
130 | unsigned int sz = one->size; | |
29a3eefd | 131 | int is_text = !diff_filespec_is_binary(one); |
c06c7966 JH |
132 | |
133 | i = INITIAL_HASH_SIZE; | |
50a6c8ef JK |
134 | hash = xmalloc(st_add(sizeof(*hash), |
135 | st_mult(sizeof(struct spanhash), 1<<i))); | |
c06c7966 | 136 | hash->alloc_log2 = i; |
2821104d | 137 | hash->free = INITIAL_FREE(i); |
c06c7966 | 138 | memset(hash->data, 0, sizeof(struct spanhash) * (1<<i)); |
65416758 | 139 | |
3c7ceba4 LT |
140 | n = 0; |
141 | accum1 = accum2 = 0; | |
ba23bbc8 | 142 | while (sz) { |
e31c9f24 LT |
143 | unsigned int c = *buf++; |
144 | unsigned int old_1 = accum1; | |
ba23bbc8 | 145 | sz--; |
b9905fed JH |
146 | |
147 | /* Ignore CR in CRLF sequence if text */ | |
148 | if (is_text && c == '\r' && sz && *buf == '\n') | |
149 | continue; | |
150 | ||
e31c9f24 LT |
151 | accum1 = (accum1 << 7) ^ (accum2 >> 25); |
152 | accum2 = (accum2 << 7) ^ (old_1 >> 25); | |
3c7ceba4 LT |
153 | accum1 += c; |
154 | if (++n < 64 && c != '\n') | |
155 | continue; | |
156 | hashval = (accum1 + accum2 * 0x61) % HASHBASE; | |
157 | hash = add_spanhash(hash, hashval, n); | |
158 | n = 0; | |
159 | accum1 = accum2 = 0; | |
65416758 | 160 | } |
9ed0d8d6 | 161 | QSORT(hash->data, 1ul << hash->alloc_log2, spanhash_cmp); |
c06c7966 | 162 | return hash; |
65416758 JH |
163 | } |
164 | ||
d8c3d03a JH |
165 | int diffcore_count_changes(struct diff_filespec *src, |
166 | struct diff_filespec *dst, | |
c06c7966 JH |
167 | void **src_count_p, |
168 | void **dst_count_p, | |
65416758 JH |
169 | unsigned long delta_limit, |
170 | unsigned long *src_copied, | |
171 | unsigned long *literal_added) | |
172 | { | |
eb4d0e3f | 173 | struct spanhash *s, *d; |
c06c7966 | 174 | struct spanhash_top *src_count, *dst_count; |
ba23bbc8 JH |
175 | unsigned long sc, la; |
176 | ||
c06c7966 JH |
177 | src_count = dst_count = NULL; |
178 | if (src_count_p) | |
179 | src_count = *src_count_p; | |
180 | if (!src_count) { | |
b9905fed | 181 | src_count = hash_chars(src); |
c06c7966 JH |
182 | if (src_count_p) |
183 | *src_count_p = src_count; | |
184 | } | |
185 | if (dst_count_p) | |
186 | dst_count = *dst_count_p; | |
187 | if (!dst_count) { | |
b9905fed | 188 | dst_count = hash_chars(dst); |
c06c7966 JH |
189 | if (dst_count_p) |
190 | *dst_count_p = dst_count; | |
191 | } | |
ba23bbc8 | 192 | sc = la = 0; |
c06c7966 | 193 | |
eb4d0e3f LT |
194 | s = src_count->data; |
195 | d = dst_count->data; | |
196 | for (;;) { | |
c06c7966 JH |
197 | unsigned dst_cnt, src_cnt; |
198 | if (!s->cnt) | |
eb4d0e3f LT |
199 | break; /* we checked all in src */ |
200 | while (d->cnt) { | |
201 | if (d->hashval >= s->hashval) | |
202 | break; | |
77cd6ab6 | 203 | la += d->cnt; |
eb4d0e3f LT |
204 | d++; |
205 | } | |
c06c7966 | 206 | src_cnt = s->cnt; |
77cd6ab6 LT |
207 | dst_cnt = 0; |
208 | if (d->cnt && d->hashval == s->hashval) { | |
209 | dst_cnt = d->cnt; | |
210 | d++; | |
211 | } | |
c06c7966 JH |
212 | if (src_cnt < dst_cnt) { |
213 | la += dst_cnt - src_cnt; | |
214 | sc += src_cnt; | |
ba23bbc8 | 215 | } |
c06c7966 JH |
216 | else |
217 | sc += dst_cnt; | |
eb4d0e3f | 218 | s++; |
ba23bbc8 | 219 | } |
77cd6ab6 LT |
220 | while (d->cnt) { |
221 | la += d->cnt; | |
222 | d++; | |
223 | } | |
c06c7966 JH |
224 | |
225 | if (!src_count_p) | |
226 | free(src_count); | |
227 | if (!dst_count_p) | |
228 | free(dst_count); | |
ba23bbc8 JH |
229 | *src_copied = sc; |
230 | *literal_added = la; | |
ba23bbc8 | 231 | return 0; |
65416758 | 232 | } |