dpbtbcz6650 2018-04-03 15:54
浏览 36

散列反射类型

I'm trying to find a quick way of performing comparisons between two []reflect.Type. Right now I have the following:

func Equal(left, right []reflect.Type) bool {
    if len(left) != len(right) {
        return false
    }
    for i := 0; i < len(left); i++ {
        if left[i] != right[i] {
            return false
        }
    }
    return true
}

Most of the slices don't change. So if I can find a way to hash them, I'd get a huge perf boost.

Background

I'm trying to (for fun) implement a form of function overloading in Go using the reflect package. The first thing I did is to convert each specialised/overloaded function into a signature type.

type Signature struct {
    Variadic bool
    In, Out  []reflect.Type
}

The idea is that, when the overloaded function gets called, I'll convert the arguments into a slice of reflect.Type and then find a Signature where the In types match.

This works, but for each comparison, it's a linear scan which is pretty slow. If I could hash the slice of []reflect.Type I could stick that in a map and get constant time lookups.

  • 写回答

1条回答 默认 最新

  • dragon321723 2018-04-03 16:31
    关注

    I ended up abusing the built-in map to assign unique ids to each reflect.Type. Then I hash those using djb2.

    type TypeCode struct {
        seq   int64
        codes map[reflect.Type]int64
    }
    
    func (td *TypeCode) TypeID(t reflect.Type) int64 {
        if code, ok := td.codes[t]; ok {
            return code
        }
        td.seq++
        td.codes[t] = td.seq
        return td.seq
    }
    
    func (td *TypeCode) SliceTypeID(tt []reflect.Type) int64 {
        id := int64(5381)
        for _, t := range tt {
            id = ((id << 5) + id) + td.TypeID(t)
        }
        return id
    }
    

    edit: I switched to a string based approach which is less efficient, but removes any potential for collisions.

    type TypeCode struct {
        seq   int64
        codes map[reflect.Type]string
    }
    
    func (td *TypeCode) TypeID(t reflect.Type) string {
        if code, ok := td.codes[t]; ok {
            return code
        }
        td.seq++
        id := strconv.FormatInt(td.seq, 10)
        td.codes[t] = id
        return id
    }
    
    func (td *TypeCode) SliceTypeID(tt []reflect.Type) string {
        ids := make([]string, len(tt))
        for i := 0; i < len(tt); i++ {
            ids[i] = td.TypeID(tt[i])
        }
        return strings.Join(ids, ".")
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教