From 12e0f559912093fdf26366d48a51f99e7a97d80f Mon Sep 17 00:00:00 2001 From: Radu Ciorba Date: Sun, 16 Jul 2017 14:28:01 +0300 Subject: [PATCH] replace modulo with if to check for wraparound in hashmap Integer division is slow, and this improves the speed of all operations on the hashmap. Benchmarked this patch on the rciorba/master-bench branch: https://cdn.rawgit.com/rciorba/e3eded290e91a361f74eefdbc84416bb/raw/9e5d61e03c5842712d629e3e5567da3a512f1d79/results.html --- src/borg/_hashindex.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/borg/_hashindex.c b/src/borg/_hashindex.c index c6435d85b..9fbd1ebda 100644 --- a/src/borg/_hashindex.c +++ b/src/borg/_hashindex.c @@ -146,7 +146,10 @@ hashindex_lookup(HashIndex *index, const void *key, int *start_idx) } return idx; } - idx = (idx + 1) % index->num_buckets; + idx++; + if (idx >= index->num_buckets) { + idx -= index->num_buckets; + } if(idx == start) { break; } @@ -547,7 +550,10 @@ hashindex_set(HashIndex *index, const void *key, const void *value) } idx = start_idx; while(!BUCKET_IS_EMPTY(index, idx) && !BUCKET_IS_DELETED(index, idx)) { - idx = (idx + 1) % index->num_buckets; + idx++; + if (idx >= index->num_buckets){ + idx -= index->num_buckets; + } } if(BUCKET_IS_EMPTY(index, idx)){ index->num_empty--;