Skip to content

perf: cache and optimize struct tag processing - #195

Open
tcard wants to merge 1 commit into
go-viper:mainfrom
tcard:cache-tags
Open

perf: cache and optimize struct tag processing#195
tcard wants to merge 1 commit into
go-viper:mainfrom
tcard:cache-tags

Conversation

@tcard

@tcard tcard commented Jul 23, 2026

Copy link
Copy Markdown

Currently, mapstructure is often slower than just JSON-marshaling and unmarshaling again.

Adding some caches improves performance significantly.

goos: darwin
goarch: arm64
pkg: github.com/go-viper/mapstructure/v2
cpu: Apple M4
                              │ /tmp/msbench/baseline.txt │        /tmp/msbench/final.txt        │
                              │          sec/op           │    sec/op     vs base                │
_Decode-10                                   1.862µ ±  0%   1.637µ ±  5%  -12.08% (p=0.001 n=10)
_DecodeBasic-10                              7.804µ ±  4%   6.071µ ±  1%  -22.21% (p=0.000 n=10)
_DecodeEmbedded-10                           6.131µ ±  6%   3.721µ ±  2%  -39.31% (p=0.000 n=10)
_DecodeMapOfStruct-10                       11.847µ ±  1%   7.250µ ± 15%  -38.80% (p=0.002 n=10)
_DecodeSliceOfStruct-10                     11.593µ ±  9%   6.742µ ± 20%  -41.85% (p=0.000 n=10)
_DecodeMetadata-10                           1.569µ ± 12%   1.228µ ±  2%  -21.73% (p=0.000 n=10)
_DecodeMetadataEmbedded-10                   7.599µ ± 10%   5.769µ ±  3%  -24.09% (p=0.000 n=10)
_DecodeTagged-10                             817.2n ±  5%   582.4n ±  5%  -28.74% (p=0.000 n=10)
_DecodeWithRemainingFields-10                1.957µ ±  3%   1.841µ ±  1%   -5.95% (p=0.000 n=10)
geomean                                      3.949µ         2.882µ        -27.01%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant