diffcore-delta.c 5.18 KB
Newer Older
1 2 3
#include "cache.h"
#include "diff.h"
#include "diffcore.h"
4 5 6 7

/*
 * Idea here is very simple.
 *
8 9 10
 * Almost all data we are interested in are text, but sometimes we have
 * to deal with binary data.  So we cut them into chunks delimited by
 * LF byte, or 64-byte sequence, whichever comes first, and hash them.
11
 *
12 13 14 15 16 17
 * For those chunks, 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.
18 19 20 21 22 23
 *
 * 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.
 */

24
/* Wild guess at the initial hash size */
25
#define INITIAL_HASH_SIZE 9
26

27 28 29 30
/* 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))
31

32 33 34 35 36 37 38
/* 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

39
struct spanhash {
40 41
	unsigned int hashval;
	unsigned int cnt;
42 43 44 45 46 47 48
};
struct spanhash_top {
	int alloc_log2;
	int free;
	struct spanhash data[FLEX_ARRAY];
};

49
static struct spanhash *spanhash_find(struct spanhash_top *top,
50
				      unsigned int hashval)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
{
	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;
74
	new->free = INITIAL_FREE(new->alloc_log2);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	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,
99
					 unsigned int hashval, int cnt)
100 101 102 103 104 105 106 107 108 109
{
	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;
110
			h->cnt = cnt;
111 112 113 114 115 116
			top->free--;
			if (top->free < 0)
				return spanhash_rehash(top);
			return top;
		}
		if (h->hashval == hashval) {
117
			h->cnt += cnt;
118 119 120 121 122 123 124
			return top;
		}
		if (lim <= bucket)
			bucket = 0;
	}
}

125
static struct spanhash_top *hash_chars(struct diff_filespec *one)
126
{
127
	int i, n;
128
	unsigned int accum1, accum2, hashval;
129
	struct spanhash_top *hash;
130 131
	unsigned char *buf = one->data;
	unsigned int sz = one->size;
132
	int is_text = !diff_filespec_is_binary(one);
133 134 135 136

	i = INITIAL_HASH_SIZE;
	hash = xmalloc(sizeof(*hash) + sizeof(struct spanhash) * (1<<i));
	hash->alloc_log2 = i;
137
	hash->free = INITIAL_FREE(i);
138
	memset(hash->data, 0, sizeof(struct spanhash) * (1<<i));
139

140 141
	n = 0;
	accum1 = accum2 = 0;
142
	while (sz) {
143 144
		unsigned int c = *buf++;
		unsigned int old_1 = accum1;
145
		sz--;
146 147 148 149 150

		/* Ignore CR in CRLF sequence if text */
		if (is_text && c == '\r' && sz && *buf == '\n')
			continue;

151 152
		accum1 = (accum1 << 7) ^ (accum2 >> 25);
		accum2 = (accum2 << 7) ^ (old_1 >> 25);
153 154 155 156 157 158 159
		accum1 += c;
		if (++n < 64 && c != '\n')
			continue;
		hashval = (accum1 + accum2 * 0x61) % HASHBASE;
		hash = add_spanhash(hash, hashval, n);
		n = 0;
		accum1 = accum2 = 0;
160
	}
161
	return hash;
162 163
}

164 165
int diffcore_count_changes(struct diff_filespec *src,
			   struct diff_filespec *dst,
166 167
			   void **src_count_p,
			   void **dst_count_p,
168 169 170 171
			   unsigned long delta_limit,
			   unsigned long *src_copied,
			   unsigned long *literal_added)
{
172 173
	int i, ssz;
	struct spanhash_top *src_count, *dst_count;
174 175
	unsigned long sc, la;

176 177 178 179
	src_count = dst_count = NULL;
	if (src_count_p)
		src_count = *src_count_p;
	if (!src_count) {
180
		src_count = hash_chars(src);
181 182 183 184 185 186
		if (src_count_p)
			*src_count_p = src_count;
	}
	if (dst_count_p)
		dst_count = *dst_count_p;
	if (!dst_count) {
187
		dst_count = hash_chars(dst);
188 189 190
		if (dst_count_p)
			*dst_count_p = dst_count;
	}
191
	sc = la = 0;
192 193 194 195 196 197 198 199 200 201 202 203 204 205

	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;
206
		}
207 208
		else
			sc += dst_cnt;
209
	}
210 211 212 213 214

	if (!src_count_p)
		free(src_count);
	if (!dst_count_p)
		free(dst_count);
215 216 217
	*src_copied = sc;
	*literal_added = la;
	return 0;
218
}