Skip to content

Commit 4c8ab99

Browse files
committed
internal/runtime/maps: don't hash twice when deleting
│ baseline │ experiment │ │ sec/op │ sec/op vs base │ MapDeleteLargeKey-24 312.0n ± 6% 162.3n ± 5% -47.97% (p=0.000 n=10) Change-Id: I31f1f8e3c344cf8abf2e9eb4b51b78fcd67b93c4 Reviewed-on: https://go-review.googlesource.com/c/go/+/625906 Reviewed-by: Michael Pratt <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent f9159b1 commit 4c8ab99

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

src/internal/runtime/maps/map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ func (m *Map) Delete(typ *abi.SwissMapType, key unsafe.Pointer) {
663663
m.deleteSmall(typ, hash, key)
664664
} else {
665665
idx := m.directoryIndex(hash)
666-
m.directoryAt(idx).Delete(typ, m, key)
666+
m.directoryAt(idx).Delete(typ, m, hash, key)
667667
}
668668

669669
if m.used == 0 {

src/internal/runtime/maps/table.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,7 @@ func (t *table) uncheckedPutSlot(typ *abi.SwissMapType, hash uintptr, key unsafe
409409
}
410410
}
411411

412-
func (t *table) Delete(typ *abi.SwissMapType, m *Map, key unsafe.Pointer) {
413-
hash := typ.Hasher(key, m.seed)
414-
412+
func (t *table) Delete(typ *abi.SwissMapType, m *Map, hash uintptr, key unsafe.Pointer) {
415413
seq := makeProbeSeq(h1(hash), t.groups.lengthMask)
416414
for ; ; seq = seq.next() {
417415
g := t.groups.group(typ, seq.offset)

src/runtime/map_benchmark_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,3 +1090,14 @@ func BenchmarkMapPop(b *testing.B) {
10901090
b.Run("Key=*int32/Elem=int32", benchSizes(benchmarkMapPop[*int32, int32]))
10911091
b.Run("Key=int32/Elem=*int32", benchSizes(benchmarkMapPop[int32, *int32]))
10921092
}
1093+
1094+
func BenchmarkMapDeleteLargeKey(b *testing.B) {
1095+
m := map[string]int{}
1096+
for i := range 9 {
1097+
m[fmt.Sprintf("%d", i)] = i
1098+
}
1099+
key := strings.Repeat("*", 10000)
1100+
for range b.N {
1101+
delete(m, key)
1102+
}
1103+
}

0 commit comments

Comments
 (0)