mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-29 00:32:43 +00:00
0f81b38d61
This CL moves and renames the protoreflect.ProtoMessage.XXX_Methods to protoreflect.Message.ProtoMethods. Since one needs to obtain a protoreflect.Message now to get at the fast-path methods, we modify the method signatures to take in a protoreflect.Message instead of protoreflect.ProtoMessage. Doing so also avoids the wrapper hack that was formerly done on impl.messageReflectWrapper. After this change the new protoc-gen-go no longer generates any XXX fields or methods. All internal fields and methods are truly hidden from the end-user. name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-4 1.50µs ±10% 1.50µs ± 2% ~ (p=0.483 n=10+9) Wire/Unmarshal/google_message1_proto3-4 1.06µs ± 6% 1.06µs ± 4% ~ (p=0.814 n=9+9) Wire/Unmarshal/google_message2-4 734µs ±22% 689µs ±13% ~ (p=0.133 n=10+9) Wire/Marshal/google_message1_proto2-4 790ns ±46% 652ns ± 8% ~ (p=0.590 n=10+9) Wire/Marshal/google_message1_proto3-4 872ns ± 4% 857ns ± 3% ~ (p=0.168 n=9+9) Wire/Marshal/google_message2-4 232µs ±16% 221µs ± 3% -4.75% (p=0.014 n=9+9) Wire/Size/google_message1_proto2-4 164ns ± 2% 167ns ± 4% +1.87% (p=0.046 n=9+10) Wire/Size/google_message1_proto3-4 240ns ± 9% 229ns ± 1% -4.81% (p=0.000 n=9+8) Wire/Size/google_message2-4 58.9µs ± 9% 59.6µs ± 2% +1.23% (p=0.040 n=9+9) name old alloc/op new alloc/op delta Wire/Unmarshal/google_message1_proto2-4 912B ± 0% 912B ± 0% ~ (all equal) Wire/Unmarshal/google_message1_proto3-4 688B ± 0% 688B ± 0% ~ (all equal) Wire/Unmarshal/google_message2-4 470kB ± 0% 470kB ± 0% ~ (p=0.215 n=10+10) Wire/Marshal/google_message1_proto2-4 240B ± 0% 240B ± 0% ~ (all equal) Wire/Marshal/google_message1_proto3-4 224B ± 0% 224B ± 0% ~ (all equal) Wire/Marshal/google_message2-4 90.1kB ± 0% 90.1kB ± 0% ~ (all equal) Wire/Size/google_message1_proto2-4 0.00B 0.00B ~ (all equal) Wire/Size/google_message1_proto3-4 0.00B 0.00B ~ (all equal) Wire/Size/google_message2-4 0.00B 0.00B ~ (all equal) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-4 24.0 ± 0% 24.0 ± 0% ~ (all equal) Wire/Unmarshal/google_message1_proto3-4 6.00 ± 0% 6.00 ± 0% ~ (all equal) Wire/Unmarshal/google_message2-4 8.49k ± 0% 8.49k ± 0% ~ (all equal) Wire/Marshal/google_message1_proto2-4 1.00 ± 0% 1.00 ± 0% ~ (all equal) Wire/Marshal/google_message1_proto3-4 1.00 ± 0% 1.00 ± 0% ~ (all equal) Wire/Marshal/google_message2-4 1.00 ± 0% 1.00 ± 0% ~ (all equal) Wire/Size/google_message1_proto2-4 0.00 0.00 ~ (all equal) Wire/Size/google_message1_proto3-4 0.00 0.00 ~ (all equal) Wire/Size/google_message2-4 0.00 0.00 ~ (all equal) Change-Id: Ibf3263ad0f293326695c22020a92a6b938ef4f65 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185697 Reviewed-by: Damien Neil <dneil@google.com>
196 lines
5.5 KiB
Go
196 lines
5.5 KiB
Go
// Copyright 2019 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"
|
|
|
|
"google.golang.org/protobuf/internal/pragma"
|
|
pvalue "google.golang.org/protobuf/internal/value"
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
|
)
|
|
|
|
// MessageState is a data structure that is nested as the first field in a
|
|
// concrete message. It provides a way to implement the ProtoReflect method
|
|
// in an allocation-free way without needing to have a shadow Go type generated
|
|
// for every message type. This technique only works using unsafe.
|
|
//
|
|
//
|
|
// Example generated code:
|
|
//
|
|
// type M struct {
|
|
// state protoimpl.MessageState
|
|
//
|
|
// Field1 int32
|
|
// Field2 string
|
|
// Field3 *BarMessage
|
|
// ...
|
|
// }
|
|
//
|
|
// func (m *M) ProtoReflect() protoreflect.Message {
|
|
// mi := &file_fizz_buzz_proto_msgInfos[5]
|
|
// if protoimpl.UnsafeEnabled && m != nil {
|
|
// ms := protoimpl.X.MessageStateOf(Pointer(m))
|
|
// if ms.LoadMessageInfo() == nil {
|
|
// ms.StoreMessageInfo(mi)
|
|
// }
|
|
// return ms
|
|
// }
|
|
// return mi.MessageOf(m)
|
|
// }
|
|
//
|
|
// The MessageState type holds a *MessageInfo, which must be atomically set to
|
|
// the message info associated with a given message instance.
|
|
// By unsafely converting a *M into a *MessageState, the MessageState object
|
|
// has access to all the information needed to implement protobuf reflection.
|
|
// It has access to the message info as its first field, and a pointer to the
|
|
// MessageState is identical to a pointer to the concrete message value.
|
|
//
|
|
//
|
|
// Requirements:
|
|
// • The type M must implement protoreflect.ProtoMessage.
|
|
// • The address of m must not be nil.
|
|
// • The address of m and the address of m.state must be equal,
|
|
// even though they are different Go types.
|
|
type MessageState struct {
|
|
pragma.NoUnkeyedLiterals
|
|
pragma.DoNotCompare
|
|
pragma.DoNotCopy
|
|
|
|
mi *MessageInfo
|
|
}
|
|
|
|
type messageState MessageState
|
|
|
|
var (
|
|
_ pref.Message = (*messageState)(nil)
|
|
_ pvalue.Unwrapper = (*messageState)(nil)
|
|
)
|
|
|
|
// messageDataType is a tuple of a pointer to the message data and
|
|
// a pointer to the message type. It is a generalized way of providing a
|
|
// reflective view over a message instance. The disadvantage of this approach
|
|
// is the need to allocate this tuple of 16B.
|
|
type messageDataType struct {
|
|
p pointer
|
|
mi *MessageInfo
|
|
}
|
|
|
|
type (
|
|
messageReflectWrapper messageDataType
|
|
messageIfaceWrapper messageDataType
|
|
)
|
|
|
|
var (
|
|
_ pref.Message = (*messageReflectWrapper)(nil)
|
|
_ pvalue.Unwrapper = (*messageReflectWrapper)(nil)
|
|
_ pref.ProtoMessage = (*messageIfaceWrapper)(nil)
|
|
_ pvalue.Unwrapper = (*messageIfaceWrapper)(nil)
|
|
)
|
|
|
|
// MessageOf returns a reflective view over a message. The input must be a
|
|
// pointer to a named Go struct. If the provided type has a ProtoReflect method,
|
|
// it must be implemented by calling this method.
|
|
func (mi *MessageInfo) MessageOf(m interface{}) pref.Message {
|
|
// TODO: Switch the input to be an opaque Pointer.
|
|
if reflect.TypeOf(m) != mi.GoType {
|
|
panic(fmt.Sprintf("type mismatch: got %T, want %v", m, mi.GoType))
|
|
}
|
|
p := pointerOfIface(m)
|
|
if p.IsNil() {
|
|
return mi.nilMessage.Init(mi)
|
|
}
|
|
return &messageReflectWrapper{p, mi}
|
|
}
|
|
|
|
func (m *messageReflectWrapper) pointer() pointer { return m.p }
|
|
|
|
func (m *messageIfaceWrapper) ProtoReflect() pref.Message {
|
|
return (*messageReflectWrapper)(m)
|
|
}
|
|
func (m *messageIfaceWrapper) ProtoUnwrap() interface{} {
|
|
return m.p.AsIfaceOf(m.mi.GoType.Elem())
|
|
}
|
|
|
|
type extensionMap map[int32]ExtensionField
|
|
|
|
func (m *extensionMap) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
|
|
if m != nil {
|
|
for _, x := range *m {
|
|
xt := x.GetType()
|
|
if !f(xt, xt.ValueOf(x.GetValue())) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
func (m *extensionMap) Has(xt pref.ExtensionType) (ok bool) {
|
|
if m != nil {
|
|
_, ok = (*m)[int32(xt.Number())]
|
|
}
|
|
return ok
|
|
}
|
|
func (m *extensionMap) Clear(xt pref.ExtensionType) {
|
|
delete(*m, int32(xt.Number()))
|
|
}
|
|
func (m *extensionMap) Get(xt pref.ExtensionType) pref.Value {
|
|
if m != nil {
|
|
if x, ok := (*m)[int32(xt.Number())]; ok {
|
|
return xt.ValueOf(x.GetValue())
|
|
}
|
|
}
|
|
if !isComposite(xt) {
|
|
return defaultValueOf(xt)
|
|
}
|
|
return frozenValueOf(xt.New())
|
|
}
|
|
func (m *extensionMap) Set(xt pref.ExtensionType, v pref.Value) {
|
|
if *m == nil {
|
|
*m = make(map[int32]ExtensionField)
|
|
}
|
|
var x ExtensionField
|
|
x.SetType(xt)
|
|
x.SetEagerValue(xt.InterfaceOf(v))
|
|
(*m)[int32(xt.Number())] = x
|
|
}
|
|
func (m *extensionMap) Mutable(xt pref.ExtensionType) pref.Value {
|
|
if !isComposite(xt) {
|
|
panic("invalid Mutable on field with non-composite type")
|
|
}
|
|
if x, ok := (*m)[int32(xt.Number())]; ok {
|
|
return xt.ValueOf(x.GetValue())
|
|
}
|
|
v := xt.New()
|
|
m.Set(xt, v)
|
|
return v
|
|
}
|
|
|
|
func isComposite(fd pref.FieldDescriptor) bool {
|
|
return fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind || fd.IsList() || fd.IsMap()
|
|
}
|
|
|
|
// checkField verifies that the provided field descriptor is valid.
|
|
// Exactly one of the returned values is populated.
|
|
func (mi *MessageInfo) checkField(fd pref.FieldDescriptor) (*fieldInfo, pref.ExtensionType) {
|
|
if fi := mi.fields[fd.Number()]; fi != nil {
|
|
if fi.fieldDesc != fd {
|
|
panic("mismatching field descriptor")
|
|
}
|
|
return fi, nil
|
|
}
|
|
if fd.IsExtension() {
|
|
if fd.ContainingMessage().FullName() != mi.PBType.FullName() {
|
|
// TODO: Should this be exact containing message descriptor match?
|
|
panic("mismatching containing message")
|
|
}
|
|
if !mi.PBType.ExtensionRanges().Has(fd.Number()) {
|
|
panic("invalid extension field")
|
|
}
|
|
return nil, fd.(pref.ExtensionType)
|
|
}
|
|
panic("invalid field descriptor")
|
|
}
|