File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -5,36 +5,18 @@ import (
55 "sync"
66)
77
8- var cachedValues struct {
9- m map [reflect.Type ]chan reflect.Value
10- sync.RWMutex
11- }
8+ var cachedPools sync.Map // map[reflect.Type]*sync.Pool
129
1310func cachedValue (t reflect.Type ) reflect.Value {
14- cachedValues .RLock ()
15- ch := cachedValues .m [t ]
16- cachedValues .RUnlock ()
17- if ch != nil {
18- return <- ch
19- }
20-
21- cachedValues .Lock ()
22- defer cachedValues .Unlock ()
23- if ch = cachedValues .m [t ]; ch != nil {
24- return <- ch
25- }
26-
27- ch = make (chan reflect.Value , 256 )
28- go func () {
29- for {
30- ch <- reflect .New (t )
31- }
32- }()
33- if cachedValues .m == nil {
34- cachedValues .m = make (map [reflect.Type ]chan reflect.Value , 8 )
11+ v , ok := cachedPools .Load (t )
12+ if ! ok {
13+ v , _ = cachedPools .LoadOrStore (t , & sync.Pool {
14+ New : func () interface {} {
15+ return reflect .New (t )
16+ },
17+ })
3518 }
36- cachedValues .m [t ] = ch
37- return <- ch
19+ return v .(* sync.Pool ).Get ().(reflect.Value )
3820}
3921
4022func (d * Decoder ) newValue (t reflect.Type ) reflect.Value {
You can’t perform that action at this time.
0 commit comments