1 This patch makes the processing of large really large files more efficient
2 by making sure that the sender's hash table is large enough to hold all the
3 checksum entries without being overloaded. It also makes the hashing of
4 normal sized files use slightly less memory than before.
6 An extended version of a patch by Shachar Shemesh.
10 @@ -26,11 +26,6 @@ extern int append_mode;
12 int updating_basis_file;
14 -typedef unsigned short tag;
16 -#define TABLESIZE (1<<16)
17 -#define NULL_TAG (-1)
19 static int false_alarms;
22 @@ -42,47 +37,37 @@ static int total_matches;
24 extern struct stats stats;
31 -static struct target *targets;
33 -static int32 *tag_table;
35 -#define gettag2(s1,s2) (((s1) + (s2)) & 0xFFFF)
36 -#define gettag(sum) gettag2((sum)&0xFFFF,(sum)>>16)
38 -static int compare_targets(struct target *t1,struct target *t2)
40 - return (int)t1->t - (int)t2->t;
42 +static uint32 tablesize;
43 +static int32 *sum_table;
45 +#define gettag2(s1,s2) gettag((s1) + ((s2)<<16))
46 +#define gettag(sum) ((sum)%tablesize)
48 static void build_hash_table(struct sum_struct *s)
51 + uint32 prior_size = tablesize;
54 - tag_table = new_array(int32, TABLESIZE);
55 + /* Dynamically calculate the hash table size so that the hash load
56 + * for big files is about 80%. This number must be odd or s2 will
57 + * not be able to span the entire set. */
58 + tablesize = (uint32)(s->count/8) * 10 + 11;
59 + if (tablesize < 65537)
60 + tablesize = 65537; /* a prime number */
61 + if (tablesize != prior_size) {
63 + sum_table = new_array(int32, tablesize);
65 + out_of_memory("build_hash_table");
68 - targets = new_array(struct target, s->count);
69 - if (!tag_table || !targets)
70 - out_of_memory("build_hash_table");
71 + memset(sum_table, 0xFF, tablesize * sizeof sum_table[0]);
73 for (i = 0; i < s->count; i++) {
75 - targets[i].t = gettag(s->sums[i].sum1);
76 + uint32 t = gettag(s->sums[i].sum1);
77 + s->sums[i].chain = sum_table[t];
81 - qsort(targets,s->count,sizeof(targets[0]),(int (*)())compare_targets);
83 - for (i = 0; i < TABLESIZE; i++)
84 - tag_table[i] = NULL_TAG;
86 - for (i = s->count; i-- > 0; )
87 - tag_table[targets[i].t] = i;
91 @@ -176,20 +161,17 @@ static void hash_search(int f,struct sum
95 - tag t = gettag2(s1,s2);
96 + uint32 t = gettag2(s1,s2);
98 - int32 j = tag_table[t];
102 rprintf(FINFO,"offset=%.0f sum=%08x\n",(double)offset,sum);
107 sum = (s1 & 0xffff) | (s2 << 16);
110 - int32 l, i = targets[j].i;
111 + for (i = sum_table[t]; i >= 0; i = s->sums[i].chain) {
114 if (sum != s->sums[i].sum1)
116 @@ -205,9 +187,11 @@ static void hash_search(int f,struct sum
117 && !(s->sums[i].flags & SUMFLG_SAME_OFFSET))
121 - rprintf(FINFO,"potential match at %.0f target=%.0f %.0f sum=%08x\n",
122 - (double)offset,(double)j,(double)i,sum);
125 + "potential match at %.0f i=%ld sum=%08x\n",
126 + (double)offset, (long)i, sum);
130 map = (schar *)map_ptr(buf,offset,l);
131 @@ -224,8 +208,8 @@ static void hash_search(int f,struct sum
132 * one with an identical offset, so we prefer that over
133 * the following want_i optimization. */
134 if (updating_basis_file) {
136 - int32 i2 = targets[j].i;
138 + for (i2 = i; i2 >= 0; i2 = s->sums[i2].chain) {
139 if (s->sums[i2].offset != offset)
142 @@ -240,7 +224,7 @@ static void hash_search(int f,struct sum
143 * both the sender and the receiver. */
144 s->sums[i].flags |= SUMFLG_SAME_OFFSET;
146 - } while (++j < s->count && targets[j].t == t);
150 /* we've found a match, but now check to see
151 @@ -266,9 +250,8 @@ static void hash_search(int f,struct sum
155 - } while (++j < s->count && targets[j].t == t);
159 backup = offset - last_match;
160 /* We sometimes read 1 byte prior to last_match... */
162 @@ -375,11 +358,6 @@ void match_sums(int f, struct sum_struct
163 rprintf(FINFO,"sending file_sum\n");
164 write_buf(f,file_sum,MD4_SUM_LENGTH);
172 rprintf(FINFO, "false_alarms=%d tag_hits=%d matches=%d\n",
173 false_alarms, tag_hits, matches);
176 @@ -560,6 +560,7 @@ struct sum_buf {
177 OFF_T offset; /**< offset in file of this chunk */
178 int32 len; /**< length of chunk of file */
179 uint32 sum1; /**< simple checksum */
180 + int32 chain; /**< next hash-table collision */
181 short flags; /**< flag bits */
182 char sum2[SUM_LENGTH]; /**< checksum */