Commit Graph

520 Commits

Author SHA1 Message Date
Damien Neil
c37adefdac internal/impl: add fast-path marshal implementation
This is a port of the v1 table marshaler, with some substantial
cleanup and refactoring.

Benchstat results from the protobuf reference benchmark data comparing the
v1 package with v2, with AllowPartial:true set for the new package. This
is not an apples-to-apples comparison, since v1 doesn't have a way to
disable required field checks.  Required field checks in v2 package
currently go through reflection, which performs terribly; my initial
experimentation indicates that fast-path required field checks will
not add a large amount of cost; these results are incomplete but not
wholly inaccurate.

name                                           old time/op  new time/op  delta
/dataset.google_message3_1.pb/Marshal-12        219ms ± 1%   232ms ± 1%   +5.85%  (p=0.004 n=6+5)
/dataset.google_message2.pb/Marshal-12          261µs ± 3%   248µs ± 1%   -5.14%  (p=0.002 n=6+6)
/dataset.google_message1_proto2.pb/Marshal-12   681ns ± 2%   637ns ± 3%   -6.53%  (p=0.002 n=6+6)
/dataset.google_message1_proto3.pb/Marshal-12  1.10µs ± 8%  0.99µs ± 3%   -9.63%  (p=0.002 n=6+6)
/dataset.google_message3_3.pb/Marshal-12       44.2ms ± 3%  35.2ms ± 1%  -20.28%  (p=0.004 n=6+5)
/dataset.google_message4.pb/Marshal-12         91.4ms ± 2%  94.9ms ± 2%   +3.78%  (p=0.002 n=6+6)
/dataset.google_message3_2.pb/Marshal-12       78.7ms ± 6%  80.8ms ± 4%     ~     (p=0.310 n=6+6)
/dataset.google_message3_4.pb/Marshal-12       10.6ms ± 3%  10.6ms ± 8%     ~     (p=0.662 n=5+6)
/dataset.google_message3_5.pb/Marshal-12        675ms ± 4%   510ms ± 2%  -24.40%  (p=0.002 n=6+6)
/dataset.google_message3_1.pb/Marshal           219ms ± 1%   236ms ± 7%   +8.06%  (p=0.004 n=5+6)
/dataset.google_message2.pb/Marshal             257µs ± 1%   250µs ± 3%     ~     (p=0.052 n=5+6)
/dataset.google_message1_proto2.pb/Marshal      685ns ± 1%   628ns ± 1%   -8.41%  (p=0.008 n=5+5)
/dataset.google_message1_proto3.pb/Marshal     1.08µs ± 1%  0.98µs ± 2%   -9.31%  (p=0.004 n=5+6)
/dataset.google_message3_3.pb/Marshal          43.7ms ± 1%  35.1ms ± 1%  -19.76%  (p=0.002 n=6+6)
/dataset.google_message4.pb/Marshal            93.4ms ± 4%  94.9ms ± 2%     ~     (p=0.180 n=6+6)
/dataset.google_message3_2.pb/Marshal           105ms ± 2%    98ms ± 7%   -6.81%  (p=0.009 n=5+6)
/dataset.google_message3_4.pb/Marshal          16.3ms ± 6%  15.7ms ± 3%   -3.44%  (p=0.041 n=6+6)
/dataset.google_message3_5.pb/Marshal           676ms ± 4%   504ms ± 2%  -25.50%  (p=0.004 n=6+5)

Change-Id: I72cc4597117f4cf5d236ef505777d49dd4a5f75d
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/171020
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-05-16 22:13:43 +00:00
Joe Tsai
5c62f675c5 types: remove stub packages
CL/177623 updates v1 to not depend on the previous import paths.

Change-Id: I46a61b8f8fa136c9c1fe367ff311732ba8841279
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177604
Reviewed-by: Damien Neil <dneil@google.com>
2019-05-16 22:06:16 +00:00
Joe Tsai
a95b29fbf6 types: consistently name generated protos
Rename each generated protobuf package such that the base of the
Go package path is always equal to the Go package name to follow
proper Go package naming conventions.

The Go package name is derived from the .proto source file name by
replacing ".proto" with "pb" and stripping all underscores.

Change-Id: Iea05d1b5d94b1b2821ae10276ab771bb2df93c0e
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177380
Reviewed-by: Damien Neil <dneil@google.com>
2019-05-16 21:55:40 +00:00
Joe Tsai
11caeff126 internal/impl: unexport ExtensionFieldV1.Value
CL/177620 modifies v1 to stop touching the Value field directly,
such that it is now unexport the Value field.

Change-Id: I0a05bbe59146862fc77c261349d7d90d6fa312e0
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177621
Reviewed-by: Damien Neil <dneil@google.com>
2019-05-16 20:29:06 +00:00
Joe Tsai
40b83d67fc internal/impl: add scaffolding for lazy extension support
Once the ExtensionFieldV1.Value field is unexported, it forces all
read operations to go through GetValue, which can perform some form
of lazy retrieval.

The SetEagerValue method is equivalent to setting the Value directly.
The SetLazyValue method permits setting a lazy equivalent of the Value
by simply storing a function closure returning the value.

Change-Id: I410f74924f0957f5eadc82eea3669ca335a02701
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177619
Reviewed-by: Damien Neil <dneil@google.com>
2019-05-16 20:17:45 +00:00
Joe Tsai
838f1f50f9 encoding: remove jsonpb and textpb
Change-Id: Id057d697efecedb00bc07705a47cddb2d69866e9
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177603
Reviewed-by: Damien Neil <dneil@google.com>
2019-05-16 19:31:22 +00:00
John Wright
0cf31136c7 internal/prototype: support dynamic enum and message types in extension
GoExtension now supports extensions that have enum or message type that
is implemented by a Go type that can take on multiple enum or message
types (i.e. the actual enum or message type cannot be determined simply
from the zero value of its Go type). This is necessary to support
dynamic types generated at runtime from descriptors rather than at
compile-time.

Change-Id: Ia0b3b4b02332fc83c0c85e992b37ded467070472
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177338
Reviewed-by: Joe Tsai <joetsai@google.com>
2019-05-16 00:50:46 +00:00
Joe Tsai
cdb7773569 encoding: switch ordering of Unmarshal arguments
While it is general convention that the receiver being mutated
is the first argument, the standard library specifically goes against
this convention when it comes to the Unmarshal function.

Switch the ordering of the Unmarshal function to match the Go stdlib.

