summaryrefslogtreecommitdiff
path: root/struct.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-30 23:00:11 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-30 23:00:11 -0700
commit5d5ac549dc1a25963418fda4b20ed181b7afd8d2 (patch)
tree8c5c2e9f5bd1c3a2208efe4d11965ecaa95472ec /struct.go
parent005dfe26e308b965c2f42c81d34a4b48757414a3 (diff)
parentccf8dc4b21bb1a547f118affab22bca3a02df270 (diff)
Merge branch 'lukeshu/tune'
Diffstat (limited to 'struct.go')
-rw-r--r--struct.go14
1 files changed, 13 insertions, 1 deletions
diff --git a/struct.go b/struct.go
index b7fc287..8a664c6 100644
--- a/struct.go
+++ b/struct.go
@@ -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)