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条)

报告相同问题?

悬赏问题

  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?