@matheusbsbs / matheus-bagliano / commits / 38fd0721d0
Fork 0

diff-delta: allow reusing of the reference buffer index

When a reference buffer is used multiple times then its index can be computed only once and reused multiple times. This patch adds an extra pointer to a pointer argument (from_index) to diff_delta() for this. If from_index is NULL then everything is like before. If from_index is non NULL and *from_index is NULL then the index is created and its location stored to *from_index. In this case the caller has the responsibility to free the memory pointed to by *from_index. If from_index and *from_index are non NULL then the index is reused as is. This currently saves about 10% of CPU time to repack the git archive. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Feb 27, 2006 at 23:38 UTC 38fd0721d0a2a1a723bc28fc0817e3571987b1ef
6 files changed +40 -21
delta.h
+2 -1
@@ -4,7 +4,8 @@
4 /* handling of delta buffers */
5 extern void *diff_delta(void *from_buf, unsigned long from_size,
6 void *to_buf, unsigned long to_size,
7 - unsigned long *delta_size, unsigned long max_size);
7 + unsigned long *delta_size, unsigned long max_size,
8 + void **from_index);
9 extern void *patch_delta(void *src_buf, unsigned long src_size,
10 void *delta_buf, unsigned long delta_size,
11 unsigned long *dst_size);
diff-delta.c
+27 -14
@@ -30,8 +30,7 @@ struct index {
30
31 static struct index ** delta_index(const unsigned char *buf,
32 unsigned long bufsize,
33 - unsigned long trg_bufsize,
34 - unsigned int *hash_shift)
33 + unsigned long trg_bufsize)
34 {
35 unsigned long hsize;
36 unsigned int i, hshift, hlimit, *hash_count;
@@ -44,14 +43,17 @@ static struct index ** delta_index(const unsigned char *buf,
43 for (i = 8; (1 << i) < hsize && i < 24; i += 2);
44 hsize = 1 << i;
45 hshift = (i - 8) / 2;
47 - *hash_shift = hshift;
46
49 - /* allocate lookup index */
50 - mem = malloc(hsize * sizeof(*hash) + bufsize * sizeof(*entry));
47 + /*
48 + * Allocate lookup index. Note the first hash pointer
49 + * is used to store the hash shift value.
50 + */
51 + mem = malloc((1 + hsize) * sizeof(*hash) + bufsize * sizeof(*entry));
52 if (!mem)
53 return NULL;
54 hash = mem;
54 - entry = mem + hsize * sizeof(*hash);
55 + *hash++ = (void *)hshift;
56 + entry = mem + (1 + hsize) * sizeof(*hash);
57 memset(hash, 0, hsize * sizeof(*hash));
58
59 /* allocate an array to count hash entries */
@@ -107,7 +109,7 @@ static struct index ** delta_index(const unsigned char *buf,
109 }
110 free(hash_count);
111
110 - return hash;
112 + return hash-1;
113 }
114
115 /* provide the size of the copy opcode given the block offset and size */
@@ -121,7 +123,8 @@ static struct index ** delta_index(const unsigned char *buf,
123 void *diff_delta(void *from_buf, unsigned long from_size,
124 void *to_buf, unsigned long to_size,
125 unsigned long *delta_size,
124 - unsigned long max_size)
126 + unsigned long max_size,
127 + void **from_index)
128 {
129 unsigned int i, outpos, outsize, inscnt, hash_shift;
130 const unsigned char *ref_data, *ref_top, *data, *top;
@@ -130,9 +133,16 @@ void *diff_delta(void *from_buf, unsigned long from_size,
133
134 if (!from_size || !to_size)
135 return NULL;
133 - hash = delta_index(from_buf, from_size, to_size, &hash_shift);
134 - if (!hash)
135 - return NULL;
136 + if (from_index && *from_index) {
137 + hash = *from_index;
138 + } else {
139 + hash = delta_index(from_buf, from_size, to_size);
140 + if (!hash)
141 + return NULL;
142 + if (from_index)
143 + *from_index = hash;
144 + }
145 + hash_shift = (unsigned int)(*hash++);
146
147 outpos = 0;
148 outsize = 8192;
@@ -140,7 +150,8 @@ void *diff_delta(void *from_buf, unsigned long from_size,
150 outsize = max_size + MAX_OP_SIZE + 1;
151 out = malloc(outsize);
152 if (!out) {
143 - free(hash);
153 + if (!from_index)
154 + free(hash-1);
155 return NULL;
156 }
157
@@ -241,7 +252,8 @@ void *diff_delta(void *from_buf, unsigned long from_size,
252 out = realloc(out, outsize);
253 if (!out) {
254 free(tmp);
244 - free(hash);
255 + if (!from_index)
256 + free(hash-1);
257 return NULL;
258 }
259 }
@@ -250,7 +262,8 @@ void *diff_delta(void *from_buf, unsigned long from_size,
262 if (inscnt)
263 out[outpos - inscnt - 1] = inscnt;
264
253 - free(hash);
265 + if (!from_index)
266 + free(hash-1);
267 *delta_size = outpos;
268 return out;
269 }
diffcore-break.c
+1 -1
@@ -71,7 +71,7 @@ static int should_break(struct diff_filespec *src,
71
72 delta = diff_delta(src->data, src->size,
73 dst->data, dst->size,
74 - &delta_size, 0);
74 + &delta_size, 0, NULL);
75 if (!delta)
76 return 0; /* error but caught downstream */
77
diffcore-rename.c
+1 -1
@@ -168,7 +168,7 @@ static int estimate_similarity(struct diff_filespec *src,
168 delta_limit = base_size * (MAX_SCORE-minimum_score) / MAX_SCORE;
169 delta = diff_delta(src->data, src->size,
170 dst->data, dst->size,
171 - &delta_size, delta_limit);
171 + &delta_size, delta_limit, NULL);
172 if (!delta)
173 /* If delta_limit is exceeded, we have too much differences */
174 return 0;
pack-objects.c
+8 -3
@@ -204,7 +204,7 @@ static void *delta_against(void *buf, unsigned long size, struct object_entry *e
204 if (!otherbuf)
205 die("unable to read %s", sha1_to_hex(entry->delta->sha1));
206 delta_buf = diff_delta(otherbuf, othersize,
207 - buf, size, &delta_size, 0);
207 + buf, size, &delta_size, 0, NULL);
208 if (!delta_buf || delta_size != entry->delta_size)
209 die("delta size changed");
210 free(buf);
@@ -810,6 +810,7 @@ static int type_size_sort(const struct object_entry *a, const struct object_entr
810 struct unpacked {
811 struct object_entry *entry;
812 void *data;
813 + void **delta_index;
814 };
815
816 /*
@@ -891,7 +892,8 @@ static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_de
892 if (sizediff >= max_size)
893 return -1;
894 delta_buf = diff_delta(old->data, oldsize,
894 - cur->data, size, &delta_size, max_size);
895 + cur->data, size, &delta_size,
896 + max_size, old->delta_index);
897 if (!delta_buf)
898 return 0;
899 cur_entry->delta = old_entry;
@@ -948,6 +950,7 @@ static void find_deltas(struct object_entry **list, int window, int depth)
950 */
951 continue;
952
953 + free(n->delta_index);
954 free(n->data);
955 n->entry = entry;
956 n->data = read_sha1_file(entry->sha1, type, &size);
@@ -974,8 +977,10 @@ static void find_deltas(struct object_entry **list, int window, int depth)
977 if (progress)
978 fputc('\n', stderr);
979
977 - for (i = 0; i < window; ++i)
980 + for (i = 0; i < window; ++i) {
981 + free(array[i].delta_index);
982 free(array[i].data);
983 + }
984 free(array);
985 }
986
test-delta.c
+1 -1
@@ -63,7 +63,7 @@ int main(int argc, char *argv[])
63 if (argv[1][1] == 'd')
64 out_buf = diff_delta(from_buf, from_size,
65 data_buf, data_size,
66 - &out_size, 0);
66 + &out_size, 0, NULL);
67 else
68 out_buf = patch_delta(from_buf, from_size,
69 data_buf, data_size,