Skip to content

Commit 3cfbf23

Browse files
json: create codecCache type
1 parent 722d2e3 commit 3cfbf23

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

json/codec.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,15 @@ type (
7272
sortFunc func([]reflect.Value)
7373
)
7474

75+
type codecCache map[unsafe.Pointer]codec
76+
7577
// Eventually consistent cache mapping go types to dynamically generated
7678
// codecs.
7779
//
7880
// Note: using a uintptr as key instead of reflect.Type shaved ~15ns off of
7981
// the ~30ns Marhsal/Unmarshal functions which were dominated by the map
8082
// lookup time for simple types like bool, int, etc..
81-
var cache atomic.Pointer[map[unsafe.Pointer]codec]
83+
var cache atomic.Pointer[codecCache]
8284

8385
func cachedCodec(t reflect.Type) codec {
8486
cache := cacheLoad()
@@ -91,7 +93,7 @@ func cachedCodec(t reflect.Type) codec {
9193
return c
9294
}
9395

94-
func cacheLoad() map[unsafe.Pointer]codec {
96+
func cacheLoad() codecCache {
9597
p := cache.Load()
9698
if p == nil {
9799
return nil
@@ -100,8 +102,8 @@ func cacheLoad() map[unsafe.Pointer]codec {
100102
return *p
101103
}
102104

103-
func cacheStore(typ reflect.Type, cod codec, oldCodecs map[unsafe.Pointer]codec) {
104-
newCodecs := make(map[unsafe.Pointer]codec, len(oldCodecs)+1)
105+
func cacheStore(typ reflect.Type, cod codec, oldCodecs codecCache) {
106+
newCodecs := make(codecCache, len(oldCodecs)+1)
105107
maps.Copy(newCodecs, oldCodecs)
106108
newCodecs[typeid(typ)] = cod
107109

0 commit comments

Comments
 (0)