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

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格