2006-03-01 08:01:36 +08:00
|
|
|
#include "cache.h"
|
|
|
|
#include "diff.h"
|
|
|
|
#include "diffcore.h"
|
2006-03-04 19:21:55 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Idea here is very simple.
|
|
|
|
*
|
|
|
|
* We have total of (sz-N+1) N-byte overlapping sequences in buf whose
|
|
|
|
* size is sz. If the same N-byte sequence appears in both source and
|
|
|
|
* destination, we say the byte that starts that sequence is shared
|
|
|
|
* between them (i.e. copied from source to destination).
|
|
|
|
*
|
|
|
|
* For each possible N-byte sequence, if the source buffer has more
|
|
|
|
* instances of it than the destination buffer, that means the
|
|
|
|
* difference are the number of bytes not copied from source to
|
|
|
|
* destination. If the counts are the same, everything was copied
|
|
|
|
* from source to destination. If the destination has more,
|
|
|
|
* everything was copied, and destination added more.
|
|
|
|
*
|
|
|
|
* We are doing an approximation so we do not really have to waste
|
|
|
|
* memory by actually storing the sequence. We just hash them into
|
|
|
|
* somewhere around 2^16 hashbuckets and count the occurrences.
|
|
|
|
*
|
|
|
|
* The length of the sequence is arbitrarily set to 8 for now.
|
|
|
|
*/
|
|
|
|
|
2006-03-12 19:22:10 +08:00
|
|
|
/* Wild guess at the initial hash size */
|
2006-03-13 08:39:51 +08:00
|
|
|
#define INITIAL_HASH_SIZE 9
|
2006-03-13 12:32:06 +08:00
|
|
|
|
2006-03-13 08:39:51 +08:00
|
|
|
/* We leave more room in smaller hash but do not let it
|
|
|
|
* grow to have unused hole too much.
|
|
|
|
*/
|
|
|
|
#define INITIAL_FREE(sz_log2) ((1<<(sz_log2))*(sz_log2-3)/(sz_log2))
|
2006-03-04 19:21:55 +08:00
|
|
|
|
2006-03-13 12:32:06 +08:00
|
|
|
/* A prime rather carefully chosen between 2^16..2^17, so that
|
|
|
|
* HASHBASE < INITIAL_FREE(17). We want to keep the maximum hashtable
|
|
|
|
* size under the current 2<<17 maximum, which can hold this many
|
|
|
|
* different values before overflowing to hashtable of size 2<<18.
|
|
|
|
*/
|
|
|
|
#define HASHBASE 107927
|
|
|
|
|
2006-03-12 19:22:10 +08:00
|
|
|
struct spanhash {
|
2006-03-15 16:37:57 +08:00
|
|
|
unsigned int hashval;
|
|
|
|
unsigned int cnt;
|
2006-03-12 19:22:10 +08:00
|
|
|
};
|
|
|
|
struct spanhash_top {
|
|
|
|
int alloc_log2;
|
|
|
|
int free;
|
|
|
|
struct spanhash data[FLEX_ARRAY];
|
|
|
|
};
|
|
|
|
|
2006-03-13 08:39:51 +08:00
|
|
|
static struct spanhash *spanhash_find(struct spanhash_top *top,
|
2006-03-15 16:37:57 +08:00
|
|
|
unsigned int hashval)
|
2006-03-12 19:22:10 +08:00
|
|
|
{
|
|
|
|
int sz = 1 << top->alloc_log2;
|
|
|
|
int bucket = hashval & (sz - 1);
|
|
|
|
while (1) {
|
|
|
|
struct spanhash *h = &(top->data[bucket++]);
|
|
|
|
if (!h->cnt)
|
|
|
|
return NULL;
|
|
|
|
if (h->hashval == hashval)
|
|
|
|
return h;
|
|
|
|
if (sz <= bucket)
|
|
|
|
bucket = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct spanhash_top *spanhash_rehash(struct spanhash_top *orig)
|
|
|
|
{
|
|
|
|
struct spanhash_top *new;
|
|
|
|
int i;
|
|
|
|
int osz = 1 << orig->alloc_log2;
|
|
|
|
int sz = osz << 1;
|
|
|
|
|
|
|
|
new = xmalloc(sizeof(*orig) + sizeof(struct spanhash) * sz);
|
|
|
|
new->alloc_log2 = orig->alloc_log2 + 1;
|
2006-03-13 08:39:51 +08:00
|
|
|
new->free = INITIAL_FREE(new->alloc_log2);
|
2006-03-12 19:22:10 +08:00
|
|
|
memset(new->data, 0, sizeof(struct spanhash) * sz);
|
|
|
|
for (i = 0; i < osz; i++) {
|
|
|
|
struct spanhash *o = &(orig->data[i]);
|
|
|
|
int bucket;
|
|
|
|
if (!o->cnt)
|
|
|
|
continue;
|
|
|
|
bucket = o->hashval & (sz - 1);
|
|
|
|
while (1) {
|
|
|
|
struct spanhash *h = &(new->data[bucket++]);
|
|
|
|
if (!h->cnt) {
|
|
|
|
h->hashval = o->hashval;
|
|
|
|
h->cnt = o->cnt;
|
|
|
|
new->free--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sz <= bucket)
|
|
|
|
bucket = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(orig);
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct spanhash_top *add_spanhash(struct spanhash_top *top,
|
2006-03-15 16:37:57 +08:00
|
|
|
unsigned int hashval, int cnt)
|
2006-03-12 19:22:10 +08:00
|
|
|
{
|
|
|
|
int bucket, lim;
|
|
|
|
struct spanhash *h;
|
|
|
|
|
|
|
|
lim = (1 << top->alloc_log2);
|
|
|
|
bucket = hashval & (lim - 1);
|
|
|
|
while (1) {
|
|
|
|
h = &(top->data[bucket++]);
|
|
|
|
if (!h->cnt) {
|
|
|
|
h->hashval = hashval;
|
2006-03-15 16:37:57 +08:00
|
|
|
h->cnt = cnt;
|
2006-03-12 19:22:10 +08:00
|
|
|
top->free--;
|
|
|
|
if (top->free < 0)
|
|
|
|
return spanhash_rehash(top);
|
|
|
|
return top;
|
|
|
|
}
|
|
|
|
if (h->hashval == hashval) {
|
2006-03-15 16:37:57 +08:00
|
|
|
h->cnt += cnt;
|
2006-03-12 19:22:10 +08:00
|
|
|
return top;
|
|
|
|
}
|
|
|
|
if (lim <= bucket)
|
|
|
|
bucket = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-15 16:37:57 +08:00
|
|
|
static struct spanhash_top *hash_chars(unsigned char *buf, unsigned int sz)
|
2006-03-01 08:01:36 +08:00
|
|
|
{
|
2006-03-15 16:37:57 +08:00
|
|
|
int i, n;
|
2006-03-15 16:37:57 +08:00
|
|
|
unsigned int accum1, accum2, hashval;
|
2006-03-12 19:22:10 +08:00
|
|
|
struct spanhash_top *hash;
|
|
|
|
|
|
|
|
i = INITIAL_HASH_SIZE;
|
|
|
|
hash = xmalloc(sizeof(*hash) + sizeof(struct spanhash) * (1<<i));
|
|
|
|
hash->alloc_log2 = i;
|
2006-03-13 08:39:51 +08:00
|
|
|
hash->free = INITIAL_FREE(i);
|
2006-03-12 19:22:10 +08:00
|
|
|
memset(hash->data, 0, sizeof(struct spanhash) * (1<<i));
|
2006-03-01 08:01:36 +08:00
|
|
|
|
2006-03-15 16:37:57 +08:00
|
|
|
n = 0;
|
|
|
|
accum1 = accum2 = 0;
|
2006-03-04 19:21:55 +08:00
|
|
|
while (sz) {
|
2006-03-15 16:37:57 +08:00
|
|
|
unsigned int c = *buf++;
|
|
|
|
unsigned int old_1 = accum1;
|
2006-03-04 19:21:55 +08:00
|
|
|
sz--;
|
2006-03-15 16:37:57 +08:00
|
|
|
accum1 = (accum1 << 7) ^ (accum2 >> 25);
|
|
|
|
accum2 = (accum2 << 7) ^ (old_1 >> 25);
|
2006-03-15 16:37:57 +08:00
|
|
|
accum1 += c;
|
|
|
|
if (++n < 64 && c != '\n')
|
|
|
|
continue;
|
|
|
|
hashval = (accum1 + accum2 * 0x61) % HASHBASE;
|
|
|
|
hash = add_spanhash(hash, hashval, n);
|
|
|
|
n = 0;
|
|
|
|
accum1 = accum2 = 0;
|
2006-03-01 08:01:36 +08:00
|
|
|
}
|
2006-03-12 19:22:10 +08:00
|
|
|
return hash;
|
2006-03-01 08:01:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int diffcore_count_changes(void *src, unsigned long src_size,
|
|
|
|
void *dst, unsigned long dst_size,
|
2006-03-12 19:22:10 +08:00
|
|
|
void **src_count_p,
|
|
|
|
void **dst_count_p,
|
2006-03-01 08:01:36 +08:00
|
|
|
unsigned long delta_limit,
|
|
|
|
unsigned long *src_copied,
|
|
|
|
unsigned long *literal_added)
|
|
|
|
{
|
2006-03-12 19:22:10 +08:00
|
|
|
int i, ssz;
|
|
|
|
struct spanhash_top *src_count, *dst_count;
|
2006-03-04 19:21:55 +08:00
|
|
|
unsigned long sc, la;
|
|
|
|
|
2006-03-12 19:22:10 +08:00
|
|
|
src_count = dst_count = NULL;
|
|
|
|
if (src_count_p)
|
|
|
|
src_count = *src_count_p;
|
|
|
|
if (!src_count) {
|
|
|
|
src_count = hash_chars(src, src_size);
|
|
|
|
if (src_count_p)
|
|
|
|
*src_count_p = src_count;
|
|
|
|
}
|
|
|
|
if (dst_count_p)
|
|
|
|
dst_count = *dst_count_p;
|
|
|
|
if (!dst_count) {
|
|
|
|
dst_count = hash_chars(dst, dst_size);
|
|
|
|
if (dst_count_p)
|
|
|
|
*dst_count_p = dst_count;
|
|
|
|
}
|
2006-03-04 19:21:55 +08:00
|
|
|
sc = la = 0;
|
2006-03-12 19:22:10 +08:00
|
|
|
|
|
|
|
ssz = 1 << src_count->alloc_log2;
|
|
|
|
for (i = 0; i < ssz; i++) {
|
|
|
|
struct spanhash *s = &(src_count->data[i]);
|
|
|
|
struct spanhash *d;
|
|
|
|
unsigned dst_cnt, src_cnt;
|
|
|
|
if (!s->cnt)
|
|
|
|
continue;
|
|
|
|
src_cnt = s->cnt;
|
|
|
|
d = spanhash_find(dst_count, s->hashval);
|
|
|
|
dst_cnt = d ? d->cnt : 0;
|
|
|
|
if (src_cnt < dst_cnt) {
|
|
|
|
la += dst_cnt - src_cnt;
|
|
|
|
sc += src_cnt;
|
2006-03-04 19:21:55 +08:00
|
|
|
}
|
2006-03-12 19:22:10 +08:00
|
|
|
else
|
|
|
|
sc += dst_cnt;
|
2006-03-04 19:21:55 +08:00
|
|
|
}
|
2006-03-12 19:22:10 +08:00
|
|
|
|
|
|
|
if (!src_count_p)
|
|
|
|
free(src_count);
|
|
|
|
if (!dst_count_p)
|
|
|
|
free(dst_count);
|
2006-03-04 19:21:55 +08:00
|
|
|
*src_copied = sc;
|
|
|
|
*literal_added = la;
|
|
|
|
return 0;
|
2006-03-01 08:01:36 +08:00
|
|
|
}
|