Skip to content

Commit

Permalink
Optimise looking up unused HashMap keys
Browse files Browse the repository at this point in the history
This optimises lookups of unused keys. When performing a lookup using an
unused key, we would cycle over all buckets in a HashMap. This commit
changes lookups so that they return early the moment they find a missing
bucket.
  • Loading branch information
Yorick Peterse committed Jun 14, 2019
1 parent e357fd0 commit 2c96d0d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions runtime/src/std/hash_map.inko
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,21 @@ object Table!(K: Hash + Equal, V) {
let mut pair = @buckets[index]

{ pair.key != key }.while_true {
# Finding an empty bucket can mean two things:
#
# 1. The initial desired bucket is not used, meaning our key definitely
# does not exist.
# 2. The initial desired bucket is used, and we ran into the next
# available bucket. This means the key also does not exist, because it
# would otherwise use this available bucket.
#
# This early return ensures we don't iterate over all buckets if we are
# certain we won't be able to find the key.
pair == Nil
.if_true {
return
}

index = desired_bucket(index + 1)

index == desired
Expand Down

0 comments on commit 2c96d0d

Please sign in to comment.