mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
mustBe was barely over budget, so manually inlining the first flag.kind call is enough. Add a TODO to reverse that in the future, once the compiler gets better. mustBeExported and mustBeAssignable were over budget by a larger amount, so add slow path functions instead. This is the same strategy used in the sync package for common methods like Once.Do, for example. Lots of exported reflect.Value methods call these assert-like unexported methods, so avoiding the function call overhead in the common case does shave off a percent from most exported APIs. Finally, add the methods to TestIntendedInlining. While at it, replace a couple of uses of the 0 Kind with its descriptive name, Invalid. name old time/op new time/op delta Call-8 68.0ns ± 1% 66.8ns ± 1% -1.81% (p=0.000 n=10+9) PtrTo-8 8.00ns ± 2% 7.83ns ± 0% -2.19% (p=0.000 n=10+9) Updates #7818. Change-Id: Ic1603b640519393f6b50dd91ec3767753eb9e761 Reviewed-on: https://go-review.googlesource.com/c/go/+/166462 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
259 lines
6.5 KiB
Go
259 lines
6.5 KiB
Go
// 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.
|
|
|
|
package gc
|
|
|
|
import (
|
|
"bufio"
|
|
"internal/testenv"
|
|
"io"
|
|
"math/bits"
|
|
"os/exec"
|
|
"regexp"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestIntendedInlining tests that specific runtime functions are inlined.
|
|
// This allows refactoring for code clarity and re-use without fear that
|
|
// changes to the compiler will cause silent performance regressions.
|
|
func TestIntendedInlining(t *testing.T) {
|
|
if testing.Short() && testenv.Builder() == "" {
|
|
t.Skip("skipping in short mode")
|
|
}
|
|
testenv.MustHaveGoRun(t)
|
|
t.Parallel()
|
|
|
|
// want is the list of function names (by package) that should
|
|
// be inlinable. If they have no callers in their packages, they
|
|
// might not actually be inlined anywhere.
|
|
want := map[string][]string{
|
|
"runtime": {
|
|
// TODO(mvdan): enable these once mid-stack
|
|
// inlining is available
|
|
// "adjustctxt",
|
|
|
|
"add",
|
|
"acquirem",
|
|
"add1",
|
|
"addb",
|
|
"adjustpanics",
|
|
"adjustpointer",
|
|
"bucketMask",
|
|
"bucketShift",
|
|
"chanbuf",
|
|
"deferArgs",
|
|
"deferclass",
|
|
"evacuated",
|
|
"fastlog2",
|
|
"fastrand",
|
|
"float64bits",
|
|
"funcPC",
|
|
"getArgInfoFast",
|
|
"getm",
|
|
"isDirectIface",
|
|
"itabHashFunc",
|
|
"noescape",
|
|
"pcvalueCacheKey",
|
|
"readUnaligned32",
|
|
"readUnaligned64",
|
|
"releasem",
|
|
"round",
|
|
"roundupsize",
|
|
"stackmapdata",
|
|
"stringStructOf",
|
|
"subtract1",
|
|
"subtractb",
|
|
"tophash",
|
|
"totaldefersize",
|
|
"(*bmap).keys",
|
|
"(*bmap).overflow",
|
|
"(*waitq).enqueue",
|
|
|
|
// GC-related ones
|
|
"cgoInRange",
|
|
"gclinkptr.ptr",
|
|
"guintptr.ptr",
|
|
"heapBits.bits",
|
|
"heapBits.isPointer",
|
|
"heapBits.morePointers",
|
|
"heapBits.next",
|
|
"heapBitsForAddr",
|
|
"markBits.isMarked",
|
|
"muintptr.ptr",
|
|
"puintptr.ptr",
|
|
"spanOf",
|
|
"spanOfUnchecked",
|
|
//"(*gcWork).putFast", // TODO(austin): For debugging #27993
|
|
"(*gcWork).tryGetFast",
|
|
"(*guintptr).set",
|
|
"(*markBits).advance",
|
|
"(*mspan).allocBitsForIndex",
|
|
"(*mspan).base",
|
|
"(*mspan).markBitsForBase",
|
|
"(*mspan).markBitsForIndex",
|
|
"(*muintptr).set",
|
|
"(*puintptr).set",
|
|
},
|
|
"runtime/internal/sys": {},
|
|
"runtime/internal/math": {
|
|
"MulUintptr",
|
|
},
|
|
"bytes": {
|
|
"(*Buffer).Bytes",
|
|
"(*Buffer).Cap",
|
|
"(*Buffer).Len",
|
|
"(*Buffer).Grow",
|
|
"(*Buffer).Next",
|
|
"(*Buffer).Read",
|
|
"(*Buffer).ReadByte",
|
|
"(*Buffer).Reset",
|
|
"(*Buffer).String",
|
|
"(*Buffer).UnreadByte",
|
|
"(*Buffer).tryGrowByReslice",
|
|
},
|
|
"compress/flate": {
|
|
"byLiteral.Len",
|
|
"byLiteral.Less",
|
|
"byLiteral.Swap",
|
|
},
|
|
"unicode/utf8": {
|
|
"FullRune",
|
|
"FullRuneInString",
|
|
"RuneLen",
|
|
"ValidRune",
|
|
},
|
|
"reflect": {
|
|
"Value.CanAddr",
|
|
"Value.CanSet",
|
|
"Value.CanInterface",
|
|
"Value.IsValid",
|
|
"Value.pointer",
|
|
"add",
|
|
"align",
|
|
"flag.mustBe",
|
|
"flag.mustBeAssignable",
|
|
"flag.mustBeExported",
|
|
"flag.kind",
|
|
"flag.ro",
|
|
},
|
|
"regexp": {
|
|
"(*bitState).push",
|
|
},
|
|
"math/big": {
|
|
"bigEndianWord",
|
|
},
|
|
}
|
|
|
|
if runtime.GOARCH != "386" && runtime.GOARCH != "mips64" && runtime.GOARCH != "mips64le" {
|
|
// nextFreeFast calls sys.Ctz64, which on 386 is implemented in asm and is not inlinable.
|
|
// We currently don't have midstack inlining so nextFreeFast is also not inlinable on 386.
|
|
// On MIPS64x, Ctz64 is not intrinsified and causes nextFreeFast too expensive to inline
|
|
// (Issue 22239).
|
|
want["runtime"] = append(want["runtime"], "nextFreeFast")
|
|
}
|
|
if runtime.GOARCH != "386" {
|
|
// As explained above, Ctz64 and Ctz32 are not Go code on 386.
|
|
// The same applies to Bswap32.
|
|
want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz64")
|
|
want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Ctz32")
|
|
want["runtime/internal/sys"] = append(want["runtime/internal/sys"], "Bswap32")
|
|
}
|
|
if bits.UintSize == 64 {
|
|
// rotl_31 is only defined on 64-bit architectures
|
|
want["runtime"] = append(want["runtime"], "rotl_31")
|
|
}
|
|
|
|
switch runtime.GOARCH {
|
|
case "nacl", "386", "wasm", "arm":
|
|
default:
|
|
// TODO(mvdan): As explained in /test/inline_sync.go, some
|
|
// architectures don't have atomic intrinsics, so these go over
|
|
// the inlining budget. Move back to the main table once that
|
|
// problem is solved.
|
|
want["sync"] = []string{
|
|
"(*Mutex).Lock",
|
|
"(*Mutex).Unlock",
|
|
"(*RWMutex).RLock",
|
|
"(*RWMutex).RUnlock",
|
|
"(*Once).Do",
|
|
}
|
|
}
|
|
|
|
// Functions that must actually be inlined; they must have actual callers.
|
|
must := map[string]bool{
|
|
"compress/flate.byLiteral.Len": true,
|
|
"compress/flate.byLiteral.Less": true,
|
|
"compress/flate.byLiteral.Swap": true,
|
|
}
|
|
|
|
notInlinedReason := make(map[string]string)
|
|
pkgs := make([]string, 0, len(want))
|
|
for pname, fnames := range want {
|
|
pkgs = append(pkgs, pname)
|
|
for _, fname := range fnames {
|
|
fullName := pname + "." + fname
|
|
if _, ok := notInlinedReason[fullName]; ok {
|
|
t.Errorf("duplicate func: %s", fullName)
|
|
}
|
|
notInlinedReason[fullName] = "unknown reason"
|
|
}
|
|
}
|
|
|
|
args := append([]string{"build", "-a", "-gcflags=all=-m -m"}, pkgs...)
|
|
cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), args...))
|
|
pr, pw := io.Pipe()
|
|
cmd.Stdout = pw
|
|
cmd.Stderr = pw
|
|
cmdErr := make(chan error, 1)
|
|
go func() {
|
|
cmdErr <- cmd.Run()
|
|
pw.Close()
|
|
}()
|
|
scanner := bufio.NewScanner(pr)
|
|
curPkg := ""
|
|
canInline := regexp.MustCompile(`: can inline ([^ ]*)`)
|
|
haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`)
|
|
cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`)
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
if strings.HasPrefix(line, "# ") {
|
|
curPkg = line[2:]
|
|
continue
|
|
}
|
|
if m := haveInlined.FindStringSubmatch(line); m != nil {
|
|
fname := m[1]
|
|
delete(notInlinedReason, curPkg+"."+fname)
|
|
continue
|
|
}
|
|
if m := canInline.FindStringSubmatch(line); m != nil {
|
|
fname := m[1]
|
|
fullname := curPkg + "." + fname
|
|
// If function must be inlined somewhere, being inlinable is not enough
|
|
if _, ok := must[fullname]; !ok {
|
|
delete(notInlinedReason, fullname)
|
|
continue
|
|
}
|
|
}
|
|
if m := cannotInline.FindStringSubmatch(line); m != nil {
|
|
fname, reason := m[1], m[2]
|
|
fullName := curPkg + "." + fname
|
|
if _, ok := notInlinedReason[fullName]; ok {
|
|
// cmd/compile gave us a reason why
|
|
notInlinedReason[fullName] = reason
|
|
}
|
|
continue
|
|
}
|
|
}
|
|
if err := <-cmdErr; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := scanner.Err(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for fullName, reason := range notInlinedReason {
|
|
t.Errorf("%s was not inlined: %s", fullName, reason)
|
|
}
|
|
}
|