Skip to content

Commit 624e197

Browse files
committed
cmd/compile: decrease inlining call cost from 60 to 57
A Go user made a well-documented request for a slightly lower threshold. I tested against a selection of other people's benchmarks, and saw a tiny benefit (possibly noise) at equally tiny cost, and no unpleasant surprises observed in benchmarking. I.e., might help, doesn't hurt, low risk, request was delivered on a silver platter. It did, however, change the behavior of one test because now bytes.Buffer.Grow is eligible for inlining. Updates #19348. Change-Id: I85e3088a4911290872b8c6bda9601b5354c48695 Reviewed-on: https://go-review.googlesource.com/c/151977 Run-TryBot: David Chase <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent f70bd91 commit 624e197

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/cmd/compile/internal/gc/inl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const (
3939
inlineMaxBudget = 80
4040
inlineExtraAppendCost = 0
4141
// default is to inline if there's at most one call. -l=4 overrides this by using 1 instead.
42-
inlineExtraCallCost = inlineMaxBudget * 3 / 4
42+
inlineExtraCallCost = 57 // 57 was benchmarked to provided most benefit with no bad surprises; see https://github.com/golang/go/issues/19348#issuecomment-439370742
4343
inlineExtraPanicCost = 1 // do not penalize inlining panics.
4444
inlineExtraThrowCost = inlineMaxBudget // with current (2018-05/1.11) code, inlining runtime.throw does not help.
4545

test/fixedbugs/issue7921.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,41 @@ func bufferNotEscape() string {
1717
// copied during String() call, but object "handle" itself
1818
// can be stack-allocated.
1919
var b bytes.Buffer
20-
b.WriteString("123") // ERROR "b does not escape"
21-
b.Write([]byte{'4'}) // ERROR "b does not escape" "\[\]byte literal does not escape"
22-
return b.String() // ERROR "b does not escape" "inlining call" "string\(bytes\.b\.buf\[bytes.b.off:\]\) escapes to heap"
20+
b.WriteString("123") // ERROR "bufferNotEscape b does not escape$"
21+
b.Write([]byte{'4'}) // ERROR "bufferNotEscape \[\]byte literal does not escape$" "bufferNotEscape b does not escape$"
22+
return b.String() // ERROR "bufferNotEscape b does not escape$" "inlining call to bytes.\(\*Buffer\).String$" "string\(bytes.b.buf\[bytes.b.off:\]\) escapes to heap$"
2323
}
2424

25-
func bufferNoEscape2(xs []string) int { // ERROR "xs does not escape"
26-
b := bytes.NewBuffer(make([]byte, 0, 64)) // ERROR "inlining call" "make\(\[\]byte, 0, 64\) does not escape" "&bytes.Buffer literal does not escape"
25+
func bufferNoEscape2(xs []string) int { // ERROR "bufferNoEscape2 xs does not escape$"
26+
b := bytes.NewBuffer(make([]byte, 0, 64)) // ERROR "bufferNoEscape2 &bytes.Buffer literal does not escape$" "bufferNoEscape2 make\(\[\]byte, 0, 64\) does not escape$" "inlining call to bytes.NewBuffer$"
2727
for _, x := range xs {
2828
b.WriteString(x)
2929
}
30-
return b.Len() // ERROR "inlining call"
30+
return b.Len() // ERROR "inlining call to bytes.\(\*Buffer\).Len$"
3131
}
3232

33-
func bufferNoEscape3(xs []string) string { // ERROR "xs does not escape"
34-
b := bytes.NewBuffer(make([]byte, 0, 64)) // ERROR "inlining call" "make\(\[\]byte, 0, 64\) does not escape" "&bytes.Buffer literal does not escape"
33+
func bufferNoEscape3(xs []string) string { // ERROR "bufferNoEscape3 xs does not escape$"
34+
b := bytes.NewBuffer(make([]byte, 0, 64)) // ERROR "bufferNoEscape3 &bytes.Buffer literal does not escape$" "bufferNoEscape3 make\(\[\]byte, 0, 64\) does not escape$" "inlining call to bytes.NewBuffer$"
3535
for _, x := range xs {
3636
b.WriteString(x)
3737
b.WriteByte(',')
3838
}
39-
return b.String() // ERROR "inlining call" "string\(bytes.b.buf\[bytes\.b\.off:\]\) escapes to heap"
39+
return b.String() // ERROR "inlining call to bytes.\(\*Buffer\).String$" "string\(bytes.b.buf\[bytes.b.off:\]\) escapes to heap$"
4040
}
4141

4242
func bufferNoEscape4() []byte {
4343
var b bytes.Buffer
44-
b.Grow(64) // ERROR "b does not escape"
45-
useBuffer(&b) // ERROR "&b does not escape"
46-
return b.Bytes() // ERROR "inlining call" "b does not escape"
44+
b.Grow(64) // ERROR "bufferNoEscape4 b does not escape$" "bufferNoEscape4 ignoring self-assignment in bytes.b.buf = bytes.b.buf\[:bytes.m·3\]$" "inlining call to bytes.\(\*Buffer\).Grow$"
45+
useBuffer(&b) // ERROR "bufferNoEscape4 &b does not escape$"
46+
return b.Bytes() // ERROR "bufferNoEscape4 b does not escape$" "inlining call to bytes.\(\*Buffer\).Bytes$"
4747
}
4848

49-
func bufferNoEscape5() { // ERROR "can inline bufferNoEscape5"
50-
b := bytes.NewBuffer(make([]byte, 0, 128)) // ERROR "inlining call" "make\(\[\]byte, 0, 128\) does not escape" "&bytes.Buffer literal does not escape"
49+
func bufferNoEscape5() { // ERROR "can inline bufferNoEscape5$"
50+
b := bytes.NewBuffer(make([]byte, 0, 128)) // ERROR "bufferNoEscape5 &bytes.Buffer literal does not escape$" "bufferNoEscape5 make\(\[\]byte, 0, 128\) does not escape$" "inlining call to bytes.NewBuffer$"
5151
useBuffer(b)
5252
}
5353

5454
//go:noinline
55-
func useBuffer(b *bytes.Buffer) { // ERROR "b does not escape"
55+
func useBuffer(b *bytes.Buffer) { // ERROR "useBuffer b does not escape$"
5656
b.WriteString("1234")
5757
}

0 commit comments

Comments
 (0)