Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 234 lines (213 sloc) 5.488 kB
6541675 diffcore-rename: split out the delta counting code.
Junio C Hamano authored
1 #include "cache.h"
2 #include "diff.h"
3 #include "diffcore.h"
ba23bbc diffcore-delta: make change counter to byte oriented again.
Junio C Hamano authored
4
5 /*
6 * Idea here is very simple.
7 *
af3abef @gitster diffcore-delta.c: update the comment on the algorithm.
gitster authored
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.
ba23bbc diffcore-delta: make change counter to byte oriented again.
Junio C Hamano authored
11 *
af3abef @gitster diffcore-delta.c: update the comment on the algorithm.
gitster authored
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.
ba23bbc diffcore-delta: make change counter to byte oriented again.
Junio C Hamano authored
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.
22 */
23
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
24 /* Wild guess at the initial hash size */
2821104 diffcore-delta: make the hash a bit denser.
Junio C Hamano authored
25 #define INITIAL_HASH_SIZE 9
fc66d21 diffcore-delta: tweak hashbase value.
Junio C Hamano authored
26
2821104 diffcore-delta: make the hash a bit denser.
Junio C Hamano authored
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))
ba23bbc diffcore-delta: make change counter to byte oriented again.
Junio C Hamano authored
31
fc66d21 diffcore-delta: tweak hashbase value.
Junio C Hamano authored
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
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
39 struct spanhash {
e31c9f2 diffcore-delta: 64-byte-or-EOL ultrafast replacement (hash fix).
Linus Torvalds authored
40 unsigned int hashval;
41 unsigned int cnt;
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
42 };
43 struct spanhash_top {
44 int alloc_log2;
45 int free;
46 struct spanhash data[FLEX_ARRAY];
47 };
48
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
56 new = xmalloc(sizeof(*orig) + sizeof(struct spanhash) * sz);
57 new->alloc_log2 = orig->alloc_log2 + 1;
2821104 diffcore-delta: make the hash a bit denser.
Junio C Hamano authored
58 new->free = INITIAL_FREE(new->alloc_log2);
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
59 memset(new->data, 0, sizeof(struct spanhash) * sz);
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) {
67 struct spanhash *h = &(new->data[bucket++]);
68 if (!h->cnt) {
69 h->hashval = o->hashval;
70 h->cnt = o->cnt;
71 new->free--;
72 break;
73 }
74 if (sz <= bucket)
75 bucket = 0;
76 }
77 }
78 free(orig);
79 return new;
80 }
81
82 static struct spanhash_top *add_spanhash(struct spanhash_top *top,
e31c9f2 diffcore-delta: 64-byte-or-EOL ultrafast replacement (hash fix).
Linus Torvalds authored
83 unsigned int hashval, int cnt)
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
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;
3c7ceba diffcore-delta: 64-byte-or-EOL ultrafast replacement.
Linus Torvalds authored
94 h->cnt = cnt;
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
95 top->free--;
96 if (top->free < 0)
97 return spanhash_rehash(top);
98 return top;
99 }
100 if (h->hashval == hashval) {
3c7ceba diffcore-delta: 64-byte-or-EOL ultrafast replacement.
Linus Torvalds authored
101 h->cnt += cnt;
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
102 return top;
103 }
104 if (lim <= bucket)
105 bucket = 0;
106 }
107 }
108
eb4d0e3 @torvalds optimize diffcore-delta by sorting hash entries.
torvalds authored
109 static 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
b9905fe @gitster diffcore-delta.c: Ignore CR in CRLF for text files
gitster authored
123 static struct spanhash_top *hash_chars(struct diff_filespec *one)
6541675 diffcore-rename: split out the delta counting code.
Junio C Hamano authored
124 {
3c7ceba diffcore-delta: 64-byte-or-EOL ultrafast replacement.
Linus Torvalds authored
125 int i, n;
e31c9f2 diffcore-delta: 64-byte-or-EOL ultrafast replacement (hash fix).
Linus Torvalds authored
126 unsigned int accum1, accum2, hashval;
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
127 struct spanhash_top *hash;
b9905fe @gitster diffcore-delta.c: Ignore CR in CRLF for text files
gitster authored
128 unsigned char *buf = one->data;
129 unsigned int sz = one->size;
29a3eef @gitster Introduce diff_filespec_is_binary()
gitster authored
130 int is_text = !diff_filespec_is_binary(one);
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
131
132 i = INITIAL_HASH_SIZE;
133 hash = xmalloc(sizeof(*hash) + sizeof(struct spanhash) * (1<<i));
134 hash->alloc_log2 = i;
2821104 diffcore-delta: make the hash a bit denser.
Junio C Hamano authored
135 hash->free = INITIAL_FREE(i);
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
136 memset(hash->data, 0, sizeof(struct spanhash) * (1<<i));
6541675 diffcore-rename: split out the delta counting code.
Junio C Hamano authored
137
3c7ceba diffcore-delta: 64-byte-or-EOL ultrafast replacement.
Linus Torvalds authored
138 n = 0;
139 accum1 = accum2 = 0;
ba23bbc diffcore-delta: make change counter to byte oriented again.
Junio C Hamano authored
140 while (sz) {
e31c9f2 diffcore-delta: 64-byte-or-EOL ultrafast replacement (hash fix).
Linus Torvalds authored
141 unsigned int c = *buf++;
142 unsigned int old_1 = accum1;
ba23bbc diffcore-delta: make change counter to byte oriented again.
Junio C Hamano authored
143 sz--;
b9905fe @gitster diffcore-delta.c: Ignore CR in CRLF for text files
gitster authored
144
145 /* Ignore CR in CRLF sequence if text */
146 if (is_text && c == '\r' && sz && *buf == '\n')
147 continue;
148
e31c9f2 diffcore-delta: 64-byte-or-EOL ultrafast replacement (hash fix).
Linus Torvalds authored
149 accum1 = (accum1 << 7) ^ (accum2 >> 25);
150 accum2 = (accum2 << 7) ^ (old_1 >> 25);
3c7ceba diffcore-delta: 64-byte-or-EOL ultrafast replacement.
Linus Torvalds authored
151 accum1 += c;
152 if (++n < 64 && c != '\n')
153 continue;
154 hashval = (accum1 + accum2 * 0x61) % HASHBASE;
155 hash = add_spanhash(hash, hashval, n);
156 n = 0;
157 accum1 = accum2 = 0;
6541675 diffcore-rename: split out the delta counting code.
Junio C Hamano authored
158 }
eb4d0e3 @torvalds optimize diffcore-delta by sorting hash entries.
torvalds authored
159 qsort(hash->data,
160 1ul << hash->alloc_log2,
161 sizeof(hash->data[0]),
162 spanhash_cmp);
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
163 return hash;
6541675 diffcore-rename: split out the delta counting code.
Junio C Hamano authored
164 }
165
d8c3d03 @gitster diffcore_count_changes: pass diffcore_filespec
gitster authored
166 int diffcore_count_changes(struct diff_filespec *src,
167 struct diff_filespec *dst,
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
168 void **src_count_p,
169 void **dst_count_p,
6541675 diffcore-rename: split out the delta counting code.
Junio C Hamano authored
170 unsigned long delta_limit,
171 unsigned long *src_copied,
172 unsigned long *literal_added)
173 {
eb4d0e3 @torvalds optimize diffcore-delta by sorting hash entries.
torvalds authored
174 struct spanhash *s, *d;
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
175 struct spanhash_top *src_count, *dst_count;
ba23bbc diffcore-delta: make change counter to byte oriented again.
Junio C Hamano authored
176 unsigned long sc, la;
177
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
178 src_count = dst_count = NULL;
179 if (src_count_p)
180 src_count = *src_count_p;
181 if (!src_count) {
b9905fe @gitster diffcore-delta.c: Ignore CR in CRLF for text files
gitster authored
182 src_count = hash_chars(src);
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
183 if (src_count_p)
184 *src_count_p = src_count;
185 }
186 if (dst_count_p)
187 dst_count = *dst_count_p;
188 if (!dst_count) {
b9905fe @gitster diffcore-delta.c: Ignore CR in CRLF for text files
gitster authored
189 dst_count = hash_chars(dst);
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
190 if (dst_count_p)
191 *dst_count_p = dst_count;
192 }
ba23bbc diffcore-delta: make change counter to byte oriented again.
Junio C Hamano authored
193 sc = la = 0;
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
194
eb4d0e3 @torvalds optimize diffcore-delta by sorting hash entries.
torvalds authored
195 s = src_count->data;
196 d = dst_count->data;
197 for (;;) {
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
198 unsigned dst_cnt, src_cnt;
199 if (!s->cnt)
eb4d0e3 @torvalds optimize diffcore-delta by sorting hash entries.
torvalds authored
200 break; /* we checked all in src */
201 while (d->cnt) {
202 if (d->hashval >= s->hashval)
203 break;
77cd6ab @torvalds Fix diff -B/--dirstat miscounting of newly added contents
torvalds authored
204 la += d->cnt;
eb4d0e3 @torvalds optimize diffcore-delta by sorting hash entries.
torvalds authored
205 d++;
206 }
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
207 src_cnt = s->cnt;
77cd6ab @torvalds Fix diff -B/--dirstat miscounting of newly added contents
torvalds authored
208 dst_cnt = 0;
209 if (d->cnt && d->hashval == s->hashval) {
210 dst_cnt = d->cnt;
211 d++;
212 }
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
213 if (src_cnt < dst_cnt) {
214 la += dst_cnt - src_cnt;
215 sc += src_cnt;
ba23bbc diffcore-delta: make change counter to byte oriented again.
Junio C Hamano authored
216 }
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
217 else
218 sc += dst_cnt;
eb4d0e3 @torvalds optimize diffcore-delta by sorting hash entries.
torvalds authored
219 s++;
ba23bbc diffcore-delta: make change counter to byte oriented again.
Junio C Hamano authored
220 }
77cd6ab @torvalds Fix diff -B/--dirstat miscounting of newly added contents
torvalds authored
221 while (d->cnt) {
222 la += d->cnt;
223 d++;
224 }
c06c796 diffcore-rename: somewhat optimized.
Junio C Hamano authored
225
226 if (!src_count_p)
227 free(src_count);
228 if (!dst_count_p)
229 free(dst_count);
ba23bbc diffcore-delta: make change counter to byte oriented again.
Junio C Hamano authored
230 *src_copied = sc;
231 *literal_added = la;
232 return 0;
6541675 diffcore-rename: split out the delta counting code.
Junio C Hamano authored
233 }
Something went wrong with that request. Please try again.