mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-01 03:14:16 +00:00
e8e8875f94
Comparing -tags=protoreflect to fast-path: name old time/op new time/op delta pkg:google.golang.org/protobuf/internal/benchmarks goos:linux goarch:amd64 /Clone/google_message1_proto2-12 1.70µs ± 1% 0.30µs ± 1% -82.64% (p=0.001 n=7+7) /Clone/google_message1_proto3-12 1.01µs ± 1% 0.19µs ± 1% -80.77% (p=0.000 n=7+8) /Clone/google_message2-12 818µs ± 8% 141µs ± 6% -82.78% (p=0.000 n=8+8) pkg:google.golang.org/protobuf/internal/benchmarks/micro goos:linux goarch:amd64 EmptyMessage/Clone-12 51.1ns ± 1% 39.3ns ± 3% -23.03% (p=0.000 n=7+8) RepeatedInt32/Clone-12 24.5µs ± 1% 1.1µs ± 3% -95.64% (p=0.000 n=8+8) Required/Clone-12 978ns ± 1% 132ns ± 2% -86.46% (p=0.000 n=8+8) name old alloc/op new alloc/op delta pkg:google.golang.org/protobuf/internal/benchmarks goos:linux goarch:amd64 /Clone/google_message1_proto2-12 1.08kB ± 0% 0.74kB ± 0% -31.85% (p=0.000 n=8+8) /Clone/google_message1_proto3-12 872B ± 0% 544B ± 0% -37.61% (p=0.000 n=8+8) /Clone/google_message2-12 602kB ± 0% 411kB ± 0% -31.65% (p=0.000 n=8+8) pkg:google.golang.org/protobuf/internal/benchmarks/micro goos:linux goarch:amd64 EmptyMessage/Clone-12 96.0B ± 0% 64.0B ± 0% -33.33% (p=0.000 n=8+8) RepeatedInt32/Clone-12 25.4kB ± 0% 3.2kB ± 0% -87.33% (p=0.000 n=8+8) Required/Clone-12 416B ± 0% 256B ± 0% -38.46% (p=0.000 n=8+8) name old allocs/op new allocs/op delta pkg:google.golang.org/protobuf/internal/benchmarks goos:linux goarch:amd64 /Clone/google_message1_proto2-12 52.0 ± 0% 21.0 ± 0% -59.62% (p=0.000 n=8+8) /Clone/google_message1_proto3-12 33.0 ± 0% 3.0 ± 0% -90.91% (p=0.000 n=8+8) /Clone/google_message2-12 22.3k ± 0% 7.5k ± 0% -66.41% (p=0.000 n=8+8) pkg:google.golang.org/protobuf/internal/benchmarks/micro goos:linux goarch:amd64 EmptyMessage/Clone-12 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.000 n=8+8) RepeatedInt32/Clone-12 1.51k ± 0% 0.00k ± 0% -99.80% (p=0.000 n=8+8) Required/Clone-12 51.0 ± 0% 18.0 ± 0% -64.71% (p=0.000 n=8+8) Change-Id: Ife9018097c34cb025dc9c4fdd9a61b2f947853c6 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/219147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
214 lines
6.6 KiB
Go
214 lines
6.6 KiB
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package impl
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
|
|
"google.golang.org/protobuf/internal/genname"
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
|
piface "google.golang.org/protobuf/runtime/protoiface"
|
|
)
|
|
|
|
// MessageInfo provides protobuf related functionality for a given Go type
|
|
// that represents a message. A given instance of MessageInfo is tied to
|
|
// exactly one Go type, which must be a pointer to a struct type.
|
|
//
|
|
// The exported fields must be populated before any methods are called
|
|
// and cannot be mutated after set.
|
|
type MessageInfo struct {
|
|
// GoReflectType is the underlying message Go type and must be populated.
|
|
GoReflectType reflect.Type // pointer to struct
|
|
|
|
// Desc is the underlying message descriptor type and must be populated.
|
|
Desc pref.MessageDescriptor
|
|
|
|
// Exporter must be provided in a purego environment in order to provide
|
|
// access to unexported fields.
|
|
Exporter exporter
|
|
|
|
// OneofWrappers is list of pointers to oneof wrapper struct types.
|
|
OneofWrappers []interface{}
|
|
|
|
initMu sync.Mutex // protects all unexported fields
|
|
initDone uint32
|
|
|
|
reflectMessageInfo // for reflection implementation
|
|
coderMessageInfo // for fast-path method implementations
|
|
}
|
|
|
|
// exporter is a function that returns a reference to the ith field of v,
|
|
// where v is a pointer to a struct. It returns nil if it does not support
|
|
// exporting the requested field (e.g., already exported).
|
|
type exporter func(v interface{}, i int) interface{}
|
|
|
|
// getMessageInfo returns the MessageInfo for any message type that
|
|
// is generated by our implementation of protoc-gen-go (for v2 and on).
|
|
// If it is unable to obtain a MessageInfo, it returns nil.
|
|
func getMessageInfo(mt reflect.Type) *MessageInfo {
|
|
m, ok := reflect.Zero(mt).Interface().(pref.ProtoMessage)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
mr, ok := m.ProtoReflect().(interface{ ProtoMessageInfo() *MessageInfo })
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return mr.ProtoMessageInfo()
|
|
}
|
|
|
|
func (mi *MessageInfo) init() {
|
|
// This function is called in the hot path. Inline the sync.Once logic,
|
|
// since allocating a closure for Once.Do is expensive.
|
|
// Keep init small to ensure that it can be inlined.
|
|
if atomic.LoadUint32(&mi.initDone) == 0 {
|
|
mi.initOnce()
|
|
}
|
|
}
|
|
|
|
func (mi *MessageInfo) initOnce() {
|
|
mi.initMu.Lock()
|
|
defer mi.initMu.Unlock()
|
|
if mi.initDone == 1 {
|
|
return
|
|
}
|
|
|
|
t := mi.GoReflectType
|
|
if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct {
|
|
panic(fmt.Sprintf("got %v, want *struct kind", t))
|
|
}
|
|
t = t.Elem()
|
|
|
|
si := mi.makeStructInfo(t)
|
|
mi.makeReflectFuncs(t, si)
|
|
mi.makeCoderMethods(t, si)
|
|
|
|
atomic.StoreUint32(&mi.initDone, 1)
|
|
}
|
|
|
|
// getPointer returns the pointer for a message, which should be of
|
|
// the type of the MessageInfo. If the message is of a different type,
|
|
// it returns ok==false.
|
|
func (mi *MessageInfo) getPointer(m pref.Message) (p pointer, ok bool) {
|
|
switch m := m.(type) {
|
|
case *messageState:
|
|
return m.pointer(), m.mi == mi
|
|
case *messageReflectWrapper:
|
|
return m.pointer(), m.mi == mi
|
|
}
|
|
return pointer{}, false
|
|
}
|
|
|
|
type (
|
|
SizeCache = int32
|
|
WeakFields = map[int32]piface.MessageV1
|
|
UnknownFields = []byte
|
|
ExtensionFields = map[int32]ExtensionField
|
|
)
|
|
|
|
var (
|
|
sizecacheType = reflect.TypeOf(SizeCache(0))
|
|
weakFieldsType = reflect.TypeOf(WeakFields(nil))
|
|
unknownFieldsType = reflect.TypeOf(UnknownFields(nil))
|
|
extensionFieldsType = reflect.TypeOf(ExtensionFields(nil))
|
|
)
|
|
|
|
type structInfo struct {
|
|
sizecacheOffset offset
|
|
weakOffset offset
|
|
unknownOffset offset
|
|
extensionOffset offset
|
|
|
|
fieldsByNumber map[pref.FieldNumber]reflect.StructField
|
|
oneofsByName map[pref.Name]reflect.StructField
|
|
oneofWrappersByType map[reflect.Type]pref.FieldNumber
|
|
oneofWrappersByNumber map[pref.FieldNumber]reflect.Type
|
|
}
|
|
|
|
func (mi *MessageInfo) makeStructInfo(t reflect.Type) structInfo {
|
|
si := structInfo{
|
|
sizecacheOffset: invalidOffset,
|
|
weakOffset: invalidOffset,
|
|
unknownOffset: invalidOffset,
|
|
extensionOffset: invalidOffset,
|
|
|
|
fieldsByNumber: map[pref.FieldNumber]reflect.StructField{},
|
|
oneofsByName: map[pref.Name]reflect.StructField{},
|
|
oneofWrappersByType: map[reflect.Type]pref.FieldNumber{},
|
|
oneofWrappersByNumber: map[pref.FieldNumber]reflect.Type{},
|
|
}
|
|
|
|
fieldLoop:
|
|
for i := 0; i < t.NumField(); i++ {
|
|
switch f := t.Field(i); f.Name {
|
|
case genname.SizeCache, genname.SizeCacheA:
|
|
if f.Type == sizecacheType {
|
|
si.sizecacheOffset = offsetOf(f, mi.Exporter)
|
|
}
|
|
case genname.WeakFields, genname.WeakFieldsA:
|
|
if f.Type == weakFieldsType {
|
|
si.weakOffset = offsetOf(f, mi.Exporter)
|
|
}
|
|
case genname.UnknownFields, genname.UnknownFieldsA:
|
|
if f.Type == unknownFieldsType {
|
|
si.unknownOffset = offsetOf(f, mi.Exporter)
|
|
}
|
|
case genname.ExtensionFields, genname.ExtensionFieldsA, genname.ExtensionFieldsB:
|
|
if f.Type == extensionFieldsType {
|
|
si.extensionOffset = offsetOf(f, mi.Exporter)
|
|
}
|
|
default:
|
|
for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
|
|
if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
|
|
n, _ := strconv.ParseUint(s, 10, 64)
|
|
si.fieldsByNumber[pref.FieldNumber(n)] = f
|
|
continue fieldLoop
|
|
}
|
|
}
|
|
if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
|
|
si.oneofsByName[pref.Name(s)] = f
|
|
continue fieldLoop
|
|
}
|
|
}
|
|
}
|
|
|
|
// Derive a mapping of oneof wrappers to fields.
|
|
oneofWrappers := mi.OneofWrappers
|
|
if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
|
|
oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{})
|
|
}
|
|
if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
|
|
oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{})
|
|
}
|
|
for _, v := range oneofWrappers {
|
|
tf := reflect.TypeOf(v).Elem()
|
|
f := tf.Field(0)
|
|
for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
|
|
if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
|
|
n, _ := strconv.ParseUint(s, 10, 64)
|
|
si.oneofWrappersByType[tf] = pref.FieldNumber(n)
|
|
si.oneofWrappersByNumber[pref.FieldNumber(n)] = tf
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
return si
|
|
}
|
|
|
|
func (mi *MessageInfo) New() protoreflect.Message {
|
|
return mi.MessageOf(reflect.New(mi.GoReflectType.Elem()).Interface())
|
|
}
|
|
func (mi *MessageInfo) Zero() protoreflect.Message {
|
|
return mi.MessageOf(reflect.Zero(mi.GoReflectType).Interface())
|
|
}
|
|
func (mi *MessageInfo) Descriptor() protoreflect.MessageDescriptor { return mi.Desc }
|