1
0
mirror of https://github.com/golang/go synced 2024-11-23 05:00:07 -07:00
go/test/escape4.go
Russ Cox 77ccb16eb1 cmd/internal/gc: transitive inlining
Inlining refuses to inline bodies containing an actual function call, so that
if that call or a child uses runtime.Caller it cannot observe
the inlining.

However, inlining was also refusing to inline bodies that contained
function calls that were themselves inlined away. For example:

	func f() int {
		return f1()
	}

	func f1() int {
		return f2()
	}

	func f2() int {
		return 2
	}

The f2 call in f1 would be inlined, but the f1 call in f would not,
because f1's call to f2 blocked the inlining, despite itself eventually
being inlined away.

Account properly for this kind of transitive inlining and enable.

Also bump the inlining budget a bit, so that the runtime's
heapBits.next is inlined.

This reduces the time for '6g *.go' in html/template by around 12% (!).
(For what it's worth, closing Chrome reduces the time by about 17%.)

Change-Id: If1aa673bf3e583082dcfb5f223e67355c984bfc1
Reviewed-on: https://go-review.googlesource.com/5952
Reviewed-by: Austin Clements <austin@google.com>
2015-02-26 17:36:00 +00:00

58 lines
1.4 KiB
Go

// errorcheck -0 -m
// Copyright 2010 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.
// Test, using compiler diagnostic flags, that the escape analysis is working.
// Compiles but does not run. Inlining is enabled.
package foo
var p *int
func alloc(x int) *int { // ERROR "can inline alloc" "moved to heap: x"
return &x // ERROR "&x escapes to heap"
}
var f func()
func f1() {
p = alloc(2) // ERROR "inlining call to alloc" "&x escapes to heap" "moved to heap: x"
// Escape analysis used to miss inlined code in closures.
func() { // ERROR "func literal does not escape" "can inline f1.func1"
p = alloc(3) // ERROR "inlining call to alloc" "&x escapes to heap" "moved to heap: x"
}()
f = func() { // ERROR "func literal escapes to heap" "can inline f1.func2"
p = alloc(3) // ERROR "inlining call to alloc" "&x escapes to heap" "moved to heap: x"
}
f()
}
func f2() {} // ERROR "can inline f2"
// No inline for panic, recover.
func f3() { panic(1) }
func f4() { recover() }
func f5() *byte {
type T struct {
x [1]byte
}
t := new(T) // ERROR "new.T. escapes to heap"
return &t.x[0] // ERROR "&t.x.0. escapes to heap"
}
func f6() *byte {
type T struct {
x struct {
y byte
}
}
t := new(T) // ERROR "new.T. escapes to heap"
return &t.x.y // ERROR "&t.x.y escapes to heap"
}