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