authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-06 12:26:27-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-06-06 12:26:27-04:00
loga682b0acb6e74348117eddffe6b966e7720efa61
tree315dcc330cb59721b65b777d4a51a1b4aab070ab
parentddfab40e30e04e1874bff95e5cae54eaaca5542f
parent8f4229a61c311c0fc990c34b280ec91010595bc9
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2630 from squeek502/hashmap-pow2

std.HashMap: optimize by taking advantage of power of two capacity

1 files changed, 16 insertions(+), 8 deletions(-)

std/hash_map.zig+16-8
......@@ -139,9 +139,9 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
139139 // ensure that the hash map will be at most 60% full if
140140 // expected_count items are put into it
141141 var optimized_capacity = expected_count * 5 / 3;
142 // round capacity to the next power of two
143 const pow = math.log2_int_ceil(usize, optimized_capacity);
144 return math.pow(usize, 2, pow);
142 // an overflow here would mean the amount of memory required would not
143 // be representable in the address space
144 return math.ceilPowerOfTwo(usize, optimized_capacity) catch unreachable;
145145 }
146146
147147 /// Increases capacity so that the hash map will be at most
......@@ -155,6 +155,8 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
155155 /// capacity is greater than the current capacity.
156156 /// New capacity must be a power of two.
157157 fn ensureCapacityExact(self: *Self, new_capacity: usize) !void {
158 // capacity must always be a power of two to allow for modulo
159 // optimization in the constrainIndex fn
158160 const is_power_of_two = new_capacity & (new_capacity - 1) == 0;
159161 assert(is_power_of_two);
160162
......@@ -209,7 +211,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
209211 {
210212 var roll_over: usize = 0;
211213 while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
212 const index = (start_index + roll_over) % hm.entries.len;
214 const index = hm.constrainIndex(start_index + roll_over);
213215 var entry = &hm.entries[index];
214216
215217 if (!entry.used) return null;
......@@ -218,7 +220,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
218220
219221 const removed_kv = entry.kv;
220222 while (roll_over < hm.entries.len) : (roll_over += 1) {
221 const next_index = (start_index + roll_over + 1) % hm.entries.len;
223 const next_index = hm.constrainIndex(start_index + roll_over + 1);
222224 const next_entry = &hm.entries[next_index];
223225 if (!next_entry.used or next_entry.distance_from_start_index == 0) {
224226 entry.used = false;
......@@ -301,7 +303,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
301303 roll_over += 1;
302304 distance_from_start_index += 1;
303305 }) {
304 const index = (start_index + roll_over) % self.entries.len;
306 const index = self.constrainIndex(start_index + roll_over);
305307 const entry = &self.entries[index];
306308
307309 if (entry.used and !eql(entry.kv.key, key)) {
......@@ -358,7 +360,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
358360 {
359361 var roll_over: usize = 0;
360362 while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
361 const index = (start_index + roll_over) % hm.entries.len;
363 const index = hm.constrainIndex(start_index + roll_over);
362364 const entry = &hm.entries[index];
363365
364366 if (!entry.used) return null;
......@@ -369,7 +371,13 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
369371 }
370372
371373 fn keyToIndex(hm: Self, key: K) usize {
372 return usize(hash(key)) % hm.entries.len;
374 return hm.constrainIndex(usize(hash(key)));
375 }
376
377 fn constrainIndex(hm: Self, i: usize) usize {
378 // this is an optimization for modulo of power of two integers;
379 // it requires hm.entries.len to always be a power of two
380 return i & (hm.entries.len - 1);
373381 }
374382 };
375383}