doulei2100 2017-11-08 22:58
浏览 18
已采纳

Panic()与struct vs指针的含义?

What are the implications, if any, of calling panic(x) with a large struct, versus calling panic(&x) with a pointer to that struct instead?

Does the interface{} that you pass to panic get copied over each time the stack unwinds a level, or is there some other magic going on?

EDIT: An example of where this could be important is inside http.Serve, where it will recover from any panic and serve a suitable message. If I panic with a really large struct, this could have some performance impact as the stack frames unwind and cause undue load to my webserver.

  • 写回答

1条回答 默认 最新

  • doumei1203 2017-11-09 00:14
    关注

    It depends on how many times panics are executed, on value or pointer argument, on the size of the struct, and so on. In Go, arguments are passed by value. Interface values are represented as a two-word pair giving a pointer to information about the type stored in the interface and a pointer to a copy of the associated data.

    Use the Go testing package benchmark facilities. For example,

    package main
    
    import "testing"
    
    var sink int
    
    func panic(interface{}) {}
    
    func BenchmarkPanicVal1K(b *testing.B) {
        var large struct{ big [1024]byte }
        b.ReportAllocs()
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
            panic(large)
        }
    }
    
    func BenchmarkPanicPtr1K(b *testing.B) {
        var large struct{ big [1024]byte }
        b.ReportAllocs()
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
            panic(&large)
        }
    }
    
    func BenchmarkPanicVal4K(b *testing.B) {
        var large struct{ big [4096]byte }
        b.ReportAllocs()
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
            panic(large)
        }
    }
    
    func BenchmarkPanicPtr4K(b *testing.B) {
        var large struct{ big [4096]byte }
        b.ReportAllocs()
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
            panic(&large)
        }
    }
    
    func BenchmarkIfaceVal1K(b *testing.B) {
        var iface interface{}
        var large struct{ big [1024]byte }
        b.ReportAllocs()
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
            iface = large
        }
        _ = iface
    }
    
    func BenchmarkIfacePtr1K(b *testing.B) {
        var iface interface{}
        var large struct{ big [1024]byte }
        b.ReportAllocs()
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
            iface = &large
        }
        _ = iface
    }
    func BenchmarkIfaceVal4K(b *testing.B) {
        var iface interface{}
        var large struct{ big [4096]byte }
        b.ReportAllocs()
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
            iface = large
        }
        _ = iface
    }
    
    func BenchmarkIfacePtr4K(b *testing.B) {
        var iface interface{}
        var large struct{ big [4096]byte }
        b.ReportAllocs()
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
            iface = &large
        }
        _ = iface
    }
    

    Output:

    $ gotest panic_test.go -bench=.
    
    BenchmarkPanicVal1K-4      20000000      70.3 ns/op        0 B/op    0 allocs/op
    BenchmarkPanicPtr1K-4    2000000000       0.36 ns/op       0 B/op    0 allocs/op
    BenchmarkPanicVal4K-4       1000000    1483 ns/op       4096 B/op    1 allocs/op
    BenchmarkPanicPtr4K-4    2000000000       0.36 ns/op       0 B/op    0 allocs/op
    
    
    BenchmarkIfaceVal1K-4       5000000     378 ns/op       1024 B/op    1 allocs/op
    BenchmarkIfacePtr1K-4    2000000000       0.36 ns/op       0 B/op    0 allocs/op
    BenchmarkIfaceVal4K-4       1000000    1469 ns/op       4096 B/op    1 allocs/op
    BenchmarkIfacePtr4K-4    2000000000       0.36 ns/op       0 B/op    0 allocs/op
    

    References:

    Go Data Structures: Interfaces

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页