perf(decode): cache struct schemas per type

Assisted-by: GLM 5.3
This commit is contained in:
2026-09-17 23:08:14 +02:00
parent a8d69d90d5
commit aaea68efc9
3 changed files with 29 additions and 3 deletions
+8 -2
View File
@@ -101,9 +101,15 @@ sequenceDiagram
setter methods are not, and must finish before the value is shared.
- The parser is allocated per `ParseContext` call; nothing is cached between
documents.
- The one piece of shared state is the struct-schema cache in `decode.go`: a
`sync.Map` keyed by `reflect.Type`, holding the flattened field layout the
decoder and the encoder both consult. A schema is immutable once published,
so concurrent callers only race to build an identical value, the same
trade-off `encoding/json`'s field cache makes. The cache grows with the
number of distinct struct types, never with document size.
- The date-time wrappers are values, not pointers, and are immutable in use.
- Nothing in the library starts goroutines or holds locks; concurrency safety
comes from having no shared mutable state.
- Nothing in the library starts goroutines; apart from the schema cache above,
which never mutates a published entry, there is no shared mutable state.
## Dependencies