Skip to content

RuleGroup: use stdlib for json decoding, optimize decoding - #2109

Merged
bwplotka merged 4 commits into
prometheus:mainfrom
liggitt:rule-group
Sep 10, 2026
Merged

RuleGroup: use stdlib for json decoding, optimize decoding#2109
bwplotka merged 4 commits into
prometheus:mainfrom
liggitt:rule-group

Conversation

@liggitt

@liggitt liggitt commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

cc @bwplotka

part of #2105

  1. Adds a benchmark for RuleGroup decoding
  2. Switches to stdlib
  3. Avoids decoding rule bytes multiple times
  4. Avoids struct copies

Allocs and bytes consumed are significantly better than json-iter on both Go 1.26 and 1.27

CPU is worse on Go 1.26, better on Go 1.27.

goos: darwin
goarch: arm64
pkg: github.com/prometheus/client_golang/api/prometheus/v1
cpu: Apple M4 Pro
                       │    main     │              go1.26.6               │              go1.27.1              │
                       │   sec/op    │    sec/op     vs base               │   sec/op     vs base               │
RuleGroup/streaming-14   944.5µ ± 1%   1551.7µ ± 0%  +64.28% (p=0.002 n=6)   787.3µ ± 3%  -16.64% (p=0.002 n=6)
RuleGroup/unmarshal-14   885.9µ ± 1%   1521.7µ ± 1%  +71.76% (p=0.002 n=6)   722.0µ ± 2%  -18.50% (p=0.002 n=6)
geomean                  914.7µ         1.537m       +67.98%                 754.0µ       -17.58%

                       │     main      │              go1.26.6               │              go1.27.1               │
                       │     B/op      │     B/op      vs base               │     B/op      vs base               │
RuleGroup/streaming-14   1293.9Ki ± 0%   832.7Ki ± 0%  -35.64% (p=0.002 n=6)   620.5Ki ± 0%  -52.04% (p=0.002 n=6)
RuleGroup/unmarshal-14    998.0Ki ± 0%   578.3Ki ± 0%  -42.05% (p=0.002 n=6)   363.5Ki ± 0%  -63.57% (p=0.002 n=6)
geomean                   1.110Mi        693.9Ki       -38.93%                 475.0Ki       -58.20%

                       │     main     │              go1.26.6              │              go1.27.1              │
                       │  allocs/op   │  allocs/op   vs base               │  allocs/op   vs base               │
RuleGroup/streaming-14   27.396k ± 0%   9.614k ± 0%  -64.91% (p=0.002 n=6)   3.868k ± 0%  -85.88% (p=0.002 n=6)
RuleGroup/unmarshal-14   27.314k ± 0%   9.601k ± 0%  -64.85% (p=0.002 n=6)   3.850k ± 0%  -85.90% (p=0.002 n=6)
geomean                   27.35k        9.607k       -64.88%                 3.859k       -85.89%

Baseline benchmark

go1.26.6 test ./api/prometheus/v1 -bench RuleGroup -benchmem -run ^$ -count=6

goos: darwin
goarch: arm64
pkg: github.com/prometheus/client_golang/api/prometheus/v1
cpu: Apple M4 Pro
BenchmarkRuleGroup/streaming-14         	    1059	    945867 ns/op	 1324909 B/op	   27396 allocs/op
BenchmarkRuleGroup/streaming-14         	    1258	    946632 ns/op	 1324947 B/op	   27396 allocs/op
BenchmarkRuleGroup/streaming-14         	    1298	    936014 ns/op	 1324919 B/op	   27396 allocs/op
BenchmarkRuleGroup/streaming-14         	    1275	    942051 ns/op	 1324939 B/op	   27396 allocs/op
BenchmarkRuleGroup/streaming-14         	    1280	    949613 ns/op	 1324919 B/op	   27396 allocs/op
BenchmarkRuleGroup/streaming-14         	    1272	    943165 ns/op	 1324928 B/op	   27396 allocs/op
BenchmarkRuleGroup/unmarshal-14         	    1358	    884021 ns/op	 1021988 B/op	   27314 allocs/op
BenchmarkRuleGroup/unmarshal-14         	    1347	    885628 ns/op	 1021997 B/op	   27314 allocs/op
BenchmarkRuleGroup/unmarshal-14         	    1353	    892039 ns/op	 1021991 B/op	   27314 allocs/op
BenchmarkRuleGroup/unmarshal-14         	    1330	    886184 ns/op	 1021980 B/op	   27314 allocs/op
BenchmarkRuleGroup/unmarshal-14         	    1359	    884447 ns/op	 1021998 B/op	   27314 allocs/op
BenchmarkRuleGroup/unmarshal-14         	    1350	    890479 ns/op	 1022020 B/op	   27314 allocs/op
PASS
ok  	github.com/prometheus/client_golang/api/prometheus/v1	15.595s

