1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // WARNING: Please avoid updating this file. If this file needs to be updated,
6 // then a new devirt.pprof file should be generated:
8 // $ cd $GOROOT/src/cmd/compile/internal/test/testdata/pgo/devirtualize/
9 // $ go mod init example.com/pgo/devirtualize
10 // $ go test -bench=. -cpuprofile ./devirt.pprof
14 import "example.com/pgo/devirtualize/mult"
18 type Adder interface {
24 func (Add) Add(a, b int) int {
25 for i := 0; i < 1000; i++ {
33 func (Sub) Add(a, b int) int {
34 for i := 0; i < 1000; i++ {
40 // Exercise calls mostly a1 and m1.
43 func Exercise(iter int, a1, a2 Adder, m1, m2 mult.Multiplier) {
44 for i := 0; i < iter; i++ {
52 // N.B. Profiles only distinguish calls on a per-line level,
53 // making the two calls ambiguous. However because the
54 // interfaces and implementations are mutually exclusive,
55 // devirtualization can still select the correct callee for
58 // If they were not mutually exclusive (for example, two Add
59 // calls), then we could not definitively select the correct
61 sink += m.Multiply(42, a.Add(1, 2))
66 // TODO: until https://golang.org/cl/497175 or similar lands,
67 // we need to create an explicit reference to callees
68 // in another package for devirtualization to work.