mirror of
https://github.com/golang/go
synced 2024-11-17 22:05:02 -07:00
c12b185a6e
Currently rvalue m[k] is transformed during walk into: tmp1 := *mapaccess(m, k) tmp2 := append(tmp1, ...) *mapassign(m, k) = tmp2 However, this is suboptimal, as we could instead produce just: tmp := mapassign(m, k) *tmp := append(*tmp, ...) Optimization is possible only if during Order it may tell that m[k] is exactly the same at left and right part of assignment. It doesn't work: 1) m[f(k)] = append(m[f(k)], ...) 2) sink, m[k] = sink, append(m[k]...) 3) m[k] = append(..., m[k],...) Benchmark: name old time/op new time/op delta MapAppendAssign/Int32/256-8 33.5ns ± 3% 22.4ns ±10% -33.24% (p=0.000 n=16+18) MapAppendAssign/Int32/65536-8 68.2ns ± 6% 48.5ns ±29% -28.90% (p=0.000 n=20+20) MapAppendAssign/Int64/256-8 34.3ns ± 4% 23.3ns ± 5% -32.23% (p=0.000 n=17+18) MapAppendAssign/Int64/65536-8 65.9ns ± 7% 61.2ns ±19% -7.06% (p=0.002 n=18+20) MapAppendAssign/Str/256-8 116ns ±12% 79ns ±16% -31.70% (p=0.000 n=20+19) MapAppendAssign/Str/65536-8 134ns ±15% 111ns ±45% -16.95% (p=0.000 n=19+20) name old alloc/op new alloc/op delta MapAppendAssign/Int32/256-8 47.0B ± 0% 46.0B ± 0% -2.13% (p=0.000 n=19+18) MapAppendAssign/Int32/65536-8 27.0B ± 0% 20.7B ±30% -23.33% (p=0.000 n=20+20) MapAppendAssign/Int64/256-8 47.0B ± 0% 46.0B ± 0% -2.13% (p=0.000 n=20+17) MapAppendAssign/Int64/65536-8 27.0B ± 0% 27.0B ± 0% ~ (all equal) MapAppendAssign/Str/256-8 94.0B ± 0% 78.0B ± 0% -17.02% (p=0.000 n=20+16) MapAppendAssign/Str/65536-8 54.0B ± 0% 54.0B ± 0% ~ (all equal) Fixes #24364 Updates #5147 Change-Id: Id257d052b75b9a445b4885dc571bf06ce6f6b409 Reviewed-on: https://go-review.googlesource.com/100838 Reviewed-by: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
118 lines
2.0 KiB
Go
118 lines
2.0 KiB
Go
// run
|
|
|
|
// Copyright 2017 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.
|
|
|
|
// Test to make sure RHS is evaluated before map insert is started.
|
|
// The RHS panics in all of these cases.
|
|
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
for i, f := range []func(map[int]int){
|
|
f0, f1, f2, f3, f4, f5, f6, f7, f8,
|
|
} {
|
|
m := map[int]int{}
|
|
func() { // wrapper to scope the defer.
|
|
defer func() {
|
|
recover()
|
|
}()
|
|
f(m) // Will panic. Shouldn't modify m.
|
|
fmt.Printf("RHS didn't panic, case f%d\n", i)
|
|
}()
|
|
if len(m) != 0 {
|
|
fmt.Printf("map insert happened, case f%d\n", i)
|
|
}
|
|
}
|
|
|
|
// Append slice.
|
|
for i, f := range []func(map[int][]int){
|
|
fa0, fa1, fa2, fa3,
|
|
} {
|
|
m := map[int][]int{}
|
|
func() { // wrapper to scope the defer.
|
|
defer func() {
|
|
recover()
|
|
}()
|
|
f(m) // Will panic. Shouldn't modify m.
|
|
fmt.Printf("RHS didn't panic, case fa%d\n", i)
|
|
}()
|
|
if len(m) != 0 {
|
|
fmt.Printf("map insert happened, case fa%d\n", i)
|
|
}
|
|
}
|
|
}
|
|
|
|
func f0(m map[int]int) {
|
|
var p *int
|
|
m[0] = *p
|
|
}
|
|
|
|
func f1(m map[int]int) {
|
|
var p *int
|
|
m[0] += *p
|
|
}
|
|
|
|
func f2(m map[int]int) {
|
|
var p *int
|
|
sink, m[0] = sink, *p
|
|
}
|
|
|
|
func f3(m map[int]int) {
|
|
var p *chan int
|
|
m[0], sink = <-(*p)
|
|
}
|
|
|
|
func f4(m map[int]int) {
|
|
var p *interface{}
|
|
m[0], sink = (*p).(int)
|
|
}
|
|
|
|
func f5(m map[int]int) {
|
|
var p *map[int]int
|
|
m[0], sink = (*p)[0]
|
|
}
|
|
|
|
func f6(m map[int]int) {
|
|
var z int
|
|
m[0] /= z
|
|
}
|
|
|
|
func f7(m map[int]int) {
|
|
var a []int
|
|
m[0] = a[0]
|
|
}
|
|
|
|
func f8(m map[int]int) {
|
|
var z int
|
|
m[0] %= z
|
|
}
|
|
|
|
func fa0(m map[int][]int) {
|
|
var p *int
|
|
m[0] = append(m[0], *p)
|
|
}
|
|
|
|
func fa1(m map[int][]int) {
|
|
var p *int
|
|
sink, m[0] = !sink, append(m[0], *p)
|
|
}
|
|
|
|
func fa2(m map[int][]int) {
|
|
var p *int
|
|
m[0], _ = append(m[0], 0), *p
|
|
}
|
|
|
|
func fa3(m map[int][]int) {
|
|
// OSLICE has similar in-place-reassignment
|
|
// optimizations as OAPPEND, but we need to make sure
|
|
// to *not* optimize them, because we can't guarantee
|
|
// the slice indices are within bounds.
|
|
m[0] = m[0][:1]
|
|
}
|
|
|
|
var sink bool
|