go/test/codegen/maps.go
Youlin Feng c7ccbddf22 cmd/compile/internal/ssa: more aggressive on dead auto elim
Propagate "unread" across OpMoves. If the addr of this auto is only used
by an OpMove as its source arg, and the OpMove's target arg is the addr
of another auto. If the 2nd auto can be eliminated, this one can also be
eliminated.

This CL eliminates unnecessary memory copies and makes the frame smaller
in the following code snippet:

func contains(m map[string][16]int, k string) bool {
        _, ok := m[k]
        return ok
}

These are the benchmark results followed by the benchmark code:

goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz
                │   old.txt   │              new.txt                │
                │   sec/op    │   sec/op     vs base                │
Map1Access2Ok-8   9.582n ± 2%   9.226n ± 0%   -3.72% (p=0.000 n=20)
Map2Access2Ok-8   13.79n ± 1%   10.24n ± 1%  -25.77% (p=0.000 n=20)
Map3Access2Ok-8   68.68n ± 1%   12.65n ± 1%  -81.58% (p=0.000 n=20)

package main_test

import "testing"

var (
        m1 = map[int]int{}
        m2 = map[int][16]int{}
        m3 = map[int][256]int{}
)

func init() {
        for i := range 1000 {
                m1[i] = i
                m2[i] = [16]int{15:i}
                m3[i] = [256]int{255:i}
        }
}

func BenchmarkMap1Access2Ok(b *testing.B) {
        for i := range b.N {
                _, ok := m1[i%1000]
                if !ok {
                        b.Errorf("%d not found", i)
                }
        }
}

func BenchmarkMap2Access2Ok(b *testing.B) {
        for i := range b.N {
                _, ok := m2[i%1000]
                if !ok {
                        b.Errorf("%d not found", i)
                }
        }
}

func BenchmarkMap3Access2Ok(b *testing.B) {
        for i := range b.N {
                _, ok := m3[i%1000]
                if !ok {
                        b.Errorf("%d not found", i)
                }
        }
}

Fixes #75398

Change-Id: If75e9caaa50d460efc31a94565b9ba28c8158771
Reviewed-on: https://go-review.googlesource.com/c/go/+/702875
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
2025-11-04 12:46:15 -08:00

245 lines
4.7 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:"MOV[LQ] [$]5"
return m[5]
}
func AccessInt2(m map[int]int) bool {
// amd64:"MOV[LQ] [$]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
}
func AccessStringIntArray2(m map[string][16]int, k string) bool {
// amd64:-"MOVUPS"
_, ok := m[k]
return ok
}
type Struct struct {
A, B, C, D, E, F, G, H, I, J int
}
func AccessStringStruct2(m map[string]Struct, k string) bool {
// amd64:-"MOVUPS"
_, ok := m[k]
return ok
}
func AccessIntArrayLarge2(m map[int][512]int, k int) bool {
// amd64:-"REP",-"MOVSQ"
_, ok := m[k]
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)}]
}
func LookupStringConversion1(m map[string]int, bytes []byte) int {
// amd64:-`.*runtime\.slicebytetostring\(`
s := string(bytes)
return m[s]
}
func LookupStringConversion2(m *map[string]int, bytes []byte) int {
// amd64:-`.*runtime\.slicebytetostring\(`
s := string(bytes)
return (*m)[s]
}
func LookupStringConversion3(m map[string]int, bytes []byte) (int, bool) {
// amd64:-`.*runtime\.slicebytetostring\(`
s := string(bytes)
r, ok := m[s]
return r, ok
}
func DeleteStringConversion(m map[string]int, bytes []byte) {
// amd64:-`.*runtime\.slicebytetostring\(`
s := string(bytes)
delete(m, s)
}
// ------------------- //
// Map Clear //
// ------------------- //
// Optimization of map clear idiom (Issue #20138).
func MapClearReflexive(m map[int]int) {
// amd64:`.*runtime\.mapclear`
// amd64:-`.*runtime\.(mapiterinit|mapIterStart)`
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|mapIterStart)`
for k := range s.m {
delete(s.m, k)
}
}
func MapClearPointer(m map[*byte]int) {
// amd64:`.*runtime\.mapclear`
// amd64:-`.*runtime\.(mapiterinit|mapIterStart)`
for k := range m {
delete(m, k)
}
}
func MapClearNotReflexive(m map[float64]int) {
// amd64:`.*runtime\.(mapiterinit|mapIterStart)`
// amd64:-`.*runtime\.mapclear`
for k := range m {
delete(m, k)
}
}
func MapClearInterface(m map[interface{}]int) {
// amd64:`.*runtime\.(mapiterinit|mapIterStart)`
// amd64:-`.*runtime\.mapclear`
for k := range m {
delete(m, k)
}
}
func MapClearSideEffect(m map[int]int) int {
k := 0
// amd64:`.*runtime\.(mapiterinit|mapIterStart)`
// amd64:-`.*runtime\.mapclear`
for k = range m {
delete(m, k)
}
return k
}
func MapLiteralSizing(x int) (map[int]int, map[int]int) {
// This is tested for internal/abi/maps.go:MapBucketCountBits={3,4,5}
// amd64:"MOVL [$]33,"
m := map[int]int{
0: 0,
1: 1,
2: 2,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
10: 10,
11: 11,
12: 12,
13: 13,
14: 14,
15: 15,
16: 16,
17: 17,
18: 18,
19: 19,
20: 20,
21: 21,
22: 22,
23: 23,
24: 24,
25: 25,
26: 26,
27: 27,
28: 28,
29: 29,
30: 30,
31: 32,
32: 32,
}
// amd64:"MOVL [$]33,"
n := map[int]int{
0: 0,
1: 1,
2: 2,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
10: 10,
11: 11,
12: 12,
13: 13,
14: 14,
15: 15,
16: 16,
17: 17,
18: 18,
19: 19,
20: 20,
21: 21,
22: 22,
23: 23,
24: 24,
25: 25,
26: 26,
27: 27,
28: 28,
29: 29,
30: 30,
31: 32,
32: 32,
}
return m, n
}