dstt1818 2017-10-19 19:29
浏览 72
已采纳

为什么反射Type.Implements()比类型断言这么慢?

I'm trying to efficiently test whether an interface{} implements a given function and my solution is to create an interface with just this function and then check whether the interface{} implements this single function interface. The two options here seem to be either using reflection or a type assertion. Both seem to have identical behaviour however there is a large speed difference.

Looking at the code for Value.Implements() it does a linear scan over the functions defined on the value and compares them against the interface. The type assertion however just seems to do a constant time comparison (independent of the number of functions in the interface).

Is there a reason why Implements() doesn't just do a type assertion?

Benchmark:

package benchmarks

import (
    "reflect"
    "testing"
)

type ITest interface {
    Foo()
}

type Base struct{}

func (Base) A() {}
func (Base) B() {}
func (Base) C() {}
func (Base) D() {}
func (Base) E() {}
func (Base) F() {}
func (Base) G() {}
func (Base) H() {}
func (Base) I() {}
func (Base) J() {}

var Interface = reflect.TypeOf((*ITest)(nil)).Elem()

func BenchmarkReflection(b *testing.B) {
    var iface interface{}
    iface = Base{}
    for i := 0; i < b.N; i++ {
        if reflect.TypeOf(iface).Implements(Interface) {
            b.FailNow()
        }
    }
}

func BenchmarkAssertion(b *testing.B) {
    var iface interface{}
    iface = Base{}
    for i := 0; i < b.N; i++ {
        if _, ok := iface.(ITest); ok {
            b.FailNow()
        }
    }
}

Results:

go test -run=XXX -bench=. so_test.go
goos: linux
goarch: amd64
BenchmarkReflection-8           10000000                  208 ns/op
BenchmarkAssertion-8            200000000                9.24 ns/op
PASS
ok      command-line-arguments  5.115s
  • 写回答

1条回答 默认 最新

  • duan0531 2017-10-20 00:24
    关注

    Type assertion in Go relies on a function called runtime.assertE2I2. If you look into the code, you'll notice it relies on getitab which in turn relies on additab (in the same file).

    Now, the actual logic of checking if the given type implements an interface inside of additab is exactly the same as Implements in reflect - a linear search, which is even pointed out in this comment:

    // both inter and typ have method sorted by name,
    // and interface names are unique,
    // so can iterate over both in lock step;
    // the loop is O(ni+nt) not O(ni*nt).
    

    However, the difference is that additab actually utilises caching - the result of the type assertion is stored in a hash map, so subsequent type assertions for the same type will run in constant time, which is why you're seeing a huge difference in performance.

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP