diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-01-26 13:59:35 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-01-30 22:00:25 -0700 |
commit | 8aa12d3cb043859229810947da6c52e600d34b55 (patch) | |
tree | 4b7b3a8e9adafbbb06531ef3c06fb200d42abd32 /struct.go | |
parent | 005dfe26e308b965c2f42c81d34a4b48757414a3 (diff) |
struct.go: Cache structIndexes
This should help save some CPU time and avoid some memory churn.
Diffstat (limited to 'struct.go')
-rw-r--r-- | struct.go | 14 |
1 files changed, 13 insertions, 1 deletions
@@ -7,6 +7,8 @@ package lowmemjson import ( "reflect" + "git.lukeshu.com/go/typedsync" + "git.lukeshu.com/go/lowmemjson/internal" ) @@ -25,9 +27,19 @@ type structIndex struct { byName map[string]int } +var structIndexCache typedsync.CacheMap[reflect.Type, structIndex] + // indexStruct takes a struct Type, and indexes its fields for use by -// Decoder.Decode() and Encoder.Encode(). +// Decoder.Decode() and Encoder.Encode(). indexStruct caches its +// results. func indexStruct(typ reflect.Type) structIndex { + ret, _ := structIndexCache.LoadOrCompute(typ, indexStructReal) + return ret +} + +// indexStructReal is like indexStruct, but is the real indexer, +// bypassing the cache. +func indexStructReal(typ reflect.Type) structIndex { var byPos []structField byName := make(map[string][]int) |