pelletier / go-toml

Go library for the TOML file format
https://github.com/pelletier/go-toml
Other
1.7k stars 209 forks source link

Specialize navigating map[string]interface{} #758

Open pelletier opened 2 years ago

pelletier commented 2 years ago

This is a common type. Specializing it to reduce the use of reflection yields better performance.

Similar to https://github.com/pelletier/go-toml/pull/669, there is a lot to explore there.

The overall structure of unmarshal.go will need to change to make this reasonable in terms of maintenance. But given the results it's probably worth it overall.

goos: darwin
goarch: arm64
pkg: github.com/pelletier/go-toml/v2/benchmark

name                               old time/op    new time/op    delta
UnmarshalDataset/config-8            13.3ms ± 0%    10.9ms ± 0%  -17.97%  (p=0.008 n=5+5)
UnmarshalDataset/canada-8            55.7ms ± 0%    55.4ms ± 1%     ~     (p=0.056 n=5+5)
UnmarshalDataset/citm_catalog-8      16.6ms ± 1%    15.7ms ± 0%   -5.36%  (p=0.008 n=5+5)
UnmarshalDataset/twitter-8           7.10ms ± 1%    6.63ms ± 0%   -6.55%  (p=0.008 n=5+5)
UnmarshalDataset/code-8              63.8ms ± 0%    48.7ms ± 2%  -23.69%  (p=0.008 n=5+5)
UnmarshalDataset/example-8            121µs ± 0%     110µs ± 3%   -8.79%  (p=0.008 n=5+5)
Unmarshal/SimpleDocument/struct-8     438ns ± 1%     431ns ± 1%   -1.76%  (p=0.008 n=5+5)
Unmarshal/SimpleDocument/map-8        594ns ± 2%     540ns ± 1%   -9.00%  (p=0.008 n=5+5)
Unmarshal/ReferenceFile/struct-8     34.3µs ± 1%    33.6µs ± 0%   -2.14%  (p=0.008 n=5+5)
Unmarshal/ReferenceFile/map-8        48.6µs ± 0%    41.7µs ± 1%  -14.34%  (p=0.008 n=5+5)
Unmarshal/HugoFrontMatter-8          7.88µs ± 1%    6.74µs ± 1%  -14.51%  (p=0.008 n=5+5)
[Geo mean]                            307µs          277µs        -9.80%

name                               old speed      new speed      delta
UnmarshalDataset/config-8          78.9MB/s ± 0%  96.1MB/s ± 0%  +21.90%  (p=0.008 n=5+5)
UnmarshalDataset/canada-8          39.5MB/s ± 0%  39.8MB/s ± 1%     ~     (p=0.056 n=5+5)
UnmarshalDataset/citm_catalog-8    33.6MB/s ± 1%  35.5MB/s ± 0%   +5.66%  (p=0.008 n=5+5)
UnmarshalDataset/twitter-8         62.3MB/s ± 1%  66.6MB/s ± 0%   +7.02%  (p=0.008 n=5+5)
UnmarshalDataset/code-8            42.1MB/s ± 0%  55.1MB/s ± 2%  +31.06%  (p=0.008 n=5+5)
UnmarshalDataset/example-8         66.9MB/s ± 0%  73.4MB/s ± 3%   +9.68%  (p=0.008 n=5+5)
Unmarshal/SimpleDocument/struct-8  25.1MB/s ± 1%  25.5MB/s ± 1%   +1.79%  (p=0.008 n=5+5)
Unmarshal/SimpleDocument/map-8     18.5MB/s ± 2%  20.4MB/s ± 1%   +9.91%  (p=0.008 n=5+5)
Unmarshal/ReferenceFile/struct-8    153MB/s ± 1%   156MB/s ± 0%   +2.19%  (p=0.008 n=5+5)
Unmarshal/ReferenceFile/map-8       108MB/s ± 0%   126MB/s ± 1%  +16.73%  (p=0.008 n=5+5)
Unmarshal/HugoFrontMatter-8        69.3MB/s ± 1%  81.0MB/s ± 1%  +16.97%  (p=0.008 n=5+5)
[Geo mean]                         53.2MB/s       59.0MB/s       +10.87%

name                               old alloc/op   new alloc/op   delta
UnmarshalDataset/config-8            5.86MB ± 0%    4.75MB ± 0%  -19.02%  (p=0.008 n=5+5)
UnmarshalDataset/canada-8            83.0MB ± 0%    83.0MB ± 0%   -0.00%  (p=0.008 n=5+5)
UnmarshalDataset/citm_catalog-8      34.7MB ± 0%    34.3MB ± 0%   -1.17%  (p=0.008 n=5+5)
UnmarshalDataset/twitter-8           12.7MB ± 0%    12.5MB ± 0%   -1.44%  (p=0.008 n=5+5)
UnmarshalDataset/code-8              22.2MB ± 0%    13.9MB ± 0%  -37.19%  (p=0.008 n=5+5)
UnmarshalDataset/example-8            186kB ± 0%     182kB ± 0%   -2.24%  (p=0.008 n=5+5)
Unmarshal/SimpleDocument/struct-8      805B ± 0%      805B ± 0%     ~     (all equal)
Unmarshal/SimpleDocument/map-8       1.13kB ± 0%    1.12kB ± 0%   -1.41%  (p=0.008 n=5+5)
Unmarshal/ReferenceFile/struct-8     20.9kB ± 0%    20.9kB ± 0%     ~     (all equal)
Unmarshal/ReferenceFile/map-8        38.2kB ± 0%    35.4kB ± 0%     ~     (p=0.079 n=4+5)
Unmarshal/HugoFrontMatter-8          7.44kB ± 0%    6.98kB ± 0%   -6.24%  (p=0.008 n=5+5)
[Geo mean]                            328kB          303kB        -7.69%

name                               old allocs/op  new allocs/op  delta
UnmarshalDataset/config-8              227k ± 0%      157k ± 0%  -30.73%  (p=0.029 n=4+4)
UnmarshalDataset/canada-8              782k ± 0%      782k ± 0%   -0.00%  (p=0.008 n=5+5)
UnmarshalDataset/citm_catalog-8        192k ± 0%      167k ± 0%  -13.18%  (p=0.008 n=5+5)
UnmarshalDataset/twitter-8            56.9k ± 0%     45.5k ± 0%  -20.02%  (p=0.000 n=5+4)
UnmarshalDataset/code-8               1.05M ± 0%     0.54M ± 0%  -48.97%  (p=0.008 n=5+5)
UnmarshalDataset/example-8            1.36k ± 0%     1.11k ± 0%  -18.65%  (p=0.008 n=5+5)
Unmarshal/SimpleDocument/struct-8      9.00 ± 0%      9.00 ± 0%     ~     (all equal)
Unmarshal/SimpleDocument/map-8         13.0 ± 0%      12.0 ± 0%   -7.69%  (p=0.008 n=5+5)
Unmarshal/ReferenceFile/struct-8        183 ± 0%       183 ± 0%     ~     (all equal)
Unmarshal/ReferenceFile/map-8           642 ± 0%       466 ± 0%  -27.41%  (p=0.008 n=5+5)
Unmarshal/HugoFrontMatter-8             141 ± 0%       112 ± 0%  -20.57%  (p=0.008 n=5+5)
[Geo mean]                            4.09k          3.34k       -18.44%