File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,22 +2,31 @@ package protobuf
22
33import (
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
1317func 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 }
You can’t perform that action at this time.
0 commit comments