原文:https://www.yt-blog.top/38912/
做Go开发的,肯定少不了用反射——解析Tag、拿字段偏移、获取类型信息,ORM、序列化、配置绑定这些地方都要用到。
但是官方的reflect包性能真的不太行,解析一个字段或Tag要花几十到几百万纳秒,调得多了,直接成性能瓶颈。
很多人只知道「反射慢」,但不知道慢在哪。咱们今天就从runtime层面分析一下,顺便搞个零拷贝的优化方案。
一、先从底层说起
要搞清楚反射的性能问题,得先知道Go底层是怎么回事。
从Go1.14开始,runtime里几个核心类型的内存布局就没变过。这是个关键点。
Go的反射包就是基于runtime层的abi实现的。
reflect/type.go- // TypeOf returns the reflection [Type] that represents the dynamic type of i.
- // If i is a nil interface value, TypeOf returns nil.
- func TypeOf(i any) Type {
- return toType(abi.TypeOf(i))
- }
复制代码 其实reflect.Type就是一个接口,上面代码里的toType()把它转成了reflect.rtype。- // rtype is the common implementation of most values.
- // It is embedded in other struct types.
- type rtype struct {
- t abi.Type
- }
- func toRType(t *abi.Type) *rtype {
- return (*rtype)(unsafe.Pointer(t))
- }
复制代码 所以最后拿到的是个abi.Type实例,reflect.rtype只是给它包了一层,提供个友好的接口。也可以换成别的类型专用结构体,但本质上都是对abi.Type的封装。
internal/abi/type.go- // Type is the runtime representation of a Go type.
- //
- // Be careful about accessing this type at build time, as the version
- // of this type in the compiler/linker may not have the same layout
- // as the version in the target binary, due to pointer width
- // differences and any experiments. Use cmd/compile/internal/rttype
- // or the functions in compiletype.go to access this type instead.
- // (TODO: this admonition applies to every type in this package.
- // Put it in some shared location?)
- type Type struct {
- Size_ uintptr
- PtrBytes uintptr // number of (prefix) bytes in the type that can contain pointers
- Hash uint32 // hash of type; avoids computation in hash tables
- TFlag TFlag // extra type information flags
- Align_ uint8 // alignment of variable with this type
- FieldAlign_ uint8 // alignment of struct field with this type
- Kind_ Kind // enumeration for C
- // function for comparing objects of this type
- // (ptr to object A, ptr to object B) -> ==?
- Equal func(unsafe.Pointer, unsafe.Pointer) bool
- // GCData stores the GC type data for the garbage collector.
- // Normally, GCData points to a bitmask that describes the
- // ptr/nonptr fields of the type. The bitmask will have at
- // least PtrBytes/ptrSize bits.
- // If the TFlagGCMaskOnDemand bit is set, GCData is instead a
- // **byte and the pointer to the bitmask is one dereference away.
- // The runtime will build the bitmask if needed.
- // (See runtime/type.go:getGCMask.)
- // Note: multiple types may have the same value of GCData,
- // including when TFlagGCMaskOnDemand is set. The types will, of course,
- // have the same pointer layout (but not necessarily the same size).
- GCData *byte
- Str NameOff // string form
- PtrToThis TypeOff // type for pointer to this type, may be zero
- }
复制代码 当然实际上结构体数据是如上结构体的扩展,同样定义在一起。
internal/abi/type.go- type StructField struct {
- Name Name // name is always non-empty
- Typ *Type // type of field
- Offset uintptr // byte offset of field
- }
- type StructType struct {
- Type
- PkgPath Name
- Fields []StructField
- }
复制代码 还有一点,这些底层类型里存的结构体元数据,是编译器编译时就写进程序的只读内存区了,地址固定、GC不回收、运行时不能改。这给直接操作底层内存提供了安全保障。
既然这样,我们可以用固定偏移量精确找到目标字段,不用完整解析整个底层结构体,只要定义几个空的镜像类型来做类型标注就够了。
二、性能瓶颈在哪儿
reflect.TypeOf()底层就是做个指针转换,不拷贝不计算,挺快的。真正的性能损耗出在后面两个阶段,而且因为没缓存,损耗被放大了好几倍。
2.1 Field方法做了无意义的内存分配
调用reflect.Type.Field(i)的时候,rtype会被转成*StructType,然后从Fields字段里读目标字段信息。
reflect/type.go- // Struct field
- type structField = abi.StructField // 注意:你平时用的是 reflect.structField,不是reflect.StructField
- // structType represents a struct type.
- type structType struct {
- abi.StructType
- }
- func (t *rtype) Field(i int) StructField {
- if t.Kind() != Struct {
- panic("reflect: Field of non-struct type " + t.String())
- }
- tt := (*structType)(unsafe.Pointer(t))
- return tt.Field(i)
- }
- // Field returns the i'th struct field.
- func (t *structType) Field(i int) (f StructField) {
- if i < 0 || i >= len(t.Fields) {
- panic("reflect: Field index out of bounds")
- }
- p := &t.Fields[i]
- f.Type = toType(p.Typ)
- f.Name = p.Name.Name()
- f.Anonymous = p.Embedded()
- if !p.Name.IsExported() {
- f.PkgPath = t.PkgPath.Name()
- }
- if tag := p.Name.Tag(); tag != "" {
- f.Tag = StructTag(tag)
- }
- f.Offset = p.Offset
- // We can't safely use this optimization on js or wasi,
- // which do not appear to support read-only data.
- if i < 256 && runtime.GOOS != "js" && runtime.GOOS != "wasip1" {
- staticuint64s := getStaticuint64s()
- p := unsafe.Pointer(&(*staticuint64s)[i])
- if unsafe.Sizeof(int(0)) == 4 && goarch.BigEndian {
- p = unsafe.Add(p, 4)
- }
- f.Index = unsafe.Slice((*int)(p), 1)
- } else {
- // NOTE(rsc): This is the only allocation in the interface
- // presented by a reflect.Type. It would be nice to avoid,
- // but we need to make sure that misbehaving clients of
- // reflect cannot affect other uses of reflect.
- // One possibility is CL 5371098, but we postponed that
- // ugliness until there is a demonstrated
- // need for the performance. This is issue 2320.
- f.Index = []int{i}
- }
- return
- }
复制代码 上面这段代码问题在哪儿呢?看f.Index = []int{i}这一行。这里无意义地创建了一个列表,实际上这个数据就是你自己传进去的i,完全没必要。这步操作纯粹是为了兼容性。
具体讨论可以看golang/go · Issue#68380。
2.2 Tag获取时的字符串拷贝
刚才说的获取字段的时候,StructField的Tag字段是StructTag类型,其实就是个string。
reflect/type.go- // A StructTag is the tag string in a struct field.
- //
- // By convention, tag strings are a concatenation of
- // optionally space-separated key:"value" pairs.
- // Each key is a non-empty string consisting of non-control
- // characters other than space (U+0020 ' '), quote (U+0022 '"'),
- // and colon (U+003A ':'). Each value is quoted using U+0022 '"'
- // characters and Go string literal syntax.
- type StructTag string
- // Get returns the value associated with key in the tag string.
- // If there is no such key in the tag, Get returns the empty string.
- // If the tag does not have the conventional format, the value
- // returned by Get is unspecified. To determine whether a tag is
- // explicitly set to the empty string, use [StructTag.Lookup].
- func (tag StructTag) Get(key string) string {
- v, _ := tag.Lookup(key)
- return v
- }
- // Lookup returns the value associated with key in the tag string.
- // If the key is present in the tag the value (which may be empty)
- // is returned. Otherwise the returned value will be the empty string.
- // The ok return value reports whether the value was explicitly set in
- // the tag string. If the tag does not have the conventional format,
- // the value returned by Lookup is unspecified.
- func (tag StructTag) Lookup(key string) (value string, ok bool) {
- // When modifying this code, also update the validateStructTag code
- // in cmd/vet/structtag.go.
- for tag != "" {
- // Skip leading space.
- i := 0
- for i < len(tag) && tag[i] == ' ' {
- i++
- }
- tag = tag[i:]
- if tag == "" {
- break
- }
- // Scan to colon. A space, a quote or a control character is a syntax error.
- // Strictly speaking, control chars include the range [0x7f, 0x9f], not just
- // [0x00, 0x1f], but in practice, we ignore the multi-byte control characters
- // as it is simpler to inspect the tag's bytes than the tag's runes.
- i = 0
- for i < len(tag) && tag[i] > ' ' && tag[i] != ':' && tag[i] != '"' && tag[i] != 0x7f {
- i++
- }
- if i == 0 || i+1 >= len(tag) || tag[i] != ':' || tag[i+1] != '"' {
- break
- }
- name := string(tag[:i])
- tag = tag[i+1:]
- // Scan quoted string to find value.
- i = 1
- for i < len(tag) && tag[i] != '"' {
- if tag[i] == '\\' {
- i++
- }
- i++
- }
- if i >= len(tag) {
- break
- }
- qvalue := string(tag[:i+1])
- tag = tag[i+1:]
- if key == name {
- value, err := strconv.Unquote(qvalue)
- if err != nil {
- break
- }
- return value, true
- }
- }
- return "", false
- }
复制代码 这里的tag[:i]和tag[i+1:]会隐式转成slice,这一步只改了栈上的元信息结构体,但是string转换过程为了保证内存安全,会触发一次内存拷贝,这一步是躲不掉的。
现在主流方案像官方的strings.Builder的String()方法,因为不需要把原始数据和新字符串隔离开,所以用的是unsafe.String(unsafe.SliceData(b.buf), len(b.buf))。
这样得到的string和buf指向同一块内存,不会触发额外的内存拷贝,而且unsafe能保证内存安全,不会被GC回收。
三、零拷贝优化的思路
针对上面说的性能瓶颈,结合Go1.14+底层类型结构固定的特点,零拷贝优化的思路其实挺简单的:
- 不用反射包那一层封装,直接对接runtime层,全程只读内存,不做任何没必要的拷贝;
- 定义几个空的镜像类型来做类型标注,不用填任何字段,用Go1.14+固定的内存偏移量精准找到目标字段;
- 解析reflect.Type接口拿到底层的原始内存地址,通过unsafe操作,用固定偏移量直接读数据;
- 搞个全局缓存存结构体元数据,每个结构体只解析一次,避免高频场景下的重复操作。
这个方案的核心逻辑跟Go底层操作完全一样,所有偏移量都是基于Go1.14+的固定布局预设的,遇到特殊版本顶多改改偏移量,不用担心兼容性问题。
四、具体实现
前面分析了半天,反射慢主要有两个问题:
- Field 方法会创建一个无意义的 []int{i} 切片(为了兼容性)
- Tag.Get 会触发字符串的内存拷贝
下面是完整的零拷贝实现:
4.1 核心定义
[code]//go:build go1.14// +build go1.14package zeroreflimport ( "reflect" "strconv" "unsafe")const ( // abiTypeSize 是 abi.Type 结构体的大小 // Go1.14+ 中固定为48字节 abiTypeSize = 48)// 空镜像类型:只做类型标注,不用填字段type rtype struct{}type structType struct { PkgPath Name Fields []structField}type structField struct { Name Name Typ *rtype Offset uintptr}// Name 类型,跟 runtime.Name 一样//go:linkname Name runtime.Nametype Name struct { Bytes *byte}// 下面这些方法都是 runtime.Name 的实现//go:linkname Name_Name runtime.(*Name).Name//go:inlinefunc (n *Name) Name() string { if n.Bytes == nil { return "" } i, l := n.ReadVarint(1) return unsafe.String(n.DataChecked(1+i, "non-empty string"), l)}//go:linkname Name_Tag runtime.(*Name).Tag//go:inlinefunc (n *Name) Tag() string { if !n.HasTag() { return "" } i, l := n.ReadVarint(1) i2, l2 := n.ReadVarint(1 + i + l) return unsafe.String(n.DataChecked(1+i+l+i2, "non-empty string"), l2)}//go:linkname Name_IsExported runtime.(*Name).IsExported//go:inlinefunc (n *Name) IsExported() bool { return (*n.Bytes)&(1 |