diff --git a/internal/filedesc/desc_lazy.go b/internal/filedesc/desc_lazy.go index 9fc5b1bb..4d311096 100644 --- a/internal/filedesc/desc_lazy.go +++ b/internal/filedesc/desc_lazy.go @@ -6,6 +6,7 @@ package filedesc import ( "reflect" + "sync" "google.golang.org/protobuf/internal/descopts" "google.golang.org/protobuf/internal/encoding/wire" @@ -675,14 +676,18 @@ func (db *DescBuilder) optionsUnmarshaler(p pref.ProtoMessage, b []byte) func() if b == nil { return nil } + var opts pref.ProtoMessage + var once sync.Once return func() pref.ProtoMessage { - p := reflect.New(reflect.TypeOf(p).Elem()).Interface().(pref.ProtoMessage) - if err := (proto.UnmarshalOptions{ - AllowPartial: true, - Resolver: db.TypeResolver, - }).Unmarshal(b, p); err != nil { - panic(err) - } - return p + once.Do(func() { + opts = reflect.New(reflect.TypeOf(p).Elem()).Interface().(pref.ProtoMessage) + if err := (proto.UnmarshalOptions{ + AllowPartial: true, + Resolver: db.TypeResolver, + }).Unmarshal(b, opts); err != nil { + panic(err) + } + }) + return opts } } diff --git a/reflect/protoreflect/type.go b/reflect/protoreflect/type.go index a733be11..d252cafd 100644 --- a/reflect/protoreflect/type.go +++ b/reflect/protoreflect/type.go @@ -85,8 +85,6 @@ type Descriptor interface { // For FileDescriptor, the Path and Package are also valid. IsPlaceholder() bool - // TODO: Decide memory ownership of Options. - // Options returns the descriptor options. The caller must not modify // the returned value. //