@matheusbsbs / matheus-bagliano / commits / 907380eeff
Fork 0

count-delta: tweak counting of copied source material.

With the finer grained delta algorithm, count-delta algorithm started overcounting copied source material, since the new delta output tends to reuse the same source range more than once and more aggressively. This broke an earlier assumption that the number of bytes copied out from the source buffer is a good approximation how much source material is actually remaining in the result. This uses fairly inefficient algorithm to keep track of ranges of source material that are actually copied out to the destination buffer. With this tweak, the obvious rename/break detection tests in the testsuite start to work again. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Feb 23, 2006 at 02:58 UTC 907380eeff27e9a07d6f1c03847c3d80f9e0e79a
1 file changed +71 -6
count-delta.c
+71 -6
@@ -3,11 +3,74 @@
3 * The delta-parsing part is almost straight copy of patch-delta.c
4 * which is (C) 2005 Nicolas Pitre <nico@cam.org>.
5 */
6 +#include "cache.h"
7 +#include "delta.h"
8 +#include "count-delta.h"
9 #include <stdlib.h>
10 #include <string.h>
11 #include <limits.h>
9 -#include "delta.h"
10 -#include "count-delta.h"
12 +
13 +struct span {
14 + struct span *next;
15 + unsigned long ofs;
16 + unsigned long end;
17 +};
18 +
19 +static void touch_range(struct span **span,
20 + unsigned long ofs, unsigned long end)
21 +{
22 + struct span *e = *span;
23 + struct span *p = NULL;
24 +
25 + while (e && e->ofs <= ofs) {
26 + again:
27 + if (ofs < e->end) {
28 + while (e->end < end) {
29 + if (e->next) {
30 + e->end = e->next->ofs;
31 + e = e->next;
32 + }
33 + else {
34 + e->end = end;
35 + return;
36 + }
37 + }
38 + return;
39 + }
40 + p = e;
41 + e = e->next;
42 + }
43 + if (e && e->ofs <= end) {
44 + e->ofs = ofs;
45 + goto again;
46 + }
47 + else {
48 + e = xmalloc(sizeof(*e));
49 + e->ofs = ofs;
50 + e->end = end;
51 + if (p) {
52 + e->next = p->next;
53 + p->next = e;
54 + }
55 + else {
56 + e->next = *span;
57 + *span = e;
58 + }
59 + }
60 +}
61 +
62 +static unsigned long count_range(struct span *s)
63 +{
64 + struct span *t;
65 + unsigned long sz = 0;
66 + while (s) {
67 + t = s;
68 + sz += s->end - s->ofs;
69 + s = s->next;
70 + free(t);
71 + }
72 + return sz;
73 +}
74
75 /*
76 * NOTE. We do not _interpret_ delta fully. As an approximation, we
@@ -21,10 +84,11 @@
84 int count_delta(void *delta_buf, unsigned long delta_size,
85 unsigned long *src_copied, unsigned long *literal_added)
86 {
24 - unsigned long copied_from_source, added_literal;
87 + unsigned long added_literal;
88 const unsigned char *data, *top;
89 unsigned char cmd;
90 unsigned long src_size, dst_size, out;
91 + struct span *span = NULL;
92
93 if (delta_size < DELTA_SIZE_MIN)
94 return -1;
@@ -35,7 +99,7 @@ int count_delta(void *delta_buf, unsigned long delta_size,
99 src_size = get_delta_hdr_size(&data);
100 dst_size = get_delta_hdr_size(&data);
101
38 - added_literal = copied_from_source = out = 0;
102 + added_literal = out = 0;
103 while (data < top) {
104 cmd = *data++;
105 if (cmd & 0x80) {
@@ -49,7 +113,7 @@ int count_delta(void *delta_buf, unsigned long delta_size,
113 if (cmd & 0x40) cp_size |= (*data++ << 16);
114 if (cp_size == 0) cp_size = 0x10000;
115
52 - copied_from_source += cp_size;
116 + touch_range(&span, cp_off, cp_off+cp_size);
117 out += cp_size;
118 } else {
119 /* write literal into dst */
@@ -59,6 +123,8 @@ int count_delta(void *delta_buf, unsigned long delta_size,
123 }
124 }
125
126 + *src_copied = count_range(span);
127 +
128 /* sanity check */
129 if (data != top || out != dst_size)
130 return -1;
@@ -66,7 +132,6 @@ int count_delta(void *delta_buf, unsigned long delta_size,
132 /* delete size is what was _not_ copied from source.
133 * edit size is that and literal additions.
134 */
69 - *src_copied = copied_from_source;
135 *literal_added = added_literal;
136 return 0;
137 }