Change-Id: I893346680233ef9fec7104415a54a0a7ae353378
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177258
Reviewed-by: Damien Neil <dneil@google.com>
2019-05-14 23:18:07 +00:00
Damien Neil
5c5b531562 protogen, encoding/jsonpb, encoding/textpb: rename packages
Rename encoding/*pb to follow the convention of prefixing package names
with 'proto':

	google.golang.org/protobuf/encoding/protojson
	google.golang.org/protobuf/encoding/prototext

Move protogen under a compiler/ directory, just in case we ever do add
more compiler-related packages.

	google.golang.org/protobuf/compiler/protogen

Change-Id: I31010cb5cabcea8274fffcac468477b58b56e8eb
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177178
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-05-14 20:33:22 +00:00
Damien Neil
934520f5b2 go.mod, go.sum: add back in after v2 module renaming
Change-Id: Iba5d25391870b4a6014fd90fc79ab101c75ea3c7
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177042
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-05-14 18:44:45 +00:00
Damien Neil
e89e6244e0 all: change module to google.golang.org/protobuf
Temporarily remove go.mod, since we can't generate an accurate one until
the corresponding v1 change is submitted.

Change-Id: I1e1ad97f2b455e33f61ffaeb8676289795e47e72
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177000
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-05-14 17:28:29 +00:00
Joe Tsai
ac31a352ca reflect/protoreflect: add helper methods to FieldDescriptor
Added API:
	FieldDescriptor.IsExtension
	FieldDescriptor.IsList
	FieldDescriptor.MapKey
	FieldDescriptor.MapValue
	FieldDescriptor.ContainingOneof
	FieldDescriptor.ContainingMessage

Deprecated API (to be removed in subsequent CL):
	FieldDescriptor.Oneof
	FieldDescriptor.Extendee

These methods help cleanup several common usage patterns.

Change-Id: I9a3ffabc2edb2173c536509b22f330f98bba7cf3
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/176977
Reviewed-by: Damien Neil <dneil@google.com>
2019-05-14 17:14:05 +00:00
Joe Tsai
67c1d9b2c1 reflect/protoreflect: add Descriptor.ParentFile
Querying for the parent file that contains a descriptor declaration
is a common enough operation to warrant its own first-class method.

Change-Id: I2f41e5126a5b465df23897904a6513dd3ed8dd92
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/176777
Reviewed-by: Damien Neil <dneil@google.com>
2019-05-13 20:21:18 +00:00
Joe Tsai
0fc49f8225 reflect/protoreflect: add Descriptor specific methods
Added methods:
	Enum.Descriptor
	Message.Descriptor
	EnumType.Descriptor
	MessageType.Descriptor
	ExtensionType.Descriptor
	Message.New

All functionality is switched over to use those methods instead of
implicitly relying on the fact that {Enum,Message}Type implicitly
implement the associated descriptor interface.

This CL does not yet remove {Enum,Message}.Type or prevent
{Enum,Message,Extension}Type from implementating a descriptor.
That is a subsequent CL.

The Message.New method is also added to replace functionality
that will be lost when the Type methods are removed.

Change-Id: I7fefde1673bbd40bfdac489aca05cec9a6c98eb1
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/174918
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Herbie Ong <herbie@google.com>
2019-05-13 19:34:41 +00:00
Joe Tsai
4a58eb3f7c internal/impl: remove ExtensionFieldV1.Raw
CL/175579 switches v2 to use XXX_unrecognized exclusively.
CL/175838 switches v1 to use XXX_unrecognized exclusively.

This means that it is now safe to delete the Raw field.

Change-Id: Ic60c58147c2df4078e72c34d1202d226a7ea4bed
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/175839
Reviewed-by: Damien Neil <dneil@google.com>
2019-05-09 21:52:05 +00:00
Damien Neil
927aaba87c internal/impl: fix panic message in pointer.AsValueOf
Correctly report the type we were looking for when panicking.
Previously would say: "invalid type: got *T, want *T".

Change-Id: I90ea0dd1fc64f3aec37a5a5828c1832fb0ab2887
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/176258
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-05-09 19:29:16 +00:00
Damien Neil
3eaddf0344 internal/impl: pull out message struct info into a type
Minor refactoring of impl.MessageType initialization: Pull the
information gathered about a message struct by makeKnownFieldsFunc out
into a struct that we can pass around.

At the moment, makeKnownFieldsFunc is the only user of this struct, but
this will simplify the table (un)marshaler.

Drop the 'specialByName' map indexing XXX_ fields because it currently
isn't used anywhere.

Change-Id: I992c9f490982a05f3919d7d4e08052e2ab54d44d
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/176220
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-05-09 19:16:10 +00:00
Joe Tsai
00a323deed internal/impl: use XXX_unrecognized exclusively for unknown fields
The protobuf data model makes no distinction between unknown fields
that are within the extension field ranges or not. Now that we eagerly
unmarshal extensions, there is even less need for storing unknown
fields in the extension map. Instead, use the XXX_unrecognized field
exclusively for this purpose.

Change-Id: I673a7d6259fe9fdbdc295bcfa8252ef4da415343
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/175579
Reviewed-by: Damien Neil <dneil@google.com>
2019-05-09 01:26:50 +00:00
Joe Tsai
db38ddde7d proto: eagerly unmarshal extensions
CL/172399 switches the v1 code to eagerly unmarshal extensions.
This CL does the equivalent for v2.

For the test, we simply switch from protoV1.Equal to protoV2.Equal,
since the v2 equal does not magically unmarshal raw extensions.

Change-Id: I6f64455b0a75bbc9a9a82108558641a29bd2b982
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/175838
Reviewed-by: Damien Neil <dneil@google.com>
2019-05-09 01:19:44 +00:00
Damien Neil
82a0306187 internal/{fileinit,impl}: minimal weak field support
Weak fields are an obsolete proto1 feature. They have been superseded
by extensions. However, some vestigial support for weak fields does
remain, mostly as Google-internal patches. (They aren't exciting;
extensions really do everything weak fields do in a cleaner and
more portable fashion.)

At the moment, the only visible impact of marking a field [weak=true]
is to exclude it from "internal/fileinit".FileBuilder.DependencyIndexes.
We want to preserve that behavior just in case we ever do add full weak
field support here.

Extend fileinit to look up message descriptors for weak fields in the
global registry. If the descriptor cannot be found, use a placeholder
instead.

Remove special-case handling of weak fields in the impl package. The
code generator doesn't do anything special for them, so they can be
treated as any other field.

Change-Id: Ifa2ee3d30d63680a0eeb59c66ebc9521f38fd660
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/175997
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-05-08 17:24:54 +00:00
John Wright
bac4cd44d4 reflect/protodesc: check file imports by name
Weak imports are added to Imports as placeholders even if they can be
found in the Files registry, so we have to look at the name rather than
the actual FileDescriptor.

Change-Id: I28f62e945f233119014e5e8cb1bcbde7dca831a7
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/175897
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-05-08 01:49:39 +00:00
John Wright
9a824c9a9e reflect/protodesc: add validation for NewFile
This covers most of the TODO around validation. I left open the ones
that we didn't have clear consensus on yet.

Change-Id: I336c53173ee8d7447558b1e3a0c1ef945e986cd5
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/175140
Reviewed-by: Joe Tsai <joetsai@google.com>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-05-08 00:41:07 +00:00
Joe Tsai
972d873b59 internal/encoding/wire: increase maximum field number
The protobuf documentation explicitly specifies (1<<29)-1 as the maximum
field number, but the C++ implementation itself has a special-case where it
allows field numbers up to MaxInt32 for MessageSet fields, but continues
to apply the former limit in all non-MessageSet cases.

To avoid complicated branching logic, we use the larger limit for all cases
if MessageSet is supported, otherwise, we impose the documented limit.

Change-Id: I710a2a21aa3beba161c3e6ca2f2ed9a266920a5a
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/175817
Reviewed-by: Damien Neil <dneil@google.com>
2019-05-07 21:39:02 +00:00
Herbie Ong
9c1004563f encoding: add TODOs for producing better error messages with handling of Any
Change-Id: Iaab531d8ae454715338bf079a2749afeedc978f9
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/175577
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-05-07 04:06:17 +00:00
Damien Neil
e5e4f36f7d internal/testprotos/conformance: make package name match dir
Change the rewritten Go package name for the conformance protos to match
the basename of the directory (i.e., "conformance").

This is a trivial change, but avoids confusion when the package name and
import path don't match.

Change-Id: I9b091c78ce4a85f7051c55ee1a48ef6dbba68db6
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/174944
Reviewed-by: Herbie Ong <herbie@google.com>
2019-05-02 18:46:11 +00:00
Damien Neil
5ec58b9121 testing/prototest: add package testing message implementations
The prototest package takes a message and exercises its implementation
of the protoreflect.Message interface in a variety of ways.

Change-Id: I4c150d1f1a6c41048f7e7f0d77efa4440ae8db0d
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/174577
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-05-01 21:11:22 +00:00
Damien Neil
e6f060fdac proto: add Equal
Add support for basic equality comparison of messages.

Messages are equal if they have the same type and marshal to the
same bytes with deterministic serialization, with some exceptions:

 - Messages with different registered extensions are unequal.
 - NaN is not equal to itself.

Unlike the v1 Equal, a nil message is equal to an empty message of
the same type.

Change-Id: Ibabdadd8c767b801051b8241aeae1ba077e58121
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/174277
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-04-29 23:34:17 +00:00
Herbie Ong
decef41dcc internal/encoding/json: improve decoding speed and memory allocation
Change use of regexp for matching literals true,false,null to simple
bytes comparison. Small gain from doing this.

Remove computing for position in Value as that is only needed in error
messages. In order to preserve ability to compute for position later,
store the original input in Value instead of just the slice containing
the value, however, need to also store the start index and size of the
parsed value.

Using benchmark in encoding/bench_test.go now shows faster time and less
memory usage than V1.

name          old time/op    new time/op    delta
JSONEncode-4    30.3ms ± 1%    10.3ms ± 1%  -66.02%  (p=0.000 n=9+8)
JSONDecode-4    54.4ms ± 3%    18.9ms ± 2%  -65.33%  (p=0.000 n=10+10)

name          old alloc/op   new alloc/op   delta
JSONEncode-4    10.3MB ± 0%     3.9MB ± 0%  -61.74%  (p=0.000 n=10+9)
JSONDecode-4    19.0MB ± 0%     3.6MB ± 0%  -81.29%  (p=0.000 n=10+9)

name          old allocs/op  new allocs/op  delta
JSONEncode-4      465k ± 0%       64k ± 0%  -86.30%  (p=0.000 n=10+8)
JSONDecode-4      289k ± 0%      163k ± 0%  -43.69%  (p=0.000 n=10+9)

Change-Id: I0a3108d675d6442674facb065aaebd14051f6c5d
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/172662
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-04-29 22:13:25 +00:00
Herbie Ong
4f0be71f91 encoding/jsonpb: add unmarshal option to ignore unknown fields
This feature seems to be used quite a bit, and the conformance tests
treat this as required, perhaps as a "required option" since the
developer guide states:

"Proto3 JSON parser should reject unknown fields by default but may
provide an option to ignore unknown fields in parsing."

Also, all invalid UTF-8 errors in skipped values are also returned as it
is similar to a parse error, except it is a non-fatal one.

Change-Id: Ia26e9a355daecdbf99af23f3061353fffa32d47d
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/174017
Reviewed-by: Damien Neil <dneil@google.com>
2019-04-29 22:11:02 +00:00
Herbie Ong
d64dcebbc7 integration_test: add conformance tests
Change-Id: I3c21e5069c34a6417c9177ae5f1bdcb801418c8a
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/173665
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-04-29 22:08:16 +00:00
Herbie Ong
06a6b0bda8 encoding/jsonpb: fix parsing of number strings containing leading/trailing spaces
Per conformance tests, number strings that contain leading or trailing
spaces are not allowed.

3a3956e8a2/conformance/binary_json_conformance_suite.cc (L1196-L1203)

Change-Id: Ifbe0e4ee125fac3c291b456eece541353025cdd4
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/173664
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-04-29 20:44:00 +00:00
Herbie Ong
ad9c125737 encoding/jsonpb: fix encoding of max/min Duration and max Timestamp
Bad off-by-one error:(

Change-Id: I47dfaa8529fcdb2d53a4fc67c1f046d2152fe8fe
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/173837
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-04-29 20:43:26 +00:00
Joe Tsai
060cdacd3b internal/impl: fix Has behavior for floats
According to IEEE-754, equality on floats reports true for both +0 and -0.
However, this definition causes us to lose information. Instead, we want
a definition that checks for exact equality with +0.
Otherwise, we would lose the ability to serialize -0.

Change-Id: I36450c24258fc4f856bfd4bc4c53a90199b216b9
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/172970
Reviewed-by: Herbie Ong <herbie@google.com>
2019-04-22 20:01:26 +00:00
Joe Tsai
c90b6ada23 reflect/protoreflect: remove methods with Type suffix
CL/172238 added equivalent methods without the Type suffix,
while keeping the old methods.
CL/172582 updates the v1 codebase to use the new methods.
This CL removes the methods with the Type suffix.

Change-Id: Iaaaa4fff11cac1694735657db2e5fd7cadc90afe
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/173138
Reviewed-by: Herbie Ong <herbie@google.com>
2019-04-20 08:14:13 +00:00
Joe Tsai
d24bc72368 reflect/protoreflect: rename methods with Type suffix
The protobuf type system uses the word "descriptor" instead of "type".
We should avoid the "type" verbage when we aren't talking about Go types.
The old names are temporarily kept around for compatibility reasons.

Change-Id: Icc99c913528ead011f7a74aa8399d9c5ec6dc56e
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/172238
Reviewed-by: Herbie Ong <herbie@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
2019-04-20 06:35:24 +00:00
Herbie Ong
1e09691415 internal/encoding/{json,text}: improve string parsing
Previous calls to indexNeedEscape with a type conversion from []byte
to string incurs allocation.

Make 2 different calls instead, one for string and one for bytes.

Type converting string to []byte does not incur extra allocation,
however, the benchmark results still show it to be slower by ~3% for
textpb and 6+% for jsonpb, hence decided to go with 2 separate calls
instead.

Results over current head:
name          old time/op    new time/op    delta
TextEncode-4    18.1ms ± 2%    18.3ms ± 2%     ~     (p=0.065 n=10+9)
TextDecode-4     233ms ± 3%     102ms ± 1%  -56.34%  (p=0.000 n=9+10)
JSONEncode-4    10.4ms ± 2%    10.5ms ± 0%   +0.56%  (p=0.019 n=9+9)
JSONDecode-4     870ms ± 2%     354ms ± 4%  -59.33%  (p=0.000 n=9+10)

name          old alloc/op   new alloc/op   delta
TextEncode-4    28.9MB ± 0%    28.9MB ± 0%   +0.00%  (p=0.000 n=10+9)
TextDecode-4    1.16GB ± 0%    0.03GB ± 0%  -97.44%  (p=0.000 n=9+10)
JSONEncode-4    3.94MB ± 0%    3.94MB ± 0%   +0.00%  (p=0.000 n=10+10)
JSONDecode-4    3.35GB ± 0%    0.01GB ± 0%  -99.83%  (p=0.000 n=10+10)

name          old allocs/op  new allocs/op  delta
TextEncode-4     73.5k ± 0%     73.5k ± 0%     ~     (all equal)
TextDecode-4      278k ± 0%      255k ± 0%   -8.26%  (p=0.000 n=9+10)
JSONEncode-4     63.8k ± 0%     63.8k ± 0%     ~     (all equal)
JSONDecode-4      247k ± 0%      210k ± 0%  -14.92%  (p=0.000 n=10+10)

Change-Id: Ibc64e9a7827ec1fffa213eb79f60497950203700
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/172239
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-04-17 00:26:13 +00:00
Joe Tsai
00e50dc9c1 runtime/protoimpl: remove ExtensionFieldsV1
This is no longer directly used by v1.

Change-Id: I14a283dd2d1572d50ebd82fdf55f7d86bfacc272
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/172438
Reviewed-by: Damien Neil <dneil@google.com>
2019-04-16 23:53:07 +00:00
Joe Tsai
ab61d41ec9 cmd/protoc-gen-go: add constants to control generation of deprecated features
These deprecated features add a non-trivial amount of binary bloat.
Protect these under a constant to make it easy to patch them out.

Change-Id: I12cc33613c19ef60ceec4c4449d0cf3692835170
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/172407
Reviewed-by: Damien Neil <dneil@google.com>
2019-04-16 22:29:53 +00:00
Joe Tsai
5e71dc95f2 cmd/protoc-gen-go: used named types for special fields
Using a named fields gives us the flexibility to change the underlying
representation of special fields without needing to regenerate user code.

We add a named type for ExtensionFields, UnknownFields, and SizeCache.

Change-Id: I107cf82899850ea76665310ce79def60f0f7ab97
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/172402
Reviewed-by: Damien Neil <dneil@google.com>
2019-04-16 22:04:16 +00:00
Joe Tsai
50c16719d7 internal/impl: simplify extension data structures
The v2 implementation will eagerly unmarshal extension fields,
which means that the logic to synchronize the extensions fields
is no longer necessary. Using type aliases, we can ensure
impl.ExtensionsV1 is identical to the extensions map prior to
a4ab9ec5 from May 2016. This makes it such that we only have one
legacy implementation that we have to deal with.

Change-Id: I41b977e912fbb899c5bc4f055890d474b419429c
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/172240
Reviewed-by: Damien Neil <dneil@google.com>
2019-04-16 19:21:22 +00:00
Joe Tsai
e089c0f929 internal/fileinit: add FileDescriptor.ProtoLegacyRawDesc method
Add a ProtoLegacyRawDesc method for v1 registration support.

Change-Id: I4f03d022854c8d79da776610cb7d75ba9976334f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/172241
Reviewed-by: Damien Neil <dneil@google.com>
2019-04-16 18:51:45 +00:00
Joe Tsai
7ca7098113 cmd/protoc-gen-go: drop xxx_ prefix for package-level variables
Lower-casing the FileDescriptor variable provides for shorter and
more readable global variable names. This has a very minor benefit
that the read-only data section of a binary is slightly smaller
since function names do end up in the final binary.

Change-Id: I077364905e5c9adea69873b3ea580fddc68b9eb8
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/172119
Reviewed-by: Herbie Ong <herbie@google.com>
2019-04-16 16:31:54 +00:00
Damien Neil
4419fabcb0 integration_test: drop "go build ./..."
This should be redundant with "go test ./...", and on pre-Go1.11 versions it
fails on directories that contain no non-test files.

Change-Id: I2c16cf3b26e67f3baa7732f0f7744aa547972e1a
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/172237
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-04-16 06:05:21 +00:00
Damien Neil
2300c18725 reflect/protoreflect, reflect/protoregistry: move descriptor lookup to registry
Drop the protoreflect.FileDescriptor.DescriptorByName method.
Descriptor lookup will always happen through a protoregistry.Files, which
is more generally useful (it's rare that you want to find a descriptor in a
specific file, as opposed to a package which may be composed of multiple files).

Split protoregistry.Files descriptor lookup into individual per-type functions
(enum, message, extension, service), matching the preg.Types API.

Drop the ability to look up enum values, message fields, and service methods
for now. This can be easily added later if needed, and is trivial to implement
in user code. (e.g., look up the service and then consult sd.Methods.ByName().)

Change-Id: I2b3d8ef888921a8464ba1434eddab20c7d3a458e
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/172118
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-04-16 06:03:04 +00:00
Herbie Ong
8eba9eee0b encoding: add simple benchmarks to compare V1 vs V2
name          old time/op    new time/op     delta
TextEncode-4    7.70ms ± 2%    18.57ms ± 3%    +141.06%  (p=0.000 n=10+9)
TextDecode-4    9.62ms ± 6%   252.85ms ±10%   +2529.10%  (p=0.000 n=10+10)
JSONEncode-4    30.3ms ± 0%     10.7ms ± 7%     -64.80%  (p=0.000 n=9+9)
JSONDecode-4    54.1ms ± 0%    880.1ms ± 3%   +1526.65%  (p=0.000 n=9+10)

name          old alloc/op   new alloc/op    delta
TextEncode-4    3.98MB ± 0%    28.90MB ± 0%    +626.36%  (p=0.000 n=10+10)
TextDecode-4    2.74MB ± 0%  1158.24MB ± 0%  +42114.16%  (p=0.000 n=10+8)
JSONEncode-4    10.3MB ± 0%      3.9MB ± 0%     -61.74%  (p=0.000 n=10+9)
JSONDecode-4    19.0MB ± 0%   3349.9MB ± 0%  +17534.57%  (p=0.000 n=10+10)

name          old allocs/op  new allocs/op   delta
TextEncode-4     43.9k ± 0%      73.5k ± 0%     +67.53%  (p=0.000 n=10+10)
TextDecode-4     56.2k ± 0%     277.1k ± 0%    +393.31%  (p=0.000 n=8+8)
JSONEncode-4      465k ± 0%        64k ± 0%     -86.30%  (p=0.000 n=10+10)
JSONDecode-4      289k ± 0%       247k ± 0%     -14.58%  (p=0.000 n=10+10)

Change-Id: I593a52445b6356eec1488236d5f1f1a8c4b62cb3
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/172137
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-04-16 00:06:16 +00:00
Joe Tsai
a2dd228004 internal/fileinit: make fileinit responsible for registering concrete options types
Changes:
* Remove protoreflect.OptionsMessage and use protoreflect.ProtoMessage
now that the generated options natively implement reflection.
* Make registration of concrete option types the responsibility of
internal/fileinit since the init logic of descriptor.pb.go uses that.
* Remove equivalent logic in protoc-gen-go to special-case descriptor.

Change-Id: Id814651fafa4d888ff4532d59c6a4f9b68145157
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/171465
Reviewed-by: Damien Neil <dneil@google.com>
2019-04-15 20:37:10 +00:00
Joe Tsai
5b9fa1f023 all: remove direct dependence on internal/legacy
Change-Id: Ic547a5edab4a1e7799c421b59ea78eca98534e96
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/171699
Reviewed-by: Damien Neil <dneil@google.com>
2019-04-15 20:23:52 +00:00
Damien Neil
bc310b58c6 proto: validate UTF-8 in proto3 strings
Change-Id: I6a495730c3f438e7b2c4ca86edade7d6f25aa47d
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/171700
Reviewed-by: Herbie Ong <herbie@google.com>
2019-04-12 17:43:10 +00:00
Joe Tsai
a5f43e834e internal/fileinit: add scary warnings about compatibility
Change-Id: I4c2f06ed5b06bf481b585ed58ed1c54647843c79
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/171466
Reviewed-by: Herbie Ong <herbie@google.com>
2019-04-11 00:38:23 +00:00
Herbie Ong
21a3974ed6 encoding/textpb: add string fields UTF-8 validation
Change-Id: I15aec2b90efae9366eb496dc221b9e8cacd9d8e6
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/171122
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-04-10 23:05:52 +00:00