proto, internal/impl: don't create fast path Size for legacy Marshalers
Implementations of the legacy Marshaler type have no way to efficiently
compute the size of the message. Rather than generating an inefficient
fast-path Size method which marshals the message and examines the
length of the result, don't generate a fast-path at all.
Drop the requirement that a fast-path MarshalAppend requires a
corresponding Size.
Avoids O(N^2) behavior when marshaling a legacy Marshaler that
recursively calls proto.Marshal.
Change-Id: I4793cf32275d08f29c8e1a1a44a193d9a5724058
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/213443
Reviewed-by: Joe Tsai <joetsai@google.com>
diff --git a/proto/size.go b/proto/size.go
index 9947580..beaa925 100644
--- a/proto/size.go
+++ b/proto/size.go
@@ -22,9 +22,16 @@
}
func sizeMessage(m protoreflect.Message) (size int) {
- if methods := protoMethods(m); methods != nil && methods.Size != nil {
+ methods := protoMethods(m)
+ if methods != nil && methods.Size != nil {
return methods.Size(m, protoiface.MarshalOptions{})
}
+ if methods != nil && methods.MarshalAppend != nil {
+ // This is not efficient, but we don't have any choice.
+ // This case is mainly used for legacy types with a Marshal method.
+ b, _ := methods.MarshalAppend(nil, m, protoiface.MarshalOptions{})
+ return len(b)
+ }
return sizeMessageSlow(m)
}