Skip to content

Commit 1262a41

Browse files
hargoniXkim-em
authored andcommitted
perf: Don't hash the key when searching in empty hash tables
Based on: rust-lang/hashbrown#305
1 parent a7efe5b commit 1262a41

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

src/Lean/Data/HashMap.lean

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,21 @@ private def mkIdx {sz : Nat} (hash : UInt64) (h : sz.isPowerOfTwo) : { u : USize
6464

6565
def findEntry? [BEq α] [Hashable α] (m : HashMapImp α β) (a : α) : Option (α × β) :=
6666
match m with
67+
| ⟨0, _⟩ => none
6768
| ⟨_, buckets⟩ =>
6869
let ⟨i, h⟩ := mkIdx (hash a) buckets.property
6970
buckets.val[i].findEntry? a
7071

7172
def find? [beq : BEq α] [Hashable α] (m : HashMapImp α β) (a : α) : Option β :=
7273
match m with
74+
| ⟨0, _⟩ => none
7375
| ⟨_, buckets⟩ =>
7476
let ⟨i, h⟩ := mkIdx (hash a) buckets.property
7577
buckets.val[i].find? a
7678

7779
def contains [BEq α] [Hashable α] (m : HashMapImp α β) (a : α) : Bool :=
7880
match m with
81+
| ⟨0, _⟩ => false
7982
| ⟨_, buckets⟩ =>
8083
let ⟨i, h⟩ := mkIdx (hash a) buckets.property
8184
buckets.val[i].contains a

src/Lean/Data/HashSet.lean

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ private def mkIdx {sz : Nat} (hash : UInt64) (h : sz.isPowerOfTwo) : { u : USize
6060

6161
def find? [BEq α] [Hashable α] (m : HashSetImp α) (a : α) : Option α :=
6262
match m with
63+
| ⟨0, _⟩ => none
6364
| ⟨_, buckets⟩ =>
6465
let ⟨i, h⟩ := mkIdx (hash a) buckets.property
6566
buckets.val[i].find? (fun a' => a == a')
6667

6768
def contains [BEq α] [Hashable α] (m : HashSetImp α) (a : α) : Bool :=
6869
match m with
70+
| ⟨0, _⟩ => false
6971
| ⟨_, buckets⟩ =>
7072
let ⟨i, h⟩ := mkIdx (hash a) buckets.property
7173
buckets.val[i].contains a

0 commit comments

Comments
 (0)