This is a creation in Article, where the information may have evolved or changed. There is a special keyword ' defer ' in the Go language. For more information about it, see [here] (https://blog.golang.org/defer-panic-and-recover). The ' defer ' statement appends a function to the list of function calls. This list is called in turn when the function returns. ' Defer ' is commonly used for various cleanup operations. But ' defer ' itself is a cost. Using the Go benchmark tool we can quantify this overhead. The following two functions do the same work. One uses the ' defer ' statement while the other does not use: ' ' Gopackage mainfunc donodefer (t *int) {func () {*t++} ()}func Dodefer (t *int) {defer func () {*t++} ( } ' benchmark code: ' ' Gopackage mainimport ("testing") func Benchmarkdeferyes (b *testing. b) {t: = 0for I: = 0; i < B.N; i++ {dodefer (&t)}}func Benchmarkdeferno (b *testing. B) {t: = 0for I: = 0; i < B.N; i++ {donodefer (&t)}} "" Run benchmark on a 8-core Google Cloud Host: ' ⇒go test-v-bench benchmarkdefer-b Enchmemgoos:linuxgoarch:amd64pkg:cmdbenchmarkdeferyes-8 20000000 62.4 ns/op 0 b/op 0 allocs/opbenchmarkdeferno-8 50000 0000 3.70 Ns/op 0 b/op 0 allocs/op "and as expected, none of these functions allocate any additional memory. But ' Dodefer ' is 16 times times more expensive than ' donodefer '. We need to use disassembly code to understand why ' defer ' costs so much. The disassembly code of the function call section ' Dodefer ' and ' donodefer ' are the same. "' Main.go:10 movq 0x8 (SP), axmain.go:11 movq 0 (AX), CXmain.go:11 Incq cxmain.go:11 movq CX, 0 (AX) main.go:12 RET ' ' Donodefer ' first initialize the necessary registration work and then call ' Main.doNoDefer.func1 '. "TEXT Main.donodefer (SB) main.gomain.go:3 movq fs:0xfffffff8, Cxmain.go:3 cmpq 0x10 (CX), Spmain.go:3 Jbe 0x450b65main.go:3 subq $0x10, Spmain.go:3 movq BP, 0x8 (sp) main.go:3 Leaq 0x8 (sp), Bpmain.go:3 movq 0x18 (sp), Axmain.go:6 M Ovq AX, 0 (sp) main.go:6 call MAIN.DONODEFER.FUNC1 (SB) main.go:7 movq 0x8 (sp), Bpmain.go:7 addq $0x10, Spmain.go:7 retmain.g O:3 call Runtime.morestack_noctxt (SB) main.go:3 JMP main.donodefer (SB) "Dodefer" will also perform the necessary registration work first, but it will invoke several additional functions: the first one is ' Runtime.deferproc ', which is used to set the delay function that needs to be called. The second one is ' Runtime.deferreturn ', which automatically calls each ' defer ' statement. "TEXT Main.dodefer (SB) main.gomain.go:9 movq fs:0xfffffff8, Cxmain.go:9 cmpq 0x10 (CX), Spmain.go:9 Jbe 0x450bd3main.go : 9 Subq $0x20, Spmain.go:9 movq BP, 0x18 (sp) main.go:9 leaq 0x18 (sp), Bpmain.go:9 movq 0x28 (sp), Axmain.go:12 movq AX, 0x10 (SP) main.go:10 movl $0x8, 0 (sp) main.go:10 Leaq 0x218e3 (IP), Axmain.go:10 movq AX, 0x8 (sp) main.go:10 CAll Runtime.deferproc (SB) main.go:10 testl AX, axmain.go:10 JNE 0x450bc3main.go:13 noplmain.go:13 Call Runtime.deferreturn (SB) main.go:13 movq 0x18 (SP), bpmain.go:13 addq $0x20, spmain.go:13 retmain.go:10 NOPLmain.go:10 Call Runtime.deferreturn (SB) main.go:10 movq 0x18 (SP), Bpmain.go:10 addq $0x20, spmain.go:10 retmain.go:9 call Runtime.morestack_noctxt (SB) main.go:9 JMP main.dodefer (SB) ' Deferproc ' and ' deferreturn ' are more complex functions, They perform a series of configurations and calculations as they enter and exit functions. So, don't use the ' defer ' keyword in your hot code, because it's expensive and hard to detect.
Via:https://medium.com/i0exception/runtime-overhead-of-using-defer-in-go-7140d5c40e32
Author: Aniruddha Translator: Saberuster proofreading: polaris1119
This article by GCTT original compilation, go language Chinese network honor launches
This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove
422 Reads