Signed-off-by: Jordan Liggitt <liggitt@google.com>
Benchmark delta on go1.26.6

goos: darwin
goarch: arm64
pkg: github.com/prometheus/client_golang/api/prometheus/v1
cpu: Apple M4 Pro
                       │ 1-jsoniter.txt │             2-gojson.txt             │
                       │     sec/op     │    sec/op     vs base                │
RuleGroup/streaming-14      944.5µ ± 1%   2083.1µ ± 1%  +120.54% (p=0.002 n=6)
RuleGroup/unmarshal-14      885.9µ ± 1%   2143.6µ ± 2%  +141.97% (p=0.002 n=6)
geomean                     914.7µ         2.113m       +131.01%

                       │ 1-jsoniter.txt │            2-gojson.txt             │
                       │      B/op      │     B/op      vs base               │
RuleGroup/streaming-14    1293.9Ki ± 0%   962.7Ki ± 0%  -25.60% (p=0.002 n=6)
RuleGroup/unmarshal-14     998.0Ki ± 0%   708.2Ki ± 0%  -29.05% (p=0.002 n=6)
geomean                    1.110Mi        825.7Ki       -27.34%

                       │ 1-jsoniter.txt │            2-gojson.txt            │
                       │   allocs/op    │  allocs/op   vs base               │
RuleGroup/streaming-14      27.40k ± 0%   11.92k ± 0%  -56.49% (p=0.002 n=6)
RuleGroup/unmarshal-14      27.31k ± 0%   11.91k ± 0%  -56.41% (p=0.002 n=6)
geomean                     27.35k        11.91k       -56.45%

Signed-off-by: Jordan Liggitt <liggitt@google.com>
goos: darwin
goarch: arm64
pkg: github.com/prometheus/client_golang/api/prometheus/v1
cpu: Apple M4 Pro
                       │ 2-gojson.txt │          3-typefield.txt           │
                       │    sec/op    │   sec/op     vs base               │
RuleGroup/streaming-14    2.083m ± 1%   1.606m ± 0%  -22.93% (p=0.002 n=6)
RuleGroup/unmarshal-14    2.144m ± 2%   1.604m ± 3%  -25.18% (p=0.002 n=6)
geomean                   2.113m        1.605m       -24.06%

                       │ 2-gojson.txt │           3-typefield.txt           │
                       │     B/op     │     B/op      vs base               │
RuleGroup/streaming-14   962.7Ki ± 0%   832.7Ki ± 0%  -13.50% (p=0.002 n=6)
RuleGroup/unmarshal-14   708.2Ki ± 0%   578.3Ki ± 0%  -18.33% (p=0.002 n=6)
geomean                  825.7Ki        693.9Ki       -15.95%

                       │ 2-gojson.txt │          3-typefield.txt           │
                       │  allocs/op   │  allocs/op   vs base               │
RuleGroup/streaming-14   11.920k ± 0%   9.614k ± 0%  -19.35% (p=0.002 n=6)
RuleGroup/unmarshal-14   11.907k ± 0%   9.601k ± 0%  -19.37% (p=0.002 n=6)
geomean                   11.91k        9.607k       -19.36%

Signed-off-by: Jordan Liggitt <liggitt@google.com>
goos: darwin
goarch: arm64
pkg: github.com/prometheus/client_golang/api/prometheus/v1
cpu: Apple M4 Pro
                       │ 3-typefield.txt │         4-structcopy.txt          │
                       │     sec/op      │   sec/op     vs base              │
RuleGroup/streaming-14       1.606m ± 0%   1.552m ± 0%  -3.35% (p=0.002 n=6)
RuleGroup/unmarshal-14       1.604m ± 3%   1.522m ± 1%  -5.13% (p=0.002 n=6)
geomean                      1.605m        1.537m       -4.25%

                       │ 3-typefield.txt │          4-structcopy.txt          │
                       │      B/op       │     B/op      vs base              │
RuleGroup/streaming-14      832.7Ki ± 0%   832.7Ki ± 0%       ~ (p=0.418 n=6)
RuleGroup/unmarshal-14      578.3Ki ± 0%   578.3Ki ± 0%       ~ (p=0.197 n=6)
geomean                     693.9Ki        693.9Ki       -0.00%

                       │ 3-typefield.txt │          4-structcopy.txt           │
                       │    allocs/op    │  allocs/op   vs base                │
RuleGroup/streaming-14       9.614k ± 0%   9.614k ± 0%       ~ (p=1.000 n=6) ¹
RuleGroup/unmarshal-14       9.601k ± 0%   9.601k ± 0%       ~ (p=1.000 n=6) ¹
geomean                      9.607k        9.607k       +0.00%

Signed-off-by: Jordan Liggitt <liggitt@google.com>
Comment thread api/prometheus/v1/api.go

@bwplotka bwplotka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@bwplotka
bwplotka merged commit 3f29fcf into prometheus:main Sep 10, 2026
15 checks passed
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