Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Stdlib] Speedup Dict (changing modulus to bitshifting) #3071

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions stdlib/src/collections/dict.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -271,30 +271,30 @@ struct _DictIndex:
fn get_index(self, reserved: Int, slot: Int) -> Int:
if reserved <= 128:
var data = self.data.bitcast[DType.int8]()
return int(Scalar.load(data, slot % reserved))
return int(Scalar.load(data, slot & (reserved - 1)))
elif reserved <= 2**16 - 2:
var data = self.data.bitcast[DType.int16]()
return int(Scalar.load(data, slot % reserved))
return int(Scalar.load(data, slot & (reserved - 1)))
elif reserved <= 2**32 - 2:
var data = self.data.bitcast[DType.int32]()
return int(Scalar.load(data, slot % reserved))
return int(Scalar.load(data, slot & (reserved - 1)))
else:
var data = self.data.bitcast[DType.int64]()
return int(Scalar.load(data, slot % reserved))
return int(Scalar.load(data, slot & (reserved - 1)))

fn set_index(inout self, reserved: Int, slot: Int, value: Int):
if reserved <= 128:
var data = self.data.bitcast[DType.int8]()
return Scalar.store(data, slot % reserved, value)
return Scalar.store(data, slot & (reserved - 1), value)
elif reserved <= 2**16 - 2:
var data = self.data.bitcast[DType.int16]()
return Scalar.store(data, slot % reserved, value)
return Scalar.store(data, slot & (reserved - 1), value)
elif reserved <= 2**32 - 2:
var data = self.data.bitcast[DType.int32]()
return Scalar.store(data, slot % reserved, value)
return Scalar.store(data, slot & (reserved - 1), value)
else:
var data = self.data.bitcast[DType.int64]()
return Scalar.store(data, slot % reserved, value)
return Scalar.store(data, slot & (reserved - 1), value)

fn __del__(owned self):
self.data.free()
Expand Down Expand Up @@ -888,10 +888,10 @@ struct Dict[K: KeyElement, V: CollectionElement](
fn _next_index_slot(self, inout slot: Int, inout perturb: UInt64):
alias PERTURB_SHIFT = 5
perturb >>= PERTURB_SHIFT
slot = ((5 * slot) + int(perturb + 1)) % self._reserved()
slot = ((5 * slot) + int(perturb + 1)) & (self._reserved() - 1)

fn _find_empty_index(self, hash: Int) -> Int:
var slot = hash % self._reserved()
var slot = hash & (self._reserved() - 1)
var perturb = bitcast[DType.uint64](Int64(hash))
while True:
var index = self._get_index(slot)
Expand All @@ -901,7 +901,7 @@ struct Dict[K: KeyElement, V: CollectionElement](

fn _find_index(self, hash: Int, key: K) -> (Bool, Int, Int):
# Return (found, slot, index)
var slot = hash % self._reserved()
var slot = hash & (self._reserved() - 1)
var perturb = bitcast[DType.uint64](Int64(hash))
while True:
var index = self._get_index(slot)
Expand Down