Browse Source

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

(cherry picked from commit 12e0f559912093fdf26366d48a51f99e7a97d80f)
Radu Ciorba 8 năm trước cách đây
mục cha
commit
2cdd4353b6
1 tập tin đã thay đổi với 8 bổ sung2 xóa
  1. 8 2
      borg/_hashindex.c

+ 8 - 2
borg/_hashindex.c

@@ -143,7 +143,10 @@ hashindex_lookup(HashIndex *index, const void *key)
             }
             return idx;
         }
-        idx = (idx + 1) % index->num_buckets;
+        idx++;
+        if (idx >= index->num_buckets) {
+            idx -= index->num_buckets;
+        }
         if(idx == start) {
             return -1;
         }
@@ -434,7 +437,10 @@ hashindex_set(HashIndex *index, const void *key, const void *value)
         }
         idx = hashindex_index(index, key);
         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--;