protobuf-go/internal/impl/codec_map.go

331 lines
8.6 KiB
Go
Raw Normal View History

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-04-01 13:49:56 -07:00
// 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 (
"errors"
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-04-01 13:49:56 -07:00
"reflect"
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
"sort"
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-04-01 13:49:56 -07:00
"google.golang.org/protobuf/internal/encoding/wire"
pref "google.golang.org/protobuf/reflect/protoreflect"
)
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
type mapInfo struct {
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
goType reflect.Type
keyWiretag uint64
valWiretag uint64
keyFuncs valueCoderFuncs
valFuncs valueCoderFuncs
keyZero pref.Value
keyKind pref.Kind
valMessageInfo *MessageInfo
conv *mapConverter
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
}
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-04-01 13:49:56 -07:00
func encoderFuncsForMap(fd pref.FieldDescriptor, ft reflect.Type) (funcs pointerCoderFuncs) {
// TODO: Consider generating specialized map coders.
keyField := fd.MapKey()
valField := fd.MapValue()
keyWiretag := wire.EncodeTag(1, wireTypes[keyField.Kind()])
valWiretag := wire.EncodeTag(2, wireTypes[valField.Kind()])
keyFuncs := encoderFuncsForValue(keyField)
valFuncs := encoderFuncsForValue(valField)
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
conv := newMapConverter(ft, fd)
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-04-01 13:49:56 -07:00
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
mapi := &mapInfo{
goType: ft,
keyWiretag: keyWiretag,
valWiretag: valWiretag,
keyFuncs: keyFuncs,
valFuncs: valFuncs,
keyZero: keyField.Default(),
keyKind: keyField.Kind(),
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
conv: conv,
}
if valField.Kind() == pref.MessageKind {
mapi.valMessageInfo = getMessageInfo(ft.Elem())
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
}
funcs = pointerCoderFuncs{
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-04-01 13:49:56 -07:00
size: func(p pointer, tagsize int, opts marshalOptions) int {
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
return sizeMap(p.AsValueOf(ft).Elem(), tagsize, mapi, opts)
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-04-01 13:49:56 -07:00
},
marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
return appendMap(b, p.AsValueOf(ft).Elem(), wiretag, mapi, opts)
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-04-01 13:49:56 -07:00
},
unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (unmarshalOutput, error) {
mp := p.AsValueOf(ft)
if mp.Elem().IsNil() {
mp.Elem().Set(reflect.MakeMap(mapi.goType))
}
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
if mapi.valMessageInfo == nil {
return consumeMap(b, mp.Elem(), wtyp, mapi, opts)
} else {
return consumeMapOfMessage(b, mp.Elem(), wtyp, mapi, opts)
}
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
},
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-04-01 13:49:56 -07:00
}
if valFuncs.isInit != nil {
funcs.isInit = func(p pointer) error {
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
return isInitMap(p.AsValueOf(ft).Elem(), mapi)
}
}
return funcs
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-04-01 13:49:56 -07:00
}
const (
mapKeyTagSize = 1 // field 1, tag size 1.
mapValTagSize = 1 // field 2, tag size 2.
)
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
func sizeMap(mapv reflect.Value, tagsize int, mapi *mapInfo, opts marshalOptions) int {
if mapv.Len() == 0 {
return 0
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
}
n := 0
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
iter := mapRange(mapv)
for iter.Next() {
key := mapi.conv.keyConv.PBValueOf(iter.Key()).MapKey()
keySize := mapi.keyFuncs.size(key.Value(), mapKeyTagSize, opts)
var valSize int
value := mapi.conv.valConv.PBValueOf(iter.Value())
if mapi.valMessageInfo == nil {
valSize = mapi.valFuncs.size(value, mapValTagSize, opts)
} else {
p := pointerOfValue(iter.Value())
valSize += mapValTagSize
valSize += wire.SizeBytes(mapi.valMessageInfo.sizePointer(p, opts))
}
n += tagsize + wire.SizeBytes(keySize+valSize)
}
return n
}
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
func consumeMap(b []byte, mapv reflect.Value, wtyp wire.Type, mapi *mapInfo, opts unmarshalOptions) (out unmarshalOutput, err error) {
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
if wtyp != wire.BytesType {
return out, errUnknown
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
}
b, n := wire.ConsumeBytes(b)
if n < 0 {
return out, wire.ParseError(n)
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
}
var (
key = mapi.keyZero
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
val = mapi.conv.valConv.New()
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
)
for len(b) > 0 {
num, wtyp, n := wire.ConsumeTag(b)
if n < 0 {
return out, wire.ParseError(n)
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
}
if num > wire.MaxValidNumber {
return out, errors.New("invalid field number")
}
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
b = b[n:]
err := errUnknown
switch num {
case 1:
var v pref.Value
var o unmarshalOutput
v, o, err = mapi.keyFuncs.unmarshal(b, key, num, wtyp, opts)
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
if err != nil {
break
}
key = v
n = o.n
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
case 2:
var v pref.Value
var o unmarshalOutput
v, o, err = mapi.valFuncs.unmarshal(b, val, num, wtyp, opts)
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
if err != nil {
break
}
val = v
n = o.n
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
}
if err == errUnknown {
n = wire.ConsumeFieldValue(num, wtyp, b)
if n < 0 {
return out, wire.ParseError(n)
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
}
} else if err != nil {
return out, err
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
}
b = b[n:]
}
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
mapv.SetMapIndex(mapi.conv.keyConv.GoValueOf(key), mapi.conv.valConv.GoValueOf(val))
out.n = n
return out, nil
internal/impl: add fast-path unmarshal Benchmarks run with: go test ./benchmarks/ -bench=Wire -benchtime=500ms -benchmem -count=8 Fast-path vs. parent commit: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 1.35µs ± 2% 0.45µs ± 4% -67.01% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 1.07µs ± 1% 0.31µs ± 1% -71.04% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 691µs ± 2% 188µs ± 2% -72.78% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 60.0 ± 0% 25.0 ± 0% -58.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 42.0 ± 0% 7.0 ± 0% -83.33% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 28.6k ± 0% 8.5k ± 0% -70.34% (p=0.000 n=8+8) Fast-path vs. -v1: name old time/op new time/op delta Wire/Unmarshal/google_message1_proto2-12 702ns ± 1% 445ns ± 4% -36.58% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 604ns ± 1% 311ns ± 1% -48.54% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 179µs ± 3% 188µs ± 2% +5.30% (p=0.000 n=7+8) name old allocs/op new allocs/op delta Wire/Unmarshal/google_message1_proto2-12 26.0 ± 0% 25.0 ± 0% -3.85% (p=0.000 n=8+8) Wire/Unmarshal/google_message1_proto3-12 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=8+8) Wire/Unmarshal/google_message2-12 8.49k ± 0% 8.49k ± 0% -0.01% (p=0.000 n=8+8) Change-Id: I6247ac3fd66a63d9acb902cbd192094ee3d151c3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185147 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-27 10:54:42 -07:00
}
func consumeMapOfMessage(b []byte, mapv reflect.Value, wtyp wire.Type, mapi *mapInfo, opts unmarshalOptions) (out unmarshalOutput, err error) {
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
if wtyp != wire.BytesType {
return out, errUnknown
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-04-01 13:49:56 -07:00
}
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
b, n := wire.ConsumeBytes(b)
if n < 0 {
return out, wire.ParseError(n)
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
}
var (
key = mapi.keyZero
val = reflect.New(mapi.valMessageInfo.GoReflectType.Elem())
)
for len(b) > 0 {
num, wtyp, n := wire.ConsumeTag(b)
if n < 0 {
return out, wire.ParseError(n)
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
}
if num > wire.MaxValidNumber {
return out, errors.New("invalid field number")
}
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
b = b[n:]
err := errUnknown
switch num {
case 1:
var v pref.Value
var o unmarshalOutput
v, o, err = mapi.keyFuncs.unmarshal(b, key, num, wtyp, opts)
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
if err != nil {
break
}
key = v
n = o.n
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
case 2:
if wtyp != wire.BytesType {
break
}
var v []byte
v, n = wire.ConsumeBytes(b)
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
if n < 0 {
return out, wire.ParseError(n)
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
}
var o unmarshalOutput
o, err = mapi.valMessageInfo.unmarshalPointer(v, pointerOfValue(val), 0, opts)
if o.initialized {
// Consider this map item initialized so long as we see
// an initialized value.
out.initialized = true
}
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
}
if err == errUnknown {
n = wire.ConsumeFieldValue(num, wtyp, b)
if n < 0 {
return out, wire.ParseError(n)
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
}
} else if err != nil {
return out, err
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
}
b = b[n:]
}
mapv.SetMapIndex(mapi.conv.keyConv.GoValueOf(key), val)
out.n = n
return out, nil
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
}
func appendMapItem(b []byte, keyrv, valrv reflect.Value, mapi *mapInfo, opts marshalOptions) ([]byte, error) {
if mapi.valMessageInfo == nil {
key := mapi.conv.keyConv.PBValueOf(keyrv).MapKey()
val := mapi.conv.valConv.PBValueOf(valrv)
size := 0
size += mapi.keyFuncs.size(key.Value(), mapKeyTagSize, opts)
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
size += mapi.valFuncs.size(val, mapValTagSize, opts)
b = wire.AppendVarint(b, uint64(size))
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
b, err := mapi.keyFuncs.marshal(b, key.Value(), mapi.keyWiretag, opts)
if err != nil {
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
return nil, err
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-04-01 13:49:56 -07:00
}
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
return mapi.valFuncs.marshal(b, val, mapi.valWiretag, opts)
} else {
key := mapi.conv.keyConv.PBValueOf(keyrv).MapKey()
val := pointerOfValue(valrv)
valSize := mapi.valMessageInfo.sizePointer(val, opts)
size := 0
size += mapi.keyFuncs.size(key.Value(), mapKeyTagSize, opts)
size += mapValTagSize + wire.SizeBytes(valSize)
b = wire.AppendVarint(b, uint64(size))
b, err := mapi.keyFuncs.marshal(b, key.Value(), mapi.keyWiretag, opts)
if err != nil {
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
return nil, err
}
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
b = wire.AppendVarint(b, mapi.valWiretag)
b = wire.AppendVarint(b, uint64(valSize))
return mapi.valMessageInfo.marshalAppendPointer(b, val, opts)
}
}
func appendMap(b []byte, mapv reflect.Value, wiretag uint64, mapi *mapInfo, opts marshalOptions) ([]byte, error) {
if mapv.Len() == 0 {
return b, nil
}
if opts.Deterministic() {
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
return appendMapDeterministic(b, mapv, wiretag, mapi, opts)
}
iter := mapRange(mapv)
for iter.Next() {
var err error
b = wire.AppendVarint(b, wiretag)
b, err = appendMapItem(b, iter.Key(), iter.Value(), mapi, opts)
if err != nil {
return b, err
}
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-04-01 13:49:56 -07:00
}
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
return b, nil
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-04-01 13:49:56 -07:00
}
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
func appendMapDeterministic(b []byte, mapv reflect.Value, wiretag uint64, mapi *mapInfo, opts marshalOptions) ([]byte, error) {
keys := mapv.MapKeys()
sort.Slice(keys, func(i, j int) bool {
switch keys[i].Kind() {
case reflect.Bool:
return !keys[i].Bool() && keys[j].Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return keys[i].Int() < keys[j].Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return keys[i].Uint() < keys[j].Uint()
case reflect.Float32, reflect.Float64:
return keys[i].Float() < keys[j].Float()
case reflect.String:
return keys[i].String() < keys[j].String()
default:
panic("invalid kind: " + keys[i].Kind().String())
}
})
internal/impl: faster map fast path Avoid using protobuf reflection on map values in the fast path. Range operations in particular are expensive in protoreflect, because the closure passed to Map.Range escapes. Iterate maps using a reflect.MapIter when available. When operating on maps of messages where we have a *MessageInfo for the message type, directly jump to the fast-path *MessageInfo methods rather than passing through the proto package. Benchmarks deltas for a google.protobuf.Struct with JSON represention: {"parameters":{"a":{"b":{"c":{"d":{"e":{"f":{"g":{}}}}}}}}} Compared to previous revision: name old time/op new time/op delta NestedStruct/Size 7.22µs ± 2% 4.84µs ± 2% -32.96% (p=0.000 n=8+8) NestedStruct/Size-8 9.30µs ± 2% 5.89µs ± 2% -36.60% (p=0.000 n=8+8) NestedStruct/Marshal 77.6µs ±12% 9.8µs ± 4% -87.33% (p=0.000 n=8+8) NestedStruct/Marshal-8 91.6µs ± 2% 11.9µs ± 2% -86.99% (p=0.000 n=8+8) NestedStruct/Unmarshal 11.5µs ± 4% 8.7µs ± 2% -24.76% (p=0.000 n=8+8) NestedStruct/Unmarshal-8 15.4µs ± 4% 11.9µs ± 2% -22.41% (p=0.000 n=8+8) Compared to github.com/golang/protobuf: name old time/op new time/op delta NestedStruct/Size 5.42µs ± 1% 4.84µs ± 2% -10.61% (p=0.000 n=8+8) NestedStruct/Size-8 6.34µs ± 2% 5.89µs ± 2% -7.10% (p=0.000 n=8+8) NestedStruct/Marshal 12.5µs ± 2% 9.8µs ± 4% -21.41% (p=0.000 n=7+8) NestedStruct/Marshal-8 14.1µs ± 3% 11.9µs ± 2% -15.52% (p=0.000 n=8+8) NestedStruct/Unmarshal 9.66µs ± 1% 8.65µs ± 2% -10.40% (p=0.000 n=7+8) NestedStruct/Unmarshal-8 11.7µs ± 3% 11.9µs ± 2% +1.95% (p=0.038 n=8+8) Change-Id: I0effe6491f30d41f31904777f74eca3ac3694db3 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211737 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-12-17 11:39:17 -08:00
for _, key := range keys {
var err error
b = wire.AppendVarint(b, wiretag)
b, err = appendMapItem(b, key, mapv.MapIndex(key), mapi, opts)
if err != nil {
return b, err
}
}
return b, nil
}
func isInitMap(mapv reflect.Value, mapi *mapInfo) error {
if mi := mapi.valMessageInfo; mi != nil {
mi.init()
if !mi.needsInitCheck {
return nil
}
iter := mapRange(mapv)
for iter.Next() {
val := pointerOfValue(iter.Value())
if err := mi.isInitializedPointer(val); err != nil {
return err
}
}
} else {
iter := mapRange(mapv)
for iter.Next() {
val := mapi.conv.valConv.PBValueOf(iter.Value())
if err := mapi.valFuncs.isInit(val); err != nil {
return err
}
}
}
return nil
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-04-01 13:49:56 -07:00
}