Skip to content

Commit f6944c7

Browse files
committed
runtime: add TestIntendedInlining
The intent is to allow more aggressive refactoring in the runtime without silent performance changes. The test would be useful for many functions. I've seeded it with the runtime functions tophash and add; it will grow organically (or wither!) from here. Updates #21536 and #17566 Change-Id: Ib26d9cfd395e7a8844150224da0856add7bedc42 Reviewed-on: https://go-review.googlesource.com/57410 Reviewed-by: Martin Möhrmann <moehrmann@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent 6cbe5c8 commit f6944c7

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/runtime/runtime_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
package runtime_test
66

77
import (
8+
"bytes"
9+
"internal/testenv"
810
"io"
11+
"os/exec"
912
. "runtime"
1013
"runtime/debug"
1114
"strings"
@@ -354,3 +357,42 @@ func TestVersion(t *testing.T) {
354357
t.Fatalf("cr/nl in version: %q", vers)
355358
}
356359
}
360+
361+
// TestIntendedInlining tests that specific runtime functions are inlined.
362+
// This allows refactoring for code clarity and re-use without fear that
363+
// changes to the compiler will cause silent performance regressions.
364+
func TestIntendedInlining(t *testing.T) {
365+
if testing.Short() {
366+
t.Skip("skipping in short mode")
367+
}
368+
testenv.MustHaveGoRun(t)
369+
t.Parallel()
370+
371+
// want is the list of function names that should be inlined.
372+
want := []string{"tophash", "add"}
373+
374+
m := make(map[string]bool, len(want))
375+
for _, s := range want {
376+
m[s] = true
377+
}
378+
379+
cmd := testEnv(exec.Command(testenv.GoToolPath(t), "build", "-gcflags=-m", "runtime"))
380+
out, err := cmd.CombinedOutput()
381+
if err != nil {
382+
t.Logf("%s", out)
383+
t.Fatal(err)
384+
}
385+
lines := bytes.Split(out, []byte{'\n'})
386+
for _, x := range lines {
387+
f := bytes.Split(x, []byte(": can inline "))
388+
if len(f) < 2 {
389+
continue
390+
}
391+
fn := bytes.TrimSpace(f[1])
392+
delete(m, string(fn))
393+
}
394+
395+
for s := range m {
396+
t.Errorf("function %s not inlined", s)
397+
}
398+
}

0 commit comments

Comments
 (0)