Skip to content

Commit cb9b777

Browse files
committed
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
1 parent 4a5b3fd commit cb9b777

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

proto/map_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package proto_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/golang/protobuf/proto"
8+
ppb "github.com/golang/protobuf/proto/proto3_proto"
9+
)
10+
11+
func marshalled() []byte {
12+
m := &ppb.IntMaps{}
13+
for i := 0; i < 1000; i++ {
14+
m.Maps = append(m.Maps, &ppb.IntMap{
15+
Rtt: map[int32]int32{1: 2},
16+
})
17+
}
18+
b, err := proto.Marshal(m)
19+
if err != nil {
20+
panic(fmt.Sprintf("Can't marshal %+v: %v", m, err))
21+
}
22+
return b
23+
}
24+
25+
func BenchmarkConcurrentMapUnmarshal(b *testing.B) {
26+
in := marshalled()
27+
b.RunParallel(func(pb *testing.PB) {
28+
for pb.Next() {
29+
var out ppb.IntMaps
30+
if err := proto.Unmarshal(in, &out); err != nil {
31+
b.Errorf("Can't unmarshal ppb.IntMaps: %v", err)
32+
}
33+
}
34+
})
35+
}
36+
37+
func BenchmarkSequentialMapUnmarshal(b *testing.B) {
38+
in := marshalled()
39+
b.ResetTimer()
40+
for i := 0; i < b.N; i++ {
41+
var out ppb.IntMaps
42+
if err := proto.Unmarshal(in, &out); err != nil {
43+
b.Errorf("Can't unmarshal ppb.IntMaps: %v", err)
44+
}
45+
}
46+
}

proto/proto3_proto/proto3.proto

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,12 @@ message Nested {
7676
message MessageWithMap {
7777
map<bool, bytes> byte_mapping = 1;
7878
}
79+
80+
81+
message IntMap {
82+
map<int32, int32> rtt = 1;
83+
}
84+
85+
message IntMaps {
86+
repeated IntMap maps = 1;
87+
}

0 commit comments

Comments
 (0)