dongyu2764 2017-08-29 11:25
浏览 127
已采纳

在Go中,不捕获的闭包会损害性能吗?

For instance, github.com/yhat/scrape suggests using a closure like this:

func someFunc() {
    ...
    matcher := func(n *html.Node) bool {
        return n.DataAtom == atom.Body
    }
    body, ok := scrape.Find(root, matcher)
    ...
}

Since matcher doesn’t actually capture any local variables, this could equivalently be written as:

func someFunc() {
    ...
    body, ok := scrape.Find(root, matcher)
    ...
}

func matcher(n *html.Node) bool {
    return n.DataAtom == atom.Body
}

The first form looks better, because the matcher function is quite specific to that place in the code. But does it perform worse at runtime (assuming someFunc may be called often)?

I guess there must be some overhead to creating a closure, but this kind of closure could be optimized into a regular function by the compiler?

(Obviously the language spec doesn’t require this; I’m interested in what gc actually does.)

  • 写回答

3条回答 默认 最新

  • duanhuang3074 2018-01-04 14:46
    关注

    It seems like there is no difference. We can check in the generated machine code.

    Here is a toy program:

    package main
    
    import "fmt"
    
    func topLevelFunction(x int) int {
        return x + 4
    }
    
    func useFunction(fn func(int) int) {
        fmt.Println(fn(10))
    }
    
    func invoke() {
        innerFunction := func(x int) int {
            return x + 8
        }
        useFunction(topLevelFunction)
        useFunction(innerFunction)
    }
    
    func main() {
        invoke()
    }
    

    And here is its disassembly:

    $ go version
    go version go1.8.5 linux/amd64
    
    $ go tool objdump -s 'main\.(invoke|topLevel)' bin/toy 
    TEXT main.topLevelFunction(SB) /home/vasiliy/cur/work/learn-go/src/my/toy/toy.go
        toy.go:6    0x47b7a0    488b442408  MOVQ 0x8(SP), AX    
        toy.go:6    0x47b7a5    4883c004    ADDQ $0x4, AX       
        toy.go:6    0x47b7a9    4889442410  MOVQ AX, 0x10(SP)   
        toy.go:6    0x47b7ae    c3      RET         
    
    TEXT main.invoke(SB) /home/vasiliy/cur/work/learn-go/src/my/toy/toy.go
        toy.go:13   0x47b870    64488b0c25f8ffffff  FS MOVQ FS:0xfffffff8, CX       
        toy.go:13   0x47b879    483b6110        CMPQ 0x10(CX), SP           
        toy.go:13   0x47b87d    7638            JBE 0x47b8b7                
        toy.go:13   0x47b87f    4883ec10        SUBQ $0x10, SP              
        toy.go:13   0x47b883    48896c2408      MOVQ BP, 0x8(SP)            
        toy.go:13   0x47b888    488d6c2408      LEAQ 0x8(SP), BP            
        toy.go:17   0x47b88d    488d052cfb0200      LEAQ 0x2fb2c(IP), AX            
        toy.go:17   0x47b894    48890424        MOVQ AX, 0(SP)              
        toy.go:17   0x47b898    e813ffffff      CALL main.useFunction(SB)       
        toy.go:14   0x47b89d    488d0514fb0200      LEAQ 0x2fb14(IP), AX            
        toy.go:18   0x47b8a4    48890424        MOVQ AX, 0(SP)              
        toy.go:18   0x47b8a8    e803ffffff      CALL main.useFunction(SB)       
        toy.go:19   0x47b8ad    488b6c2408      MOVQ 0x8(SP), BP            
        toy.go:19   0x47b8b2    4883c410        ADDQ $0x10, SP              
        toy.go:19   0x47b8b6    c3          RET                 
        toy.go:13   0x47b8b7    e874f7fcff      CALL runtime.morestack_noctxt(SB)   
        toy.go:13   0x47b8bc    ebb2            JMP main.invoke(SB)         
    
    TEXT main.invoke.func1(SB) /home/vasiliy/cur/work/learn-go/src/my/toy/toy.go
        toy.go:15   0x47b8f0    488b442408  MOVQ 0x8(SP), AX    
        toy.go:15   0x47b8f5    4883c008    ADDQ $0x8, AX       
        toy.go:15   0x47b8f9    4889442410  MOVQ AX, 0x10(SP)   
        toy.go:15   0x47b8fe    c3      RET         
    

    As we can see, at least in this simple case, there is no structural difference in how topLevelFunction and innerFunction (invoke.func1), and their passing to useFunction, are translated to machine code.

    (It is instructive to compare this to the case where innerFunction does capture a local variable; and to the case where, moreover, innerFunction is passed via a global variable rather than a function argument — but these are left as an exercise to the reader.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器