Add a benchmark demonstrating lock contention when Marshaling/Unmarshaling maps. This benchmark is derived from a Google service which experienced poor performance when handling protos with maps. The current implementation of map decoding uses reflection. In particular reflect.New, reflect.NewAt, and reflect.(*Value).Addr all call reflect.(*rvalue).ptrTo. reflect.(*rvalue).ptrTo uses a cache protected by a mutex. Grabbing this lock is what causes the problem. reflect.(*rvalue).ptrTo also implements a fast path (which avoids critical sections) for certain types known to the compiler. Hopefully we can extend the compiler to generate descriptors for more types (https://golang.org/issue/17931) so that we can hit the fast path for all types needed for proto decoding. PiperOrigin-RevId: 139337589
diff --git a/proto/map_test.go b/proto/map_test.go new file mode 100644 index 0000000..313e879 --- /dev/null +++ b/proto/map_test.go
@@ -0,0 +1,46 @@ +package proto_test + +import ( + "fmt" + "testing" + + "github.com/golang/protobuf/proto" + ppb "github.com/golang/protobuf/proto/proto3_proto" +) + +func marshalled() []byte { + m := &ppb.IntMaps{} + for i := 0; i < 1000; i++ { + m.Maps = append(m.Maps, &ppb.IntMap{ + Rtt: map[int32]int32{1: 2}, + }) + } + b, err := proto.Marshal(m) + if err != nil { + panic(fmt.Sprintf("Can't marshal %+v: %v", m, err)) + } + return b +} + +func BenchmarkConcurrentMapUnmarshal(b *testing.B) { + in := marshalled() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + var out ppb.IntMaps + if err := proto.Unmarshal(in, &out); err != nil { + b.Errorf("Can't unmarshal ppb.IntMaps: %v", err) + } + } + }) +} + +func BenchmarkSequentialMapUnmarshal(b *testing.B) { + in := marshalled() + b.ResetTimer() + for i := 0; i < b.N; i++ { + var out ppb.IntMaps + if err := proto.Unmarshal(in, &out); err != nil { + b.Errorf("Can't unmarshal ppb.IntMaps: %v", err) + } + } +}
diff --git a/proto/proto3_proto/proto3.proto b/proto/proto3_proto/proto3.proto index 75d5a02..2048655 100644 --- a/proto/proto3_proto/proto3.proto +++ b/proto/proto3_proto/proto3.proto
@@ -76,3 +76,12 @@ message MessageWithMap { map<bool, bytes> byte_mapping = 1; } + + +message IntMap { + map<int32, int32> rtt = 1; +} + +message IntMaps { + repeated IntMap maps = 1; +}