Skip to content

model: reduce allocations in Time.UnmarshalJSON#918

Open
bboreham wants to merge 4 commits into
mainfrom
time-split
Open

model: reduce allocations in Time.UnmarshalJSON#918
bboreham wants to merge 4 commits into
mainfrom
time-split

Conversation

@bboreham

@bboreham bboreham commented Jun 8, 2026

Copy link
Copy Markdown
Member

The basic idea was to use strings.Cut instead of strings.Split.

However I also removed another allocation if the decimal was smaller than three figures, and fixed a bug for negative times.

Benchmarks:

goos: linux
goarch: amd64
pkg: github.com/prometheus/common/model
cpu: Intel(R) Core(TM) i7-14700K
                                │   before    │               after                │
                                │   sec/op    │   sec/op     vs base               │
UnmarshalTime/1780924784-28       39.89n ± 0%   18.98n ± 1%  -52.42% (p=0.002 n=6)
UnmarshalTime/1780924784.01-28    67.99n ± 0%   26.68n ± 1%  -60.76% (p=0.002 n=6)
UnmarshalTime/1780924784.001-28   53.84n ± 1%   27.64n ± 0%  -48.66% (p=0.002 n=6)
geomean                           52.66n        24.10n       -54.23%

                                │   before   │                 after                 │
                                │    B/op    │   B/op     vs base                    │
UnmarshalTime/1780924784-28       32.00 ± 0%   0.00 ± 0%  -100.00% (p=0.002 n=6)
UnmarshalTime/1780924784.01-28    48.00 ± 0%   0.00 ± 0%  -100.00% (p=0.002 n=6)
UnmarshalTime/1780924784.001-28   48.00 ± 0%   0.00 ± 0%  -100.00% (p=0.002 n=6)
geomean                           41.93                   ?                      ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean

                                │   before   │                 after                  │
                                │ allocs/op  │ allocs/op   vs base                    │
UnmarshalTime/1780924784-28       2.000 ± 0%   0.000 ± 0%  -100.00% (p=0.002 n=6)
UnmarshalTime/1780924784.01-28    3.000 ± 0%   0.000 ± 0%  -100.00% (p=0.002 n=6)
UnmarshalTime/1780924784.001-28   2.000 ± 0%   0.000 ± 0%  -100.00% (p=0.002 n=6)
geomean                           2.289                    ?                      ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean

bboreham added 4 commits June 8, 2026 17:56
Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
Use strings.Cut instead of strings.Split.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
We don't need to modify the string version of the number via a memory
allocation; it's much faster to modify the integer version.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
@bboreham bboreham requested a review from SuperQ June 9, 2026 10:45
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.

2 participants