Call to empty functions

1,232 views
Skip to first unread message

Michael Banzon

unread,
Aug 30, 2015, 1:19:29 AM8/30/15
to golang-nuts
Hey,

Does the Go compiler omptimize/remove calls to empty function bodys?

See http://play.golang.org/p/tZ2nx1db4q - would the call to "empty" be compiled into the binary?

Brad Fitzpatrick

unread,
Aug 30, 2015, 1:27:10 AM8/30/15
to Michael Banzon, golang-nuts
$ cat > empty.go
package main

func main() {
empty()
}

func empty() {
}
^D

$ go tool compile -m empty.go
empty.go:7: can inline empty
empty.go:3: can inline main
empty.go:4: inlining call to empty

$ go tool compile -S empty.go
"".main t=1 size=16 value=0 args=0x0 locals=0x0
0x0000 00000 (empty.go:3) TEXT "".main(SB), $0-0
0x0000 00000 (empty.go:3) NOP
0x0000 00000 (empty.go:3) NOP
0x0000 00000 (empty.go:3) FUNCDATA $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 00000 (empty.go:3) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 00000 (empty.go:5) RET
0x0000 c3                                               .
"".empty t=1 size=16 value=0 args=0x0 locals=0x0
0x0000 00000 (empty.go:7) TEXT "".empty(SB), $0-0
0x0000 00000 (empty.go:7) NOP
0x0000 00000 (empty.go:7) NOP
0x0000 00000 (empty.go:7) FUNCDATA $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 00000 (empty.go:7) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 00000 (empty.go:8) RET
0x0000 c3                                               .
"".init t=1 size=80 value=0 args=0x0 locals=0x0
0x0000 00000 (empty.go:8) TEXT "".init(SB), $0-0
0x0000 00000 (empty.go:8) MOVQ (TLS), CX
0x0009 00009 (empty.go:8) CMPQ SP, 16(CX)
0x000d 00013 (empty.go:8) JLS 62
0x000f 00015 (empty.go:8) NOP
0x000f 00015 (empty.go:8) NOP
0x000f 00015 (empty.go:8) FUNCDATA $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x000f 00015 (empty.go:8) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x000f 00015 (empty.go:8) MOVBQZX "".initdone·(SB), BX
0x0016 00022 (empty.go:8) CMPB BL, $0
0x0019 00025 (empty.go:8) JEQ 47
0x001b 00027 (empty.go:8) MOVBQZX "".initdone·(SB), BX
0x0022 00034 (empty.go:8) CMPB BL, $2
0x0025 00037 (empty.go:8) JNE 40
0x0027 00039 (empty.go:8) RET
0x0028 00040 (empty.go:8) PCDATA $0, $0
0x0028 00040 (empty.go:8) CALL runtime.throwinit(SB)
0x002d 00045 (empty.go:8) UNDEF
0x002f 00047 (empty.go:8) MOVB $1, "".initdone·(SB)
0x0036 00054 (empty.go:8) MOVB $2, "".initdone·(SB)
0x003d 00061 (empty.go:8) RET
0x003e 00062 (empty.go:8) CALL runtime.morestack_noctxt(SB)
0x0043 00067 (empty.go:8) JMP 0
0x0000 65 48 8b 0c 25 00 00 00 00 48 3b 61 10 76 2f 0f  eH..%....H;a.v/.
0x0010 b6 1d 00 00 00 00 80 fb 00 74 14 0f b6 1d 00 00  .........t......
0x0020 00 00 80 fb 02 75 01 c3 e8 00 00 00 00 0f 0b c6  .....u..........
0x0030 05 00 00 00 00 01 c6 05 00 00 00 00 02 c3 e8 00  ................
0x0040 00 00 00 eb bb                                   .....
rel 5+4 t=13 +0
rel 18+4 t=11 "".initdone·+0
rel 30+4 t=11 "".initdone·+0
rel 41+4 t=5 runtime.throwinit+0
rel 49+4 t=11 "".initdone·+-1
rel 56+4 t=11 "".initdone·+-1
rel 63+4 t=5 runtime.morestack_noctxt+0
gclocals·33cdeccccebe80329f1fdbee7f5874cb t=7 dupok size=8 value=0
0x0000 01 00 00 00 00 00 00 00                          ........
gclocals·33cdeccccebe80329f1fdbee7f5874cb t=7 dupok size=8 value=0
0x0000 01 00 00 00 00 00 00 00                          ........
gclocals·33cdeccccebe80329f1fdbee7f5874cb t=7 dupok size=8 value=0
0x0000 01 00 00 00 00 00 00 00                          ........
gclocals·33cdeccccebe80329f1fdbee7f5874cb t=7 dupok size=8 value=0
0x0000 01 00 00 00 00 00 00 00                          ........
gclocals·33cdeccccebe80329f1fdbee7f5874cb t=7 dupok size=8 value=0
0x0000 01 00 00 00 00 00 00 00                          ........
gclocals·33cdeccccebe80329f1fdbee7f5874cb t=7 dupok size=8 value=0
0x0000 01 00 00 00 00 00 00 00                          ........
"".initdone· t=23 size=1 value=0
"".main·f t=7 dupok size=8 value=0
0x0000 00 00 00 00 00 00 00 00                          ........
rel 0+8 t=1 "".main+0
"".empty·f t=7 dupok size=8 value=0
0x0000 00 00 00 00 00 00 00 00                          ........
rel 0+8 t=1 "".empty+0
"".init·f t=7 dupok size=8 value=0
0x0000 00 00 00 00 00 00 00 00                          ........
rel 0+8 t=1 "".init+0
runtime.throwinit·f t=7 dupok size=8 value=0
0x0000 00 00 00 00 00 00 00 00                          ........
rel 0+8 t=1 runtime.throwinit+0
go.string.hdr."runtime" t=7 dupok size=16 value=0
0x0000 00 00 00 00 00 00 00 00 07 00 00 00 00 00 00 00  ................
rel 0+8 t=1 go.string."runtime"+0
go.string."runtime" t=7 dupok size=8 value=0
0x0000 72 75 6e 74 69 6d 65 00                          runtime.
go.importpath.runtime. t=7 dupok size=16 value=0
0x0000 00 00 00 00 00 00 00 00 07 00 00 00 00 00 00 00  ................
rel 0+8 t=1 go.string."runtime"+0
runtime.zerovalue t=7 dupok size=0 value=0
brad5k:~ $ 


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Banzon

unread,
Aug 30, 2015, 1:36:56 AM8/30/15
to Brad Fitzpatrick, golang-nuts
Thanks Brad.

I am currently considering doing a compile time substitution of logging functions depending on log level - nothing fancy just replacing some log functions with empty implementations if the proper build flag is used. I guess this won't be a significant overhead at runtime if all these calls (still in the code) are simply inlined.

PS. "go tool compile" <-- most sneaky awesome way to force me to (finally) upgrade to 1.5 ;-P

Benjamin Measures

unread,
Aug 30, 2015, 6:17:24 PM8/30/15
to golang-nuts
> I guess this won't be a significant overhead at runtime if all these calls (still in the code) are simply inlined.

The args would still need to be evaluated though (even for nop functions), and this can be expensive in some cases.

Check out Rob Pike's glog for how it avoids this cost in a neat way.
Reply all
Reply to author
Forward
0 new messages