1
0
mirror of https://github.com/golang/go synced 2024-11-23 19:50:06 -07:00
go/test/codegen/maps.go
Martin Möhrmann a0f57c3fd0 cmd/compile: avoid string allocations when map key is struct or array literal
x = map[string(byteslice)] is already optimized by the compiler to avoid a
string allocation. This CL generalizes this optimization to:

x = map[T1{ ... Tn{..., string(byteslice), ...} ... }]
where T1 to Tn is a nesting of struct and array literals.

Found in a hot code path that used a struct of strings made from []byte
slices to make a map lookup.

There are no uses of the more generalized optimization in the standard library.
Passes toolstash -cmp.

MapStringConversion/32/simple    21.9ns ± 2%    21.9ns ± 3%      ~     (p=0.995 n=17+20)
MapStringConversion/32/struct    28.8ns ± 3%    22.0ns ± 2%   -23.80%  (p=0.000 n=20+20)
MapStringConversion/32/array     28.5ns ± 2%    21.9ns ± 2%   -23.14%  (p=0.000 n=19+16)
MapStringConversion/64/simple    21.0ns ± 2%    21.1ns ± 3%      ~     (p=0.072 n=19+18)
MapStringConversion/64/struct    72.4ns ± 3%    21.3ns ± 2%   -70.53%  (p=0.000 n=20+20)
MapStringConversion/64/array     72.8ns ± 1%    21.0ns ± 2%   -71.13%  (p=0.000 n=17+19)

name                           old allocs/op  new allocs/op  delta
MapStringConversion/32/simple      0.00           0.00           ~     (all equal)
MapStringConversion/32/struct      0.00           0.00           ~     (all equal)
MapStringConversion/32/array       0.00           0.00           ~     (all equal)
MapStringConversion/64/simple      0.00           0.00           ~     (all equal)
MapStringConversion/64/struct      1.00 ± 0%      0.00       -100.00%  (p=0.000 n=20+20)
MapStringConversion/64/array       1.00 ± 0%      0.00       -100.00%  (p=0.000 n=20+20)

Change-Id: I483b4d84d8d74b1025b62c954da9a365e79b7a3a
Reviewed-on: https://go-review.googlesource.com/c/116275
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-10-15 19:22:07 +00:00

125 lines
2.8 KiB
Go

// asmcheck
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package codegen
// This file contains code generation tests related to the handling of
// map types.
// ------------------- //
// Access Const //
// ------------------- //
// Direct use of constants in fast map access calls (Issue #19015).
func AccessInt1(m map[int]int) int {
// amd64:"MOVQ\t[$]5"
return m[5]
}
func AccessInt2(m map[int]int) bool {
// amd64:"MOVQ\t[$]5"
_, ok := m[5]
return ok
}
func AccessString1(m map[string]int) int {
// amd64:`.*"abc"`
return m["abc"]
}
func AccessString2(m map[string]int) bool {
// amd64:`.*"abc"`
_, ok := m["abc"]
return ok
}
// ------------------- //
// String Conversion //
// ------------------- //
func LookupStringConversionSimple(m map[string]int, bytes []byte) int {
// amd64:-`.*runtime\.slicebytetostring\(`
return m[string(bytes)]
}
func LookupStringConversionStructLit(m map[struct{ string }]int, bytes []byte) int {
// amd64:-`.*runtime\.slicebytetostring\(`
return m[struct{ string }{string(bytes)}]
}
func LookupStringConversionArrayLit(m map[[2]string]int, bytes []byte) int {
// amd64:-`.*runtime\.slicebytetostring\(`
return m[[2]string{string(bytes), string(bytes)}]
}
func LookupStringConversionNestedLit(m map[[1]struct{ s [1]string }]int, bytes []byte) int {
// amd64:-`.*runtime\.slicebytetostring\(`
return m[[1]struct{ s [1]string }{struct{ s [1]string }{s: [1]string{string(bytes)}}}]
}
func LookupStringConversionKeyedArrayLit(m map[[2]string]int, bytes []byte) int {
// amd64:-`.*runtime\.slicebytetostring\(`
return m[[2]string{0: string(bytes)}]
}
// ------------------- //
// Map Clear //
// ------------------- //
// Optimization of map clear idiom (Issue #20138).
func MapClearReflexive(m map[int]int) {
// amd64:`.*runtime\.mapclear`
// amd64:-`.*runtime\.mapiterinit`
for k := range m {
delete(m, k)
}
}
func MapClearIndirect(m map[int]int) {
s := struct{ m map[int]int }{m: m}
// amd64:`.*runtime\.mapclear`
// amd64:-`.*runtime\.mapiterinit`
for k := range s.m {
delete(s.m, k)
}
}
func MapClearPointer(m map[*byte]int) {
// amd64:`.*runtime\.mapclear`
// amd64:-`.*runtime\.mapiterinit`
for k := range m {
delete(m, k)
}
}
func MapClearNotReflexive(m map[float64]int) {
// amd64:`.*runtime\.mapiterinit`
// amd64:-`.*runtime\.mapclear`
for k := range m {
delete(m, k)
}
}
func MapClearInterface(m map[interface{}]int) {
// amd64:`.*runtime\.mapiterinit`
// amd64:-`.*runtime\.mapclear`
for k := range m {
delete(m, k)
}
}
func MapClearSideEffect(m map[int]int) int {
k := 0
// amd64:`.*runtime\.mapiterinit`
// amd64:-`.*runtime\.mapclear`
for k = range m {
delete(m, k)
}
return k
}