]> git.ipfire.org Git - thirdparty/git.git/blame - diffcore-delta.c
GIT 1.5.3.4
[thirdparty/git.git] / diffcore-delta.c
CommitLineData
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 39struct spanhash {
e31c9f24
LT
40 unsigned int hashval;
41 unsigned int cnt;
c06c7966
JH
42};
43struct spanhash_top {
44 int alloc_log2;
45 int free;
46 struct spanhash data[FLEX_ARRAY];
47};
48
2821104d 49static struct spanhash *spanhash_find(struct spanhash_top *top,
e31c9f24 50 unsigned int hashval)
c06c7966
JH
51{
52 int sz = 1 << top->alloc_log2;
53 int bucket = hashval & (sz - 1);
54 while (1) {
55 struct spanhash *h = &(top->data[bucket++]);
56 if (!h->cnt)
57 return NULL;
58 if (h->hashval == hashval)
59 return h;
60 if (sz <= bucket)
61 bucket = 0;
62 }
63}
64
65static struct spanhash_top *spanhash_rehash(struct spanhash_top *orig)
66{
67 struct spanhash_top *new;
68 int i;
69 int osz = 1 << orig->alloc_log2;
70 int sz = osz << 1;
71
72 new = xmalloc(sizeof(*orig) + sizeof(struct spanhash) * sz);
73 new->alloc_log2 = orig->alloc_log2 + 1;
2821104d 74 new->free = INITIAL_FREE(new->alloc_log2);
c06c7966
JH
75 memset(new->data, 0, sizeof(struct spanhash) * sz);
76 for (i = 0; i < osz; i++) {
77 struct spanhash *o = &(orig->data[i]);
78 int bucket;
79 if (!o->cnt)
80 continue;
81 bucket = o->hashval & (sz - 1);
82 while (1) {
83 struct spanhash *h = &(new->data[bucket++]);
84 if (!h->cnt) {
85 h->hashval = o->hashval;
86 h->cnt = o->cnt;
87 new->free--;
88 break;
89 }
90 if (sz <= bucket)
91 bucket = 0;
92 }
93 }
94 free(orig);
95 return new;
96}
97
98static struct spanhash_top *add_spanhash(struct spanhash_top *top,
e31c9f24 99 unsigned int hashval, int cnt)
c06c7966
JH
100{
101 int bucket, lim;
102 struct spanhash *h;
103
104 lim = (1 << top->alloc_log2);
105 bucket = hashval & (lim - 1);
106 while (1) {
107 h = &(top->data[bucket++]);
108 if (!h->cnt) {
109 h->hashval = hashval;
3c7ceba4 110 h->cnt = cnt;
c06c7966
JH
111 top->free--;
112 if (top->free < 0)
113 return spanhash_rehash(top);
114 return top;
115 }
116 if (h->hashval == hashval) {
3c7ceba4 117 h->cnt += cnt;
c06c7966
JH
118 return top;
119 }
120 if (lim <= bucket)
121 bucket = 0;
122 }
123}
124
b9905fed 125static struct spanhash_top *hash_chars(struct diff_filespec *one)
65416758 126{
3c7ceba4 127 int i, n;
e31c9f24 128 unsigned int accum1, accum2, hashval;
c06c7966 129 struct spanhash_top *hash;
b9905fed
JH
130 unsigned char *buf = one->data;
131 unsigned int sz = one->size;
29a3eefd 132 int is_text = !diff_filespec_is_binary(one);
c06c7966
JH
133
134 i = INITIAL_HASH_SIZE;
135 hash = xmalloc(sizeof(*hash) + sizeof(struct spanhash) * (1<<i));
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 }
c06c7966 161 return hash;
65416758
JH
162}
163
d8c3d03a
JH
164int diffcore_count_changes(struct diff_filespec *src,
165 struct diff_filespec *dst,
c06c7966
JH
166 void **src_count_p,
167 void **dst_count_p,
65416758
JH
168 unsigned long delta_limit,
169 unsigned long *src_copied,
170 unsigned long *literal_added)
171{
c06c7966
JH
172 int i, ssz;
173 struct spanhash_top *src_count, *dst_count;
ba23bbc8
JH
174 unsigned long sc, la;
175
c06c7966
JH
176 src_count = dst_count = NULL;
177 if (src_count_p)
178 src_count = *src_count_p;
179 if (!src_count) {
b9905fed 180 src_count = hash_chars(src);
c06c7966
JH
181 if (src_count_p)
182 *src_count_p = src_count;
183 }
184 if (dst_count_p)
185 dst_count = *dst_count_p;
186 if (!dst_count) {
b9905fed 187 dst_count = hash_chars(dst);
c06c7966
JH
188 if (dst_count_p)
189 *dst_count_p = dst_count;
190 }
ba23bbc8 191 sc = la = 0;
c06c7966
JH
192
193 ssz = 1 << src_count->alloc_log2;
194 for (i = 0; i < ssz; i++) {
195 struct spanhash *s = &(src_count->data[i]);
196 struct spanhash *d;
197 unsigned dst_cnt, src_cnt;
198 if (!s->cnt)
199 continue;
200 src_cnt = s->cnt;
201 d = spanhash_find(dst_count, s->hashval);
202 dst_cnt = d ? d->cnt : 0;
203 if (src_cnt < dst_cnt) {
204 la += dst_cnt - src_cnt;
205 sc += src_cnt;
ba23bbc8 206 }
c06c7966
JH
207 else
208 sc += dst_cnt;
ba23bbc8 209 }
c06c7966
JH
210
211 if (!src_count_p)
212 free(src_count);
213 if (!dst_count_p)
214 free(dst_count);
ba23bbc8
JH
215 *src_copied = sc;
216 *literal_added = la;
ba23bbc8 217 return 0;
65416758 218}