--- xdelta-1.1.4/xdeltapriv.h.bak 2007-01-28 22:19:40.000000000 +0200 +++ xdelta-1.1.4/xdeltapriv.h 2008-03-29 01:47:59.919933932 +0200 @@ -126,6 +126,24 @@ gboolean map_page (X gboolean check_stream_integrity (XdeltaStream* str, const guint8* md5, guint len); XdeltaControl* control_new (void); -guint c_hash (const XdeltaChecksum* c); +static inline guint32 next_pow2_32(guint32 x) { + x--; + x |= (x >> 1); + x |= (x >> 2); + x |= (x >> 4); + x |= (x >> 8); + x |= (x >>16); + return x + 1; +} + +static inline guint +c_hash (const XdeltaChecksum* c) +{ + const guint high = c->high; + const guint low = c->low; + const guint it = (high >> 2) + (low << 3) + (high << 16); + + return (it ^ high ^ low); +} #endif /* _XDELTAPRIV_H_ */ --- xdelta-1.1.4/xdelta.c.bak 2007-01-28 22:23:05.000000000 +0200 +++ xdelta-1.1.4/xdelta.c 2008-03-29 01:50:54.319934019 +0200 @@ -441,16 +441,6 @@ xdp_source_add (XdeltaGenerator *gen, g_ptr_array_add (gen->sources, src); } -guint -c_hash (const XdeltaChecksum* c) -{ - const guint high = c->high; - const guint low = c->low; - const guint it = (high >> 2) + (low << 3) + (high << 16); - - return (it ^ high ^ low); -} - static gboolean region_insert (XdeltaGenerator* gen, const XdeltaPos *xpos, guint len) { @@ -718,7 +708,7 @@ compute_copies (XdeltaGenerator* gen, Xd XdeltaChecksum cksum; const XdeltaChecksum *source_cksum; const guint8 *segment_pointer; - guint source_offset, segment_index, index, prime = gen->table_size; + guint source_offset, segment_index, index; guint source_index; const guint32* table = gen->table; guint16 old_c, new_c; @@ -764,7 +754,7 @@ compute_copies (XdeltaGenerator* gen, Xd abort (); #endif - index = c_hash (&cksum) % prime; + index = c_hash (&cksum) & (gen->table_size-1); #ifdef DEBUG_MATCH_PRINT g_print ("%d: searching for match \"", XPOS(xpos)); @@ -955,7 +945,7 @@ xdp_generate_delta_int (XdeltaGenerator XdeltaOutStream *control_out, XdeltaOutStream *data_out) { - gint i, j, total_from_ck_count = 0, prime = 0, index = 0; + gint i, j, total_from_ck_count = 0, tablesize = 0, index = 0; gint total_from_len = 0; guint32* table = NULL; @@ -1009,10 +999,9 @@ xdp_generate_delta_int (XdeltaGenerator total_from_ck_count += xs->ck_count; } - prime = g_spaced_primes_closest (total_from_ck_count); - - gen->table = table = g_new0 (guint32, prime); - gen->table_size = prime; + tablesize = next_pow2_32(total_from_ck_count); + gen->table = table = g_new0 (guint32, tablesize); + gen->table_size = tablesize; for (i = 0; i < gen->sources->len; i += 1) { @@ -1020,7 +1009,7 @@ xdp_generate_delta_int (XdeltaGenerator for (j = xs->ck_count-1; j >= 0; j -= 1) { - index = c_hash (xs->cksums + j) % prime; + index = c_hash (xs->cksums + j) & (tablesize-1); #ifdef DEBUG_HASH gen->hash_entries += 1; @@ -1043,7 +1032,7 @@ xdp_generate_delta_int (XdeltaGenerator } #ifdef DEBUG_HASH - for (i = 0; i < prime; i += 1) + for (i = 0; i < tablesize; i += 1) { if (gen->table[i]) gen->hash_fill += 1; --- xdelta-1.1.4/xdrsync.c.bak 2007-01-28 03:41:50.000000000 +0200 +++ xdelta-1.1.4/xdrsync.c 2008-03-29 01:55:38.790933985 +0200 @@ -207,14 +207,14 @@ xdp_rsync_index_free (XdeltaRsync *rsync static gboolean xdp_rsync_hash (XdeltaRsync* rsync) { - guint i, index, prime = 0; + guint i, index, tablesize = 0; gboolean already_hashed = rsync->table != NULL; SerialRsyncIndexElt** table = NULL; if (! already_hashed) { - prime = rsync->table_size = g_spaced_primes_closest (rsync->index_len); - table = rsync->table = g_new0 (SerialRsyncIndexElt*, prime); + tablesize = rsync->table_size = next_pow2_32(rsync->index_len); + table = rsync->table = g_new0 (SerialRsyncIndexElt*, tablesize); } for (i = 0; i < rsync->index_len; i += 1) @@ -225,7 +225,7 @@ gboolean xdp_rsync_hash (XdeltaRsync* rs if (! already_hashed) { - index = c_hash (& elt->cksum) % prime; + index = c_hash (&elt->cksum) & (rsync->table_size-1); elt->next = table[index]; table[index] = elt; @@ -259,7 +259,7 @@ xdp_rsync_request (XdeltaStream *fi GArray *request = g_array_new (FALSE, FALSE, sizeof (guint)); const guint8* n_pointer, *o_pointer; guint thistime; - guint prime, index; + guint tablemask, index; SerialRsyncIndexElt **table; guint i; guint matched = 0; @@ -279,7 +279,7 @@ xdp_rsync_request (XdeltaStream *fi init_pos (file, &npos); memset (&cksum, 0, sizeof (cksum)); - prime = rsync->table_size; + tablemask = rsync->table_size - 1; table = rsync->table; if (!map_page (file, &opos)) @@ -308,7 +308,7 @@ xdp_rsync_request (XdeltaStream *fi for (; ; o_pointer += 1, n_pointer += 1) { - index = c_hash (&cksum) % prime; + index = c_hash (&cksum) & tablemask; if (table[index]) {