Skip to content

Commit 99e0cea

Browse files
authored
perf(encoding/protobuf): cache reflection results (#138)
1 parent ae88d6e commit 99e0cea

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

encoding/protobuf/protobuf.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,31 @@ package protobuf
22

33
import (
44
"reflect"
5+
"sync"
56

67
"github.com/abemedia/go-don"
78
"github.com/abemedia/go-don/encoding"
89
"google.golang.org/protobuf/proto"
910
)
1011

11-
var messageType = reflect.TypeOf((*proto.Message)(nil)).Elem()
12+
var (
13+
messageType = reflect.TypeOf((*proto.Message)(nil)).Elem()
14+
cache sync.Map
15+
)
1216

1317
func unmarshal(b []byte, v any) error {
14-
// TODO: Cache reflect results to improve performance.
15-
elem := reflect.ValueOf(v).Elem()
16-
if elem.Type().Implements(messageType) {
17-
v = elem.Interface()
18+
typ := reflect.TypeOf(v)
19+
fn, ok := cache.Load(typ)
20+
if !ok {
21+
if typ.Elem().Implements(messageType) {
22+
fn = func(v any) any { return reflect.ValueOf(v).Elem().Interface() }
23+
} else {
24+
fn = func(v any) any { return v }
25+
}
26+
cache.Store(typ, fn)
1827
}
1928

20-
m, ok := v.(proto.Message)
29+
m, ok := fn.(func(v any) any)(v).(proto.Message)
2130
if !ok {
2231
return don.ErrUnsupportedMediaType
2332
}

0 commit comments

Comments
 (0)