From 632217aae47f8abd2fe8487068367a5bb5f9ea0a Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Wed, 4 Mar 2015 16:33:28 -0800 Subject: [PATCH] cmd/internal/gc: statically initialize function pointers Previously, gc would compile code like func foo() { ... } var bar = foo by emitting a static closure to wrap "foo", but then emitting runtime initialization code to assign the closure to "bar". This CL changes gc to instead statically initialize "bar". Notably, this change shrinks the "go" tool's text segment by ~7.4kB on linux/amd64 while only increasing the data segment by ~100B: text data bss dec hex filename 7237819 122412 215616 7575847 739927 go.before 7230398 122540 215232 7568170 737b2a go.after Fixes issue #10081. Change-Id: If5e26cf46b323393ba6f2199a82a06e9e4baf411 Reviewed-on: https://go-review.googlesource.com/6880 Run-TryBot: Matthew Dempsky TryBot-Result: Gobot Gobot Reviewed-by: Keith Randall --- src/cmd/internal/gc/sinit.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/cmd/internal/gc/sinit.go b/src/cmd/internal/gc/sinit.go index b66123076f..12562231fc 100644 --- a/src/cmd/internal/gc/sinit.go +++ b/src/cmd/internal/gc/sinit.go @@ -285,7 +285,14 @@ func staticinit(n *Node, out **NodeList) bool { // like staticassign but we are copying an already // initialized value r. func staticcopy(l *Node, r *Node, out **NodeList) bool { - if r.Op != ONAME || r.Class != PEXTERN || r.Sym.Pkg != localpkg { + if r.Op != ONAME { + return false + } + if r.Class == PFUNC { + gdata(l, r, Widthptr) + return true + } + if r.Class != PEXTERN || r.Sym.Pkg != localpkg { return false } if r.Defn == nil { // probably zeroed but perhaps supplied externally and of unknown value @@ -397,9 +404,7 @@ func staticassign(l *Node, r *Node, out **NodeList) bool { break case ONAME: - if r.Class == PEXTERN && r.Sym.Pkg == localpkg { - return staticcopy(l, r, out) - } + return staticcopy(l, r, out) case OLITERAL: if iszero(r) { -- 2.48.1