6 * Idea here is very simple.
8 * We have total of (sz-N+1) N-byte overlapping sequences in buf whose
9 * size is sz. If the same N-byte sequence appears in both source and
10 * destination, we say the byte that starts that sequence is shared
11 * between them (i.e. copied from source to destination).
13 * For each possible N-byte sequence, if the source buffer has more
14 * instances of it than the destination buffer, that means the
15 * difference are the number of bytes not copied from source to
16 * destination. If the counts are the same, everything was copied
17 * from source to destination. If the destination has more,
18 * everything was copied, and destination added more.
20 * We are doing an approximation so we do not really have to waste
21 * memory by actually storing the sequence. We just hash them into
22 * somewhere around 2^16 hashbuckets and count the occurrences.
24 * The length of the sequence is arbitrarily set to 8 for now.
27 /* Wild guess at the initial hash size */
28 #define INITIAL_HASH_SIZE 9
30 /* We leave more room in smaller hash but do not let it
31 * grow to have unused hole too much.
33 #define INITIAL_FREE(sz_log2) ((1<<(sz_log2))*(sz_log2-3)/(sz_log2))
35 /* A prime rather carefully chosen between 2^16..2^17, so that
36 * HASHBASE < INITIAL_FREE(17). We want to keep the maximum hashtable
37 * size under the current 2<<17 maximum, which can hold this many
38 * different values before overflowing to hashtable of size 2<<18.
40 #define HASHBASE 107927
49 struct spanhash data
[FLEX_ARRAY
];
52 static struct spanhash
*spanhash_find(struct spanhash_top
*top
,
55 int sz
= 1 << top
->alloc_log2
;
56 int bucket
= hashval
& (sz
- 1);
58 struct spanhash
*h
= &(top
->data
[bucket
++]);
61 if (h
->hashval
== hashval
)
68 static struct spanhash_top
*spanhash_rehash(struct spanhash_top
*orig
)
70 struct spanhash_top
*new;
72 int osz
= 1 << orig
->alloc_log2
;
75 new = xmalloc(sizeof(*orig
) + sizeof(struct spanhash
) * sz
);
76 new->alloc_log2
= orig
->alloc_log2
+ 1;
77 new->free
= INITIAL_FREE(new->alloc_log2
);
78 memset(new->data
, 0, sizeof(struct spanhash
) * sz
);
79 for (i
= 0; i
< osz
; i
++) {
80 struct spanhash
*o
= &(orig
->data
[i
]);
84 bucket
= o
->hashval
& (sz
- 1);
86 struct spanhash
*h
= &(new->data
[bucket
++]);
88 h
->hashval
= o
->hashval
;
101 static struct spanhash_top
*add_spanhash(struct spanhash_top
*top
,
102 unsigned int hashval
, int cnt
)
107 lim
= (1 << top
->alloc_log2
);
108 bucket
= hashval
& (lim
- 1);
110 h
= &(top
->data
[bucket
++]);
112 h
->hashval
= hashval
;
116 return spanhash_rehash(top
);
119 if (h
->hashval
== hashval
) {
128 static struct spanhash_top
*hash_chars(unsigned char *buf
, unsigned int sz
)
131 unsigned int accum1
, accum2
, hashval
;
132 struct spanhash_top
*hash
;
134 i
= INITIAL_HASH_SIZE
;
135 hash
= xmalloc(sizeof(*hash
) + sizeof(struct spanhash
) * (1<<i
));
136 hash
->alloc_log2
= i
;
137 hash
->free
= INITIAL_FREE(i
);
138 memset(hash
->data
, 0, sizeof(struct spanhash
) * (1<<i
));
143 unsigned int c
= *buf
++;
144 unsigned int old_1
= accum1
;
146 accum1
= (accum1
<< 7) ^ (accum2
>> 25);
147 accum2
= (accum2
<< 7) ^ (old_1
>> 25);
149 if (++n
< 64 && c
!= '\n')
151 hashval
= (accum1
+ accum2
* 0x61) % HASHBASE
;
152 hash
= add_spanhash(hash
, hashval
, n
);
159 int diffcore_count_changes(void *src
, unsigned long src_size
,
160 void *dst
, unsigned long dst_size
,
163 unsigned long delta_limit
,
164 unsigned long *src_copied
,
165 unsigned long *literal_added
)
168 struct spanhash_top
*src_count
, *dst_count
;
169 unsigned long sc
, la
;
171 src_count
= dst_count
= NULL
;
173 src_count
= *src_count_p
;
175 src_count
= hash_chars(src
, src_size
);
177 *src_count_p
= src_count
;
180 dst_count
= *dst_count_p
;
182 dst_count
= hash_chars(dst
, dst_size
);
184 *dst_count_p
= dst_count
;
188 ssz
= 1 << src_count
->alloc_log2
;
189 for (i
= 0; i
< ssz
; i
++) {
190 struct spanhash
*s
= &(src_count
->data
[i
]);
192 unsigned dst_cnt
, src_cnt
;
196 d
= spanhash_find(dst_count
, s
->hashval
);
197 dst_cnt
= d
? d
->cnt
: 0;
198 if (src_cnt
< dst_cnt
) {
199 la
+= dst_cnt
- src_cnt
;