From 2493072db68a8f8b545bb2a6faebac0da0f01336 Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Mon, 18 Jul 2022 11:47:19 -0700 Subject: cmd/compile: avoid assignment conversion in append(a, b...) There's no need for a and b to match types. The typechecker already ensured that a and b are both slices with the same base type, or a and b are (possibly named) []byte and string. The optimization to treat append(b, make([], ...)) as a zeroing slice extension doesn't fire when there's a OCONVNOP wrapping the make. Fixes #53888 Change-Id: Ied871ed0bbb8e4a4b35d280c71acbab8103691bc Reviewed-on: https://go-review.googlesource.com/c/go/+/418475 TryBot-Result: Gopher Robot Reviewed-by: Matthew Dempsky Reviewed-by: Cuong Manh Le Reviewed-by: Keith Randall Run-TryBot: Keith Randall --- src/cmd/compile/internal/test/issue53888_test.go | 44 ++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/cmd/compile/internal/test/issue53888_test.go (limited to 'src/cmd/compile/internal/test') diff --git a/src/cmd/compile/internal/test/issue53888_test.go b/src/cmd/compile/internal/test/issue53888_test.go new file mode 100644 index 0000000000..9f50a82a3c --- /dev/null +++ b/src/cmd/compile/internal/test/issue53888_test.go @@ -0,0 +1,44 @@ +// Copyright 2022 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. + +//go:build !race + +package test + +import ( + "testing" +) + +func TestAppendOfMake(t *testing.T) { + for n := 32; n < 33; n++ { // avoid stack allocation of make() + b := make([]byte, n) + f := func() { + b = append(b[:0], make([]byte, n)...) + } + if n := testing.AllocsPerRun(10, f); n > 0 { + t.Errorf("got %f allocs, want 0", n) + } + type S []byte + + s := make(S, n) + g := func() { + s = append(s[:0], make(S, n)...) + } + if n := testing.AllocsPerRun(10, g); n > 0 { + t.Errorf("got %f allocs, want 0", n) + } + h := func() { + s = append(s[:0], make([]byte, n)...) + } + if n := testing.AllocsPerRun(10, h); n > 0 { + t.Errorf("got %f allocs, want 0", n) + } + i := func() { + b = append(b[:0], make(S, n)...) + } + if n := testing.AllocsPerRun(10, i); n > 0 { + t.Errorf("got %f allocs, want 0", n) + } + } +} -- cgit v1.3-5-g9baa