runtime/protoiface: use more efficient options representation
Change the representation of option flags in protoiface from bools to a
bitfield. This brings the representation of options in protoiface in
sync with that in internal/impl.
This change has several benefits:
1. We will probably find that we need to add more option flags over time.
Converting to the more efficient representation of these flags as high
in the call stack as possible minimizes the performance implication of
the struct growing.
2. On a similar note, this avoids the need to convert from the compact
representation to the larger one when passing from internal/impl to
proto, since the {Marshal,Unmarshal}State methods take the compact form.
3. This removes unused options from protoiface. Instead of documenting
that AllowPartial is always set, we can just not include an AllowPartial
flag in the protoiface options.
4. Conversely, this provides a way to add option flags to protoiface
that we don't want to expose in the proto package.
name old time/op new time/op delta
EmptyMessage/Wire/Marshal-12 11.1ns ± 7% 10.1ns ± 1% -9.35% (p=0.000 n=8+8)
EmptyMessage/Wire/Unmarshal-12 7.07ns ± 0% 6.74ns ± 1% -4.58% (p=0.000 n=8+8)
EmptyMessage/Wire/Validate-12 4.30ns ± 1% 3.80ns ± 8% -11.45% (p=0.000 n=7+8)
RepeatedInt32/Wire/Marshal-12 1.17µs ± 1% 1.21µs ± 7% +4.09% (p=0.000 n=8+8)
RepeatedInt32/Wire/Unmarshal-12 938ns ± 0% 942ns ± 3% ~ (p=0.178 n=7+8)
RepeatedInt32/Wire/Validate-12 521ns ± 4% 543ns ± 7% ~ (p=0.157 n=7+8)
Required/Wire/Marshal-12 97.2ns ± 1% 95.3ns ± 1% -1.98% (p=0.001 n=7+7)
Required/Wire/Unmarshal-12 41.0ns ± 9% 38.6ns ± 3% -5.73% (p=0.048 n=8+8)
Required/Wire/Validate-12 25.4ns ±11% 21.4ns ± 3% -15.62% (p=0.000 n=8+7)
Change-Id: I3ac1b00ab36cfdf61316ec087a5dd20d9248e4f6
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/216760
Reviewed-by: Joe Tsai <joetsai@google.com>
diff --git a/internal/impl/decode.go b/internal/impl/decode.go
index 74fd821..3cd7f5a 100644
--- a/internal/impl/decode.go
+++ b/internal/impl/decode.go
@@ -16,48 +16,18 @@
piface "google.golang.org/protobuf/runtime/protoiface"
)
-// unmarshalOptions is a more efficient representation of UnmarshalOptions.
-//
-// We don't preserve the AllowPartial flag, because fast-path (un)marshal
-// operations always allow partial messages.
-type unmarshalOptions struct {
- flags unmarshalOptionFlags
-
- // Keep this field's type identical to (proto.UnmarshalOptions).Resolver
- // to avoid a type conversion on assignment.
- resolver interface {
- FindExtensionByName(field pref.FullName) (pref.ExtensionType, error)
- FindExtensionByNumber(message pref.FullName, field pref.FieldNumber) (pref.ExtensionType, error)
- }
-}
-
-type unmarshalOptionFlags uint8
-
-const (
- unmarshalDiscardUnknown unmarshalOptionFlags = 1 << iota
-)
-
-func newUnmarshalOptions(opts piface.UnmarshalOptions) unmarshalOptions {
- o := unmarshalOptions{
- resolver: opts.Resolver,
- }
- if opts.DiscardUnknown {
- o.flags |= unmarshalDiscardUnknown
- }
- return o
-}
+type unmarshalOptions piface.UnmarshalOptions
func (o unmarshalOptions) Options() proto.UnmarshalOptions {
return proto.UnmarshalOptions{
Merge: true,
AllowPartial: true,
DiscardUnknown: o.DiscardUnknown(),
- Resolver: o.Resolver(),
+ Resolver: o.Resolver,
}
}
-func (o unmarshalOptions) DiscardUnknown() bool { return o.flags&unmarshalDiscardUnknown != 0 }
-func (o unmarshalOptions) Resolver() preg.ExtensionTypeResolver { return o.resolver }
+func (o unmarshalOptions) DiscardUnknown() bool { return o.Flags&piface.UnmarshalDiscardUnknown != 0 }
type unmarshalOutput struct {
n int // number of bytes consumed
@@ -72,7 +42,7 @@
} else {
p = m.(*messageReflectWrapper).pointer()
}
- out, err := mi.unmarshalPointer(in.Buf, p, 0, newUnmarshalOptions(opts))
+ out, err := mi.unmarshalPointer(in.Buf, p, 0, unmarshalOptions(opts))
return piface.UnmarshalOutput{
Initialized: out.initialized,
}, err
@@ -202,7 +172,7 @@
xt := x.Type()
if xt == nil {
var err error
- xt, err = opts.Resolver().FindExtensionByNumber(mi.Desc.FullName(), num)
+ xt, err = opts.Resolver.FindExtensionByNumber(mi.Desc.FullName(), num)
if err != nil {
if err == preg.NotFound {
return out, errUnknown