Skip to content

Commit be64a19

Browse files
committed
cmd/compile, cmd/link, runtime: make defers low-cost through inline code and extra funcdata
Generate inline code at defer time to save the args of defer calls to unique (autotmp) stack slots, and generate inline code at exit time to check which defer calls were made and make the associated function/method/interface calls. We remember that a particular defer statement was reached by storing in the deferBits variable (always stored on the stack). At exit time, we check the bits of the deferBits variable to determine which defer function calls to make (in reverse order). These low-cost defers are only used for functions where no defers appear in loops. In addition, we don't do these low-cost defers if there are too many defer statements or too many exits in a function (to limit code increase). When a function uses open-coded defers, we produce extra FUNCDATA_OpenCodedDeferInfo information that specifies the number of defers, and for each defer, the stack slots where the closure and associated args have been stored. The funcdata also includes the location of the deferBits variable. Therefore, for panics, we can use this funcdata to determine exactly which defers are active, and call the appropriate functions/methods/closures with the correct arguments for each active defer. In order to unwind the stack correctly after a recover(), we need to add an extra code segment to functions with open-coded defers that simply calls deferreturn() and returns. This segment is not reachable by the normal function, but is returned to by the runtime during recovery. We set the liveness information of this deferreturn() to be the same as the liveness at the first function call during the last defer exit code (so all return values and all stack slots needed by the defer calls will be live). I needed to increase the stackguard constant from 880 to 896, because of a small amount of new code in deferreturn(). The -N flag disables open-coded defers. '-d defer' prints out the kind of defer being used at each defer statement (heap-allocated, stack-allocated, or open-coded). Cost of defer statement [ go test -run NONE -bench BenchmarkDefer$ runtime ] With normal (stack-allocated) defers only: 35.4 ns/op With open-coded defers: 5.6 ns/op Cost of function call alone (remove defer keyword): 4.4 ns/op Text size increase (including funcdata) for go binary without/with open-coded defers: 0.09% The average size increase (including funcdata) for only the functions that use open-coded defers is 1.1%. The cost of a panic followed by a recover got noticeably slower, since panic processing now requires a scan of the stack for open-coded defer frames. This scan is required, even if no frames are using open-coded defers: Cost of panic and recover [ go test -run NONE -bench BenchmarkPanicRecover runtime ] Without open-coded defers: 62.0 ns/op With open-coded defers: 255 ns/op A CGO Go-to-C-to-Go benchmark got noticeably faster because of open-coded defers: CGO Go-to-C-to-Go benchmark [cd misc/cgo/test; go test -run NONE -bench BenchmarkCGoCallback ] Without open-coded defers: 443 ns/op With open-coded defers: 347 ns/op Updates #14939 (defer performance) Updates #34481 (design doc) Change-Id: I63b1a60d1ebf28126f55ee9fd7ecffe9cb23d1ff Reviewed-on: https://go-review.googlesource.com/c/go/+/202340 Reviewed-by: Austin Clements <[email protected]>
1 parent dc77dc2 commit be64a19

28 files changed

+1194
-137
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,9 @@ func (e *Escape) augmentParamHole(k EscHole, where *Node) EscHole {
880880
// non-transient location to avoid arguments from being
881881
// transiently allocated.
882882
if where.Op == ODEFER && e.loopDepth == 1 {
883-
where.Esc = EscNever // force stack allocation of defer record (see ssa.go)
883+
// force stack allocation of defer record, unless open-coded
884+
// defers are used (see ssa.go)
885+
where.Esc = EscNever
884886
return e.later(k)
885887
}
886888

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

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ var (
5353
Debug_typecheckinl int
5454
Debug_gendwarfinl int
5555
Debug_softfloat int
56+
Debug_defer int
5657
)
5758

5859
// Debug arguments.
@@ -83,6 +84,7 @@ var debugtab = []struct {
8384
{"typecheckinl", "eager typechecking of inline function bodies", &Debug_typecheckinl},
8485
{"dwarfinl", "print information about DWARF inlined function creation", &Debug_gendwarfinl},
8586
{"softfloat", "force compiler to emit soft-float code", &Debug_softfloat},
87+
{"defer", "print information about defer compilation", &Debug_defer},
8688
}
8789

8890
const debugHelpHeader = `usage: -d arg[,arg]* and arg is <key>[=<value>]

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

+3
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ func addGCLocals() {
294294
}
295295
ggloblsym(x, int32(len(x.P)), attr)
296296
}
297+
if x := s.Func.OpenCodedDeferInfo; x != nil {
298+
ggloblsym(x, int32(len(x.P)), obj.RODATA|obj.DUPOK)
299+
}
297300
}
298301
}
299302

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,16 @@ func (lv *Liveness) solve() {
863863
newliveout.vars.Set(pos)
864864
}
865865
case ssa.BlockExit:
866-
// panic exit - nothing to do
866+
if lv.fn.Func.HasDefer() && !lv.fn.Func.OpenCodedDeferDisallowed() {
867+
// All stack slots storing args for open-coded
868+
// defers are live at panic exit (since they
869+
// will be used in running defers)
870+
for i, n := range lv.vars {
871+
if n.Name.OpenDeferSlot() {
872+
newliveout.vars.Set(int32(i))
873+
}
874+
}
875+
}
867876
default:
868877
// A variable is live on output from this block
869878
// if it is live on input to some successor.

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

+4
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ func deferstruct(stksize int64) *types.Type {
317317
makefield("siz", types.Types[TUINT32]),
318318
makefield("started", types.Types[TBOOL]),
319319
makefield("heap", types.Types[TBOOL]),
320+
makefield("openDefer", types.Types[TBOOL]),
320321
makefield("sp", types.Types[TUINTPTR]),
321322
makefield("pc", types.Types[TUINTPTR]),
322323
// Note: the types here don't really matter. Defer structures
@@ -325,6 +326,9 @@ func deferstruct(stksize int64) *types.Type {
325326
makefield("fn", types.Types[TUINTPTR]),
326327
makefield("_panic", types.Types[TUINTPTR]),
327328
makefield("link", types.Types[TUINTPTR]),
329+
makefield("framepc", types.Types[TUINTPTR]),
330+
makefield("varp", types.Types[TUINTPTR]),
331+
makefield("fd", types.Types[TUINTPTR]),
328332
makefield("args", argtype),
329333
}
330334

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestSizeof(t *testing.T) {
2020
_32bit uintptr // size on 32bit platforms
2121
_64bit uintptr // size on 64bit platforms
2222
}{
23-
{Func{}, 116, 208},
23+
{Func{}, 124, 224},
2424
{Name{}, 32, 56},
2525
{Param{}, 24, 48},
2626
{Node{}, 76, 128},

0 commit comments

Comments
 (0)