Skip to content

Commit dad6163

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 cmd 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: I51a389860b9676cfa1b84722f5fb84d3c4ee9e28 Reviewed-on: https://go-review.googlesource.com/c/go/+/190098 Reviewed-by: Austin Clements <[email protected]>
1 parent e94475e commit dad6163

27 files changed

+1188
-146
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,24 @@ func (e *Escape) stmt(n *Node) {
371371
e.stmts(n.Right.Ninit)
372372
e.call(e.addrs(n.List), n.Right, nil)
373373
case ORETURN:
374+
e.curfn.Func.numReturns++
374375
results := e.curfn.Type.Results().FieldSlice()
375376
for i, v := range n.List.Slice() {
376377
e.assign(asNode(results[i].Nname), v, "return", n)
377378
}
378379
case OCALLFUNC, OCALLMETH, OCALLINTER, OCLOSE, OCOPY, ODELETE, OPANIC, OPRINT, OPRINTN, ORECOVER:
379380
e.call(nil, n, nil)
380381
case OGO, ODEFER:
382+
if n.Op == ODEFER {
383+
e.curfn.Func.SetHasDefer(true)
384+
e.curfn.Func.numDefers++
385+
if e.curfn.Func.numDefers > maxOpenDefers {
386+
// Don't allow open defers if there are more than
387+
// 8 defers in the function, since we use a single
388+
// byte to record active defers.
389+
e.curfn.Func.SetOpenCodedDeferDisallowed(true)
390+
}
391+
}
381392
e.stmts(n.Left.Ninit)
382393
e.call(nil, n.Left, n)
383394

@@ -872,8 +883,13 @@ func (e *Escape) augmentParamHole(k EscHole, where *Node) EscHole {
872883
// non-transient location to avoid arguments from being
873884
// transiently allocated.
874885
if where.Op == ODEFER && e.loopDepth == 1 {
875-
where.Esc = EscNever // force stack allocation of defer record (see ssa.go)
886+
// force stack allocation of defer record, unless open-coded
887+
// defers are used (see ssa.go)
888+
where.Esc = EscNever
876889
return e.later(k)
890+
} else if where.Op == ODEFER {
891+
// If any defer occurs in a loop, open-coded defers cannot be used
892+
e.curfn.Func.SetOpenCodedDeferDisallowed(true)
877893
}
878894

879895
return e.heapHole()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ var (
5252
Debug_typecheckinl int
5353
Debug_gendwarfinl int
5454
Debug_softfloat int
55+
Debug_defer int
5556
)
5657

5758
// Debug arguments.
@@ -81,6 +82,7 @@ var debugtab = []struct {
8182
{"typecheckinl", "eager typechecking of inline function bodies", &Debug_typecheckinl},
8283
{"dwarfinl", "print information about DWARF inlined function creation", &Debug_gendwarfinl},
8384
{"softfloat", "force compiler to emit soft-float code", &Debug_softfloat},
85+
{"defer", "print information about defer compilation", &Debug_defer},
8486
}
8587

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

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

Lines changed: 3 additions & 0 deletions
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/reflect.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ func deferstruct(stksize int64) *types.Type {
338338
makefield("siz", types.Types[TUINT32]),
339339
makefield("started", types.Types[TBOOL]),
340340
makefield("heap", types.Types[TBOOL]),
341+
makefield("openDefer", types.Types[TBOOL]),
341342
makefield("sp", types.Types[TUINTPTR]),
342343
makefield("pc", types.Types[TUINTPTR]),
343344
// Note: the types here don't really matter. Defer structures
@@ -346,6 +347,9 @@ func deferstruct(stksize int64) *types.Type {
346347
makefield("fn", types.Types[TUINTPTR]),
347348
makefield("_panic", types.Types[TUINTPTR]),
348349
makefield("link", types.Types[TUINTPTR]),
350+
makefield("framepc", types.Types[TUINTPTR]),
351+
makefield("varp", types.Types[TUINTPTR]),
352+
makefield("fd", types.Types[TUINTPTR]),
349353
makefield("args", argtype),
350354
}
351355

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

Lines changed: 1 addition & 1 deletion
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)