protobuf-go/internal/impl/codec_tables.go

585 lines
15 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 (
"fmt"
"reflect"
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
"google.golang.org/protobuf/internal/encoding/wire"
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
pref "google.golang.org/protobuf/reflect/protoreflect"
)
// pointerCoderFuncs is a set of pointer encoding functions.
type pointerCoderFuncs struct {
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
size func(p pointer, tagsize int, opts marshalOptions) int
marshal func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error)
unmarshal func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error)
isInit func(p pointer) error
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
}
// ifaceCoderFuncs is a set of interface{} encoding functions.
type ifaceCoderFuncs struct {
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
size func(ival interface{}, tagsize int, opts marshalOptions) int
marshal func(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error)
unmarshal func(b []byte, ival interface{}, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (interface{}, int, error)
isInit func(ival interface{}) error
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
}
// fieldCoder returns pointer functions for a field, used for operating on
// struct fields.
func fieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
switch {
case fd.IsMap():
return encoderFuncsForMap(fd, ft)
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
case fd.Cardinality() == pref.Repeated && !fd.IsPacked():
// Repeated fields (not packed).
if ft.Kind() != reflect.Slice {
break
}
ft := ft.Elem()
switch fd.Kind() {
case pref.BoolKind:
if ft.Kind() == reflect.Bool {
return coderBoolSlice
}
case pref.EnumKind:
if ft.Kind() == reflect.Int32 {
return coderEnumSlice
}
case pref.Int32Kind:
if ft.Kind() == reflect.Int32 {
return coderInt32Slice
}
case pref.Sint32Kind:
if ft.Kind() == reflect.Int32 {
return coderSint32Slice
}
case pref.Uint32Kind:
if ft.Kind() == reflect.Uint32 {
return coderUint32Slice
}
case pref.Int64Kind:
if ft.Kind() == reflect.Int64 {
return coderInt64Slice
}
case pref.Sint64Kind:
if ft.Kind() == reflect.Int64 {
return coderSint64Slice
}
case pref.Uint64Kind:
if ft.Kind() == reflect.Uint64 {
return coderUint64Slice
}
case pref.Sfixed32Kind:
if ft.Kind() == reflect.Int32 {
return coderSfixed32Slice
}
case pref.Fixed32Kind:
if ft.Kind() == reflect.Uint32 {
return coderFixed32Slice
}
case pref.FloatKind:
if ft.Kind() == reflect.Float32 {
return coderFloatSlice
}
case pref.Sfixed64Kind:
if ft.Kind() == reflect.Int64 {
return coderSfixed64Slice
}
case pref.Fixed64Kind:
if ft.Kind() == reflect.Uint64 {
return coderFixed64Slice
}
case pref.DoubleKind:
if ft.Kind() == reflect.Float64 {
return coderDoubleSlice
}
case pref.StringKind:
if ft.Kind() == reflect.String && fd.Syntax() == pref.Proto3 {
return coderStringSliceValidateUTF8
}
if ft.Kind() == reflect.String {
return coderStringSlice
}
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
return coderBytesSlice
}
case pref.BytesKind:
if ft.Kind() == reflect.String {
return coderStringSlice
}
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
return coderBytesSlice
}
case pref.MessageKind:
return makeMessageSliceFieldCoder(fd, ft)
case pref.GroupKind:
return makeGroupSliceFieldCoder(fd, ft)
}
case fd.Cardinality() == pref.Repeated && fd.IsPacked():
// Packed repeated fields.
//
// Only repeated fields of primitive numeric types
// (Varint, Fixed32, or Fixed64 wire type) can be packed.
if ft.Kind() != reflect.Slice {
break
}
ft := ft.Elem()
switch fd.Kind() {
case pref.BoolKind:
if ft.Kind() == reflect.Bool {
return coderBoolPackedSlice
}
case pref.EnumKind:
if ft.Kind() == reflect.Int32 {
return coderEnumPackedSlice
}
case pref.Int32Kind:
if ft.Kind() == reflect.Int32 {
return coderInt32PackedSlice
}
case pref.Sint32Kind:
if ft.Kind() == reflect.Int32 {
return coderSint32PackedSlice
}
case pref.Uint32Kind:
if ft.Kind() == reflect.Uint32 {
return coderUint32PackedSlice
}
case pref.Int64Kind:
if ft.Kind() == reflect.Int64 {
return coderInt64PackedSlice
}
case pref.Sint64Kind:
if ft.Kind() == reflect.Int64 {
return coderSint64PackedSlice
}
case pref.Uint64Kind:
if ft.Kind() == reflect.Uint64 {
return coderUint64PackedSlice
}
case pref.Sfixed32Kind:
if ft.Kind() == reflect.Int32 {
return coderSfixed32PackedSlice
}
case pref.Fixed32Kind:
if ft.Kind() == reflect.Uint32 {
return coderFixed32PackedSlice
}
case pref.FloatKind:
if ft.Kind() == reflect.Float32 {
return coderFloatPackedSlice
}
case pref.Sfixed64Kind:
if ft.Kind() == reflect.Int64 {
return coderSfixed64PackedSlice
}
case pref.Fixed64Kind:
if ft.Kind() == reflect.Uint64 {
return coderFixed64PackedSlice
}
case pref.DoubleKind:
if ft.Kind() == reflect.Float64 {
return coderDoublePackedSlice
}
}
case fd.Kind() == pref.MessageKind:
return makeMessageFieldCoder(fd, ft)
case fd.Kind() == pref.GroupKind:
return makeGroupFieldCoder(fd, ft)
case fd.Syntax() == pref.Proto3 && fd.ContainingOneof() == nil:
// Populated oneof fields always encode even if set to the zero value,
// which normally are not encoded in proto3.
switch fd.Kind() {
case pref.BoolKind:
if ft.Kind() == reflect.Bool {
return coderBoolNoZero
}
case pref.EnumKind:
if ft.Kind() == reflect.Int32 {
return coderEnumNoZero
}
case pref.Int32Kind:
if ft.Kind() == reflect.Int32 {
return coderInt32NoZero
}
case pref.Sint32Kind:
if ft.Kind() == reflect.Int32 {
return coderSint32NoZero
}
case pref.Uint32Kind:
if ft.Kind() == reflect.Uint32 {
return coderUint32NoZero
}
case pref.Int64Kind:
if ft.Kind() == reflect.Int64 {
return coderInt64NoZero
}
case pref.Sint64Kind:
if ft.Kind() == reflect.Int64 {
return coderSint64NoZero
}
case pref.Uint64Kind:
if ft.Kind() == reflect.Uint64 {
return coderUint64NoZero
}
case pref.Sfixed32Kind:
if ft.Kind() == reflect.Int32 {
return coderSfixed32NoZero
}
case pref.Fixed32Kind:
if ft.Kind() == reflect.Uint32 {
return coderFixed32NoZero
}
case pref.FloatKind:
if ft.Kind() == reflect.Float32 {
return coderFloatNoZero
}
case pref.Sfixed64Kind:
if ft.Kind() == reflect.Int64 {
return coderSfixed64NoZero
}
case pref.Fixed64Kind:
if ft.Kind() == reflect.Uint64 {
return coderFixed64NoZero
}
case pref.DoubleKind:
if ft.Kind() == reflect.Float64 {
return coderDoubleNoZero
}
case pref.StringKind:
if ft.Kind() == reflect.String {
return coderStringNoZeroValidateUTF8
}
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
return coderBytesNoZero
}
case pref.BytesKind:
if ft.Kind() == reflect.String {
return coderStringNoZero
}
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
return coderBytesNoZero
}
}
case ft.Kind() == reflect.Ptr:
ft := ft.Elem()
switch fd.Kind() {
case pref.BoolKind:
if ft.Kind() == reflect.Bool {
return coderBoolPtr
}
case pref.EnumKind:
if ft.Kind() == reflect.Int32 {
return coderEnumPtr
}
case pref.Int32Kind:
if ft.Kind() == reflect.Int32 {
return coderInt32Ptr
}
case pref.Sint32Kind:
if ft.Kind() == reflect.Int32 {
return coderSint32Ptr
}
case pref.Uint32Kind:
if ft.Kind() == reflect.Uint32 {
return coderUint32Ptr
}
case pref.Int64Kind:
if ft.Kind() == reflect.Int64 {
return coderInt64Ptr
}
case pref.Sint64Kind:
if ft.Kind() == reflect.Int64 {
return coderSint64Ptr
}
case pref.Uint64Kind:
if ft.Kind() == reflect.Uint64 {
return coderUint64Ptr
}
case pref.Sfixed32Kind:
if ft.Kind() == reflect.Int32 {
return coderSfixed32Ptr
}
case pref.Fixed32Kind:
if ft.Kind() == reflect.Uint32 {
return coderFixed32Ptr
}
case pref.FloatKind:
if ft.Kind() == reflect.Float32 {
return coderFloatPtr
}
case pref.Sfixed64Kind:
if ft.Kind() == reflect.Int64 {
return coderSfixed64Ptr
}
case pref.Fixed64Kind:
if ft.Kind() == reflect.Uint64 {
return coderFixed64Ptr
}
case pref.DoubleKind:
if ft.Kind() == reflect.Float64 {
return coderDoublePtr
}
case pref.StringKind:
if ft.Kind() == reflect.String {
return coderStringPtr
}
case pref.BytesKind:
if ft.Kind() == reflect.String {
return coderStringPtr
}
}
default:
switch fd.Kind() {
case pref.BoolKind:
if ft.Kind() == reflect.Bool {
return coderBool
}
case pref.EnumKind:
if ft.Kind() == reflect.Int32 {
return coderEnum
}
case pref.Int32Kind:
if ft.Kind() == reflect.Int32 {
return coderInt32
}
case pref.Sint32Kind:
if ft.Kind() == reflect.Int32 {
return coderSint32
}
case pref.Uint32Kind:
if ft.Kind() == reflect.Uint32 {
return coderUint32
}
case pref.Int64Kind:
if ft.Kind() == reflect.Int64 {
return coderInt64
}
case pref.Sint64Kind:
if ft.Kind() == reflect.Int64 {
return coderSint64
}
case pref.Uint64Kind:
if ft.Kind() == reflect.Uint64 {
return coderUint64
}
case pref.Sfixed32Kind:
if ft.Kind() == reflect.Int32 {
return coderSfixed32
}
case pref.Fixed32Kind:
if ft.Kind() == reflect.Uint32 {
return coderFixed32
}
case pref.FloatKind:
if ft.Kind() == reflect.Float32 {
return coderFloat
}
case pref.Sfixed64Kind:
if ft.Kind() == reflect.Int64 {
return coderSfixed64
}
case pref.Fixed64Kind:
if ft.Kind() == reflect.Uint64 {
return coderFixed64
}
case pref.DoubleKind:
if ft.Kind() == reflect.Float64 {
return coderDouble
}
case pref.StringKind:
if fd.Syntax() == pref.Proto3 && ft.Kind() == reflect.String {
return coderStringValidateUTF8
}
if ft.Kind() == reflect.String {
return coderString
}
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
return coderBytes
}
case pref.BytesKind:
if ft.Kind() == reflect.String {
return coderString
}
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
return coderBytes
}
}
}
panic(fmt.Errorf("invalid type: no encoder for %v %v %v/%v", fd.FullName(), fd.Cardinality(), fd.Kind(), ft))
}
// encoderFuncsForValue returns interface{} value functions for a field, used for
// extension values and map encoding.
func encoderFuncsForValue(fd pref.FieldDescriptor, ft reflect.Type) ifaceCoderFuncs {
switch {
case fd.Cardinality() == pref.Repeated && !fd.IsPacked():
if ft.Kind() != reflect.Ptr || ft.Elem().Kind() != reflect.Slice {
break
}
ft := ft.Elem().Elem()
switch fd.Kind() {
case pref.BoolKind:
if ft.Kind() == reflect.Bool {
return coderBoolSliceIface
}
case pref.EnumKind:
if ft.Kind() == reflect.Int32 {
return coderEnumSliceIface
}
case pref.Int32Kind:
if ft.Kind() == reflect.Int32 {
return coderInt32SliceIface
}
case pref.Sint32Kind:
if ft.Kind() == reflect.Int32 {
return coderSint32SliceIface
}
case pref.Uint32Kind:
if ft.Kind() == reflect.Uint32 {
return coderUint32SliceIface
}
case pref.Int64Kind:
if ft.Kind() == reflect.Int64 {
return coderInt64SliceIface
}
case pref.Sint64Kind:
if ft.Kind() == reflect.Int64 {
return coderSint64SliceIface
}
case pref.Uint64Kind:
if ft.Kind() == reflect.Uint64 {
return coderUint64SliceIface
}
case pref.Sfixed32Kind:
if ft.Kind() == reflect.Int32 {
return coderSfixed32SliceIface
}
case pref.Fixed32Kind:
if ft.Kind() == reflect.Uint32 {
return coderFixed32SliceIface
}
case pref.FloatKind:
if ft.Kind() == reflect.Float32 {
return coderFloatSliceIface
}
case pref.Sfixed64Kind:
if ft.Kind() == reflect.Int64 {
return coderSfixed64SliceIface
}
case pref.Fixed64Kind:
if ft.Kind() == reflect.Uint64 {
return coderFixed64SliceIface
}
case pref.DoubleKind:
if ft.Kind() == reflect.Float64 {
return coderDoubleSliceIface
}
case pref.StringKind:
if ft.Kind() == reflect.String {
return coderStringSliceIface
}
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
return coderBytesSliceIface
}
case pref.BytesKind:
if ft.Kind() == reflect.String {
return coderStringSliceIface
}
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
return coderBytesSliceIface
}
case pref.MessageKind:
return coderMessageSliceIface
case pref.GroupKind:
return coderGroupSliceIface
}
case fd.Cardinality() == pref.Repeated && fd.IsPacked():
default:
switch fd.Kind() {
case pref.BoolKind:
if ft.Kind() == reflect.Bool {
return coderBoolIface
}
case pref.EnumKind:
if ft.Kind() == reflect.Int32 {
return coderEnumIface
}
case pref.Int32Kind:
if ft.Kind() == reflect.Int32 {
return coderInt32Iface
}
case pref.Sint32Kind:
if ft.Kind() == reflect.Int32 {
return coderSint32Iface
}
case pref.Uint32Kind:
if ft.Kind() == reflect.Uint32 {
return coderUint32Iface
}
case pref.Int64Kind:
if ft.Kind() == reflect.Int64 {
return coderInt64Iface
}
case pref.Sint64Kind:
if ft.Kind() == reflect.Int64 {
return coderSint64Iface
}
case pref.Uint64Kind:
if ft.Kind() == reflect.Uint64 {
return coderUint64Iface
}
case pref.Sfixed32Kind:
if ft.Kind() == reflect.Int32 {
return coderSfixed32Iface
}
case pref.Fixed32Kind:
if ft.Kind() == reflect.Uint32 {
return coderFixed32Iface
}
case pref.FloatKind:
if ft.Kind() == reflect.Float32 {
return coderFloatIface
}
case pref.Sfixed64Kind:
if ft.Kind() == reflect.Int64 {
return coderSfixed64Iface
}
case pref.Fixed64Kind:
if ft.Kind() == reflect.Uint64 {
return coderFixed64Iface
}
case pref.DoubleKind:
if ft.Kind() == reflect.Float64 {
return coderDoubleIface
}
case pref.StringKind:
if fd.Syntax() == pref.Proto3 && ft.Kind() == reflect.String {
return coderStringIfaceValidateUTF8
}
if ft.Kind() == reflect.String {
return coderStringIface
}
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
return coderBytesIface
}
case pref.BytesKind:
if ft.Kind() == reflect.String {
return coderStringIface
}
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
return coderBytesIface
}
case pref.MessageKind:
return coderMessageIface
case pref.GroupKind:
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
return makeGroupValueCoder(fd, ft)
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
}
}
panic(fmt.Errorf("invalid type: no encoder for %v %v %v/%v", fd.FullName(), fd.Cardinality(), fd.Kind(), ft))
}