Skip to content

Commit 632217a

Browse files
mdempskyrandall77
authored andcommitted
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 24a43e6 commit 632217a

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/cmd/internal/gc/sinit.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,14 @@ func staticinit(n *Node, out **NodeList) bool {
285285
// like staticassign but we are copying an already
286286
// initialized value r.
287287
func staticcopy(l *Node, r *Node, out **NodeList) bool {
288-
if r.Op != ONAME || r.Class != PEXTERN || r.Sym.Pkg != localpkg {
288+
if r.Op != ONAME {
289+
return false
290+
}
291+
if r.Class == PFUNC {
292+
gdata(l, r, Widthptr)
293+
return true
294+
}
295+
if r.Class != PEXTERN || r.Sym.Pkg != localpkg {
289296
return false
290297
}
291298
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 {
397404
break
398405

399406
case ONAME:
400-
if r.Class == PEXTERN && r.Sym.Pkg == localpkg {
401-
return staticcopy(l, r, out)
402-
}
407+
return staticcopy(l, r, out)
403408

404409
case OLITERAL:
405410
if iszero(r) {

0 commit comments

Comments
 (0)