Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

aknuds1
Copy link
Contributor

@aknuds1 aknuds1 commented Feb 21, 2024

Add method PostingsForLabelMatching to tsdb.IndexReader, a method for obtaining an iterator over postings for labels with a certain name and values accepted by a provided callback, and use it from tsdb.PostingsForMatchers. The intention is to optimize regexp matcher paths, especially not having to load all label values before matching on them.

In addition, I refactor some tsdb/index.Reader methods and some tests in the same package. I can drop these refactorings from the PR if preferable :)

This PR is brought over from mimir-prometheus, where the changes have been reviewed in depth by @dimitarvdimitrov and @colega and approved. We (Grafana Labs) came to the conclusion that the changes are best upstreamed directly, instead of going via our fork.

Benchmarking shows memory reduction up to ~100%, and speedup of up to ~50%.

PostingsForLabelMatching Tests

I add one test for each of the two PostingsForLabelMatchingimplementations, as otherwise only the headIndexReader implementation gets tested (verified through test coverage analysis).

Benchmarks

Benchmarking stats, optimized vs non-optimized code, below:

PostingsForMatchers benchmark stats
goos: linux
goarch: amd64
pkg: github.com/prometheus/prometheus/tsdb
cpu: Intel(R) Xeon(R) Platinum 8280 CPU @ 2.70GHz
                                                                      │ main-postings-for-matchers-6.txt │ postings-for-matcher-postings-for-matchers-fast-6.txt │
                                                                      │              sec/op              │             sec/op              vs base               │
Querier/Head/PostingsForMatchers/n="1"-48                                                   476.1n ±  2%                     477.4n ±  2%        ~ (p=0.937 n=6)
Querier/Head/PostingsForMatchers/n="X"-48                                                   466.6n ±  3%                     467.6n ±  2%        ~ (p=0.818 n=6)
Querier/Head/PostingsForMatchers/n="1",j="foo"-48                                           739.0n ±  2%                     754.8n ±  1%   +2.13% (p=0.004 n=6)
Querier/Head/PostingsForMatchers/n="X",j="foo"-48                                           518.6n ±  1%                     530.6n ±  4%        ~ (p=0.180 n=6)
Querier/Head/PostingsForMatchers/j="foo",n="1"-48                                           740.8n ±  2%                     753.7n ±  3%        ~ (p=0.132 n=6)
Querier/Head/PostingsForMatchers/n="1",j!="foo"-48                                          718.2n ±  1%                     722.0n ±  3%        ~ (p=0.310 n=6)
Querier/Head/PostingsForMatchers/n="1",i!="2"-48                                            728.2n ±  2%                     762.6n ±  7%        ~ (p=0.065 n=6)
Querier/Head/PostingsForMatchers/n="X",j!="foo"-48                                          490.6n ±  2%                     505.0n ±  4%        ~ (p=0.065 n=6)
Querier/Head/PostingsForMatchers/i=~"1[0-9]",j=~"foo|bar"-48                                5.650m ±  1%                     4.897m ± 11%  -13.32% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/j=~"foo|bar"-48                                            1.008µ ±  4%                     1.021µ ±  1%        ~ (p=0.195 n=6)
Querier/Head/PostingsForMatchers/j=~"XXX|YYY"-48                                            884.2n ±  4%                     895.2n ±  2%        ~ (p=0.288 n=6)
Querier/Head/PostingsForMatchers/j=~"X.+"-48                                                641.9n ±  1%                     643.0n ±  4%        ~ (p=0.699 n=6)
Querier/Head/PostingsForMatchers/i=~"(1|2|3|4|5|6|20|55)"-48                                1.912µ ±  1%                     1.974µ ±  3%   +3.24% (p=0.024 n=6)
Querier/Head/PostingsForMatchers/i!~"(1|2|3|4|5|6|20|55)"-48                                2.034µ ±  1%                     2.058µ ±  3%        ~ (p=0.240 n=6)
Querier/Head/PostingsForMatchers/i=~"X|Y|Z"-48                                              1.062µ ±  2%                     1.099µ ±  7%   +3.48% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/i!~"X|Y|Z"-48                                              1.197µ ±  2%                     1.251µ ±  2%   +4.47% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/i=~".*"-48                                                 73.91m ±  2%                     76.86m ±  2%   +3.99% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/i=~"1.*"-48                                                14.24m ±  3%                     13.60m ±  2%   -4.52% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/i=~".*1"-48                                                6.350m ±  1%                     4.777m ±  4%  -24.77% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/i=~".+"-48                                                104.74m ±  1%                     97.75m ±  2%   -6.67% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/i=~".+",j=~"X.+"-48                                       106.45m ±  2%                     97.89m ±  0%   -8.03% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/i=~""-48                                                   47.14m ±  5%                     47.53m ±  2%        ~ (p=0.394 n=6)
Querier/Head/PostingsForMatchers/i!=""-48                                                   30.36m ±  1%                     30.30m ±  5%        ~ (p=0.937 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~".*",j="foo"-48                                   75.73m ±  3%                     77.04m ±  2%   +1.73% (p=0.015 n=6)
Querier/Head/PostingsForMatchers/n="X",i=~".*",j="foo"-48                                   653.5n ±  0%                     665.7n ±  2%   +1.87% (p=0.041 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~".*",i!="2",j="foo"-48                            74.36m ±  2%                     76.37m ±  2%   +2.70% (p=0.015 n=6)
Querier/Head/PostingsForMatchers/n="1",i!=""-48                                             30.30m ±  2%                     30.14m ±  8%        ~ (p=0.818 n=6)
Querier/Head/PostingsForMatchers/n="1",i!="",j="foo"-48                                     30.25m ±  2%                     30.06m ±  2%        ~ (p=0.699 n=6)
Querier/Head/PostingsForMatchers/n="1",i!="",j=~"X.+"-48                                    29.86m ±  2%                     29.80m ±  3%        ~ (p=0.937 n=6)
Querier/Head/PostingsForMatchers/n="1",i!="",j=~"XXX|YYY"-48                                30.05m ±  1%                     30.17m ±  3%        ~ (p=0.937 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~"X|Y|Z",j="foo"-48                                1.389µ ±  2%                     1.418µ ±  3%   +2.09% (p=0.009 n=6)
Querier/Head/PostingsForMatchers/n="1",i!~"X|Y|Z",j="foo"-48                                1.561µ ±  2%                     1.588µ ±  1%   +1.73% (p=0.006 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~".+",j="foo"-48                                  103.78m ±  2%                     97.90m ±  1%   -5.67% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~"1.+",j="foo"-48                                  14.18m ±  1%                     13.65m ±  1%   -3.73% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~".*1.*",j="foo"-48                                74.03m ±  2%                     73.07m ±  4%        ~ (p=0.093 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~".+",i!="2",j="foo"-48                           105.66m ±  2%                     97.09m ±  3%   -8.11% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~".+",i!~"2.*",j="foo"-48                          121.2m ±  4%                     110.1m ±  1%   -9.18% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~".+",i!~".*2.*",j="foo"-48                        180.5m ±  1%                     167.5m ±  2%   -7.18% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="X",i=~".+",i!~".*2.*",j="foo"-48                        643.7n ±  3%                     683.4n ±  4%   +6.17% (p=0.009 n=6)
Querier/Block/PostingsForMatchers/n="1"-48                                                  30.21µ ±  3%                     30.88µ ±  2%        ~ (p=0.180 n=6)
Querier/Block/PostingsForMatchers/n="X"-48                                                  527.5n ±  2%                     533.1n ±  2%        ~ (p=0.937 n=6)
Querier/Block/PostingsForMatchers/n="1",j="foo"-48                                          382.2µ ±  1%                     391.6µ ±  5%        ~ (p=0.093 n=6)
Querier/Block/PostingsForMatchers/n="X",j="foo"-48                                          583.6n ±  2%                     588.1n ±  2%        ~ (p=1.000 n=6)
Querier/Block/PostingsForMatchers/j="foo",n="1"-48                                          386.7µ ±  7%                     387.6µ ±  7%        ~ (p=0.937 n=6)
Querier/Block/PostingsForMatchers/n="1",j!="foo"-48                                         389.8µ ±  5%                     393.8µ ±  6%        ~ (p=0.240 n=6)
Querier/Block/PostingsForMatchers/n="1",i!="2"-48                                           31.93µ ±  0%                     31.77µ ±  4%        ~ (p=0.065 n=6)
Querier/Block/PostingsForMatchers/n="X",j!="foo"-48                                         562.4n ±  1%                     558.6n ±  2%        ~ (p=0.416 n=6)
Querier/Block/PostingsForMatchers/i=~"1[0-9]",j=~"foo|bar"-48                               3.544m ±  4%                     2.909m ±  1%  -17.90% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/j=~"foo|bar"-48                                           864.5µ ±  8%                     866.9µ ±  3%        ~ (p=0.818 n=6)
Querier/Block/PostingsForMatchers/j=~"XXX|YYY"-48                                           754.8n ±  1%                     798.3n ±  1%   +5.77% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/j=~"X.+"-48                                              1231.0n ± 27%                     613.3n ±  1%  -50.18% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/i=~"(1|2|3|4|5|6|20|55)"-48                               4.981µ ±  1%                     4.979µ ±  1%        ~ (p=0.937 n=6)
Querier/Block/PostingsForMatchers/i!~"(1|2|3|4|5|6|20|55)"-48                               879.5µ ±  6%                     874.2µ ±  2%        ~ (p=0.589 n=6)
Querier/Block/PostingsForMatchers/i=~"X|Y|Z"-48                                             982.2n ±  1%                    1008.5n ±  4%        ~ (p=0.372 n=6)
Querier/Block/PostingsForMatchers/i!~"X|Y|Z"-48                                             878.7µ ±  3%                     892.6µ ±  5%        ~ (p=0.485 n=6)
Querier/Block/PostingsForMatchers/i=~".*"-48                                                65.82m ±  2%                     67.12m ±  4%        ~ (p=0.310 n=6)
Querier/Block/PostingsForMatchers/i=~"1.*"-48                                               11.40m ±  3%                     10.65m ±  4%   -6.53% (p=0.004 n=6)
Querier/Block/PostingsForMatchers/i=~".*1"-48                                               3.034m ±  2%                     2.491m ±  3%  -17.88% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/i=~".+"-48                                                83.92m ±  6%                     81.39m ±  1%   -3.01% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/i=~".+",j=~"X.+"-48                                       82.53m ±  2%                     80.64m ±  1%   -2.29% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/i=~""-48                                                  30.85m ±  2%                     30.98m ±  0%        ~ (p=0.310 n=6)
Querier/Block/PostingsForMatchers/i!=""-48                                                  18.68m ±  8%                     18.80m ±  1%        ~ (p=0.589 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~".*",j="foo"-48                                  65.98m ±  4%                     65.87m ±  2%        ~ (p=0.485 n=6)
Querier/Block/PostingsForMatchers/n="X",i=~".*",j="foo"-48                                  719.9n ±  1%                     715.6n ±  1%        ~ (p=0.102 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~".*",i!="2",j="foo"-48                           65.38m ±  2%                     65.79m ±  3%        ~ (p=0.937 n=6)
Querier/Block/PostingsForMatchers/n="1",i!=""-48                                            19.06m ±  1%                     19.16m ±  7%        ~ (p=0.240 n=6)
Querier/Block/PostingsForMatchers/n="1",i!="",j="foo"-48                                    19.85m ±  1%                     20.32m ±  2%   +2.35% (p=0.004 n=6)
Querier/Block/PostingsForMatchers/n="1",i!="",j=~"X.+"-48                                   18.76m ±  3%                     19.26m ±  2%   +2.66% (p=0.026 n=6)
Querier/Block/PostingsForMatchers/n="1",i!="",j=~"XXX|YYY"-48                               18.86m ±  2%                     19.10m ±  3%   +1.24% (p=0.041 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~"X|Y|Z",j="foo"-48                               31.61µ ±  4%                     31.93µ ±  1%        ~ (p=0.394 n=6)
Querier/Block/PostingsForMatchers/n="1",i!~"X|Y|Z",j="foo"-48                               393.3µ ±  4%                     402.8µ ±  4%        ~ (p=0.065 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~".+",j="foo"-48                                  83.81m ±  2%                     82.72m ±  2%        ~ (p=0.310 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~"1.+",j="foo"-48                                 12.22m ±  4%                     11.46m ±  3%   -6.24% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~".*1.*",j="foo"-48                               65.82m ±  4%                     63.99m ±  4%   -2.79% (p=0.026 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~".+",i!="2",j="foo"-48                           85.41m ±  2%                     81.83m ±  1%   -4.19% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~".+",i!~"2.*",j="foo"-48                         99.39m ±  5%                     92.58m ±  1%   -6.86% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~".+",i!~".*2.*",j="foo"-48                       150.8m ±  6%                     147.8m ±  7%   -1.97% (p=0.041 n=6)
Querier/Block/PostingsForMatchers/n="X",i=~".+",i!~".*2.*",j="foo"-48                       731.9n ±  4%                     750.5n ±  3%        ~ (p=0.240 n=6)
geomean                                                                                     424.2µ                           415.4µ         -2.08%

                                                                      │ main-postings-for-matchers-6.txt │ postings-for-matcher-postings-for-matchers-fast-6.txt │
                                                                      │               B/op               │            B/op              vs base                  │
Querier/Head/PostingsForMatchers/n="1"-48                                                     64.00 ± 0%                    64.00 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="X"-48                                                     48.00 ± 0%                    48.00 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",j="foo"-48                                             176.0 ± 0%                    176.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="X",j="foo"-48                                             48.00 ± 0%                    48.00 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/j="foo",n="1"-48                                             176.0 ± 0%                    176.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",j!="foo"-48                                            176.0 ± 0%                    176.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i!="2"-48                                              176.0 ± 0%                    176.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="X",j!="foo"-48                                            48.00 ± 0%                    48.00 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/i=~"1[0-9]",j=~"foo|bar"-48                                1.531Mi ± 0%                  1.531Mi ± 0%    -0.00% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/j=~"foo|bar"-48                                              448.0 ± 0%                    448.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/j=~"XXX|YYY"-48                                              224.0 ± 0%                    224.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/j=~"X.+"-48                                                  104.0 ± 0%                    104.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/i=~"(1|2|3|4|5|6|20|55)"-48                                  848.0 ± 0%                    848.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/i!~"(1|2|3|4|5|6|20|55)"-48                                  912.0 ± 0%                    912.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/i=~"X|Y|Z"-48                                                344.0 ± 0%                    344.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/i!~"X|Y|Z"-48                                                408.0 ± 0%                    408.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/i=~".*"-48                                                 1.532Mi ± 0%                  1.532Mi ± 0%         ~ (p=1.000 n=6)
Querier/Head/PostingsForMatchers/i=~"1.*"-48                                                3.357Mi ± 0%                  3.185Mi ± 0%    -5.12% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/i=~".*1"-48                                                1.531Mi ± 0%                  1.531Mi ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/i=~".+"-48                                                 20.73Mi ± 0%                  19.20Mi ± 0%    -7.39% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/i=~".+",j=~"X.+"-48                                        20.73Mi ± 0%                  19.20Mi ± 0%    -7.39% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/i=~""-48                                                   20.73Mi ± 0%                  20.73Mi ± 0%         ~ (p=0.513 n=6)
Querier/Head/PostingsForMatchers/i!=""-48                                                   12.22Mi ± 0%                  12.22Mi ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i=~".*",j="foo"-48                                   1.532Mi ± 0%                  1.532Mi ± 0%         ~ (p=1.000 n=6)
Querier/Head/PostingsForMatchers/n="X",i=~".*",j="foo"-48                                     48.00 ± 0%                    48.00 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i=~".*",i!="2",j="foo"-48                            1.532Mi ± 0%                  1.532Mi ± 0%         ~ (p=0.455 n=6)
Querier/Head/PostingsForMatchers/n="1",i!=""-48                                             12.22Mi ± 0%                  12.22Mi ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i!="",j="foo"-48                                     12.22Mi ± 0%                  12.22Mi ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i!="",j=~"X.+"-48                                    12.22Mi ± 0%                  12.22Mi ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i!="",j=~"XXX|YYY"-48                                12.22Mi ± 0%                  12.22Mi ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i=~"X|Y|Z",j="foo"-48                                  408.0 ± 0%                    408.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i!~"X|Y|Z",j="foo"-48                                  520.0 ± 0%                    520.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i=~".+",j="foo"-48                                   20.73Mi ± 0%                  19.20Mi ± 0%    -7.39% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~"1.+",j="foo"-48                                  3.357Mi ± 0%                  3.186Mi ± 0%    -5.12% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~".*1.*",j="foo"-48                                9.146Mi ± 0%                  8.521Mi ± 0%    -6.84% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~".+",i!="2",j="foo"-48                            20.73Mi ± 0%                  19.20Mi ± 0%    -7.39% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~".+",i!~"2.*",j="foo"-48                          24.10Mi ± 0%                  22.39Mi ± 0%    -7.07% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~".+",i!~".*2.*",j="foo"-48                        29.89Mi ± 0%                  27.73Mi ± 0%    -7.21% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="X",i=~".+",i!~".*2.*",j="foo"-48                          48.00 ± 0%                    48.00 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1"-48                                                    80.00 ± 0%                    80.00 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="X"-48                                                    64.00 ± 0%                    64.00 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",j="foo"-48                                            208.0 ± 0%                    208.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="X",j="foo"-48                                            64.00 ± 0%                    64.00 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/j="foo",n="1"-48                                            208.0 ± 0%                    208.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",j!="foo"-48                                           208.0 ± 0%                    208.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i!="2"-48                                             208.0 ± 0%                    208.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="X",j!="foo"-48                                           64.00 ± 0%                    64.00 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/i=~"1[0-9]",j=~"foo|bar"-48                            1605722.00 ± 0%                    88.00 ± 1%   -99.99% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/j=~"foo|bar"-48                                             448.0 ± 0%                    448.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/j=~"XXX|YYY"-48                                             192.0 ± 0%                    192.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/j=~"X.+"-48                                               1224.00 ± 0%                    72.00 ± 0%   -94.12% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/i=~"(1|2|3|4|5|6|20|55)"-48                                 720.0 ± 0%                    720.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/i!~"(1|2|3|4|5|6|20|55)"-48                                 800.0 ± 0%                    800.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/i=~"X|Y|Z"-48                                               296.0 ± 0%                    296.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/i!~"X|Y|Z"-48                                               376.0 ± 0%                    376.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/i=~".*"-48                                                1.532Mi ± 0%                  1.532Mi ± 0%         ~ (p=0.333 n=6)
Querier/Block/PostingsForMatchers/i=~"1.*"-48                                               3.357Mi ± 0%                  1.654Mi ± 0%   -50.73% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/i=~".*1"-48                                            1605696.00 ± 0%                    64.00 ± 0%  -100.00% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/i=~".+"-48                                                20.73Mi ± 0%                  17.67Mi ± 0%   -14.77% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/i=~".+",j=~"X.+"-48                                       20.74Mi ± 0%                  17.67Mi ± 0%   -14.77% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/i=~""-48                                                  20.73Mi ± 0%                  20.73Mi ± 0%         ~ (p=0.870 n=6)
Querier/Block/PostingsForMatchers/i!=""-48                                                  12.22Mi ± 0%                  12.22Mi ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i=~".*",j="foo"-48                                  1.532Mi ± 0%                  1.532Mi ± 0%         ~ (p=0.636 n=6)
Querier/Block/PostingsForMatchers/n="X",i=~".*",j="foo"-48                                    64.00 ± 0%                    64.00 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i=~".*",i!="2",j="foo"-48                           1.532Mi ± 0%                  1.532Mi ± 0%         ~ (p=0.182 n=6)
Querier/Block/PostingsForMatchers/n="1",i!=""-48                                            12.22Mi ± 0%                  12.22Mi ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i!="",j="foo"-48                                    12.22Mi ± 0%                  12.22Mi ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i!="",j=~"X.+"-48                                   12.23Mi ± 0%                  12.22Mi ± 0%    -0.01% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="1",i!="",j=~"XXX|YYY"-48                               12.22Mi ± 0%                  12.22Mi ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i=~"X|Y|Z",j="foo"-48                                 376.0 ± 0%                    376.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i!~"X|Y|Z",j="foo"-48                                 504.0 ± 0%                    504.0 ± 0%         ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i=~".+",j="foo"-48                                  20.73Mi ± 0%                  17.67Mi ± 0%   -14.77% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~"1.+",j="foo"-48                                 3.357Mi ± 0%                  1.654Mi ± 0%   -50.73% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~".*1.*",j="foo"-48                               9.146Mi ± 0%                  6.989Mi ± 0%   -23.58% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~".+",i!="2",j="foo"-48                           20.73Mi ± 0%                  17.67Mi ± 0%   -14.77% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~".+",i!~"2.*",j="foo"-48                         24.10Mi ± 0%                  19.33Mi ± 0%   -19.78% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~".+",i!~".*2.*",j="foo"-48                       29.89Mi ± 0%                  24.67Mi ± 0%   -17.46% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="X",i=~".+",i!~".*2.*",j="foo"-48                         64.00 ± 0%                    64.00 ± 0%         ~ (p=1.000 n=6) ¹
geomean                                                                                     44.46Ki                       31.80Ki        -28.48%
¹ all samples are equal

                                                                      │ main-postings-for-matchers-6.txt │ postings-for-matcher-postings-for-matchers-fast-6.txt │
                                                                      │            allocs/op             │          allocs/op            vs base                 │
Querier/Head/PostingsForMatchers/n="1"-48                                                     3.000 ± 0%                     3.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="X"-48                                                     3.000 ± 0%                     3.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",j="foo"-48                                             7.000 ± 0%                     7.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="X",j="foo"-48                                             3.000 ± 0%                     3.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/j="foo",n="1"-48                                             7.000 ± 0%                     7.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",j!="foo"-48                                            7.000 ± 0%                     7.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i!="2"-48                                              7.000 ± 0%                     7.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="X",j!="foo"-48                                            3.000 ± 0%                     3.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/i=~"1[0-9]",j=~"foo|bar"-48                                  5.000 ± 0%                     5.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/j=~"foo|bar"-48                                              13.00 ± 0%                     13.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/j=~"XXX|YYY"-48                                              11.00 ± 0%                     11.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/j=~"X.+"-48                                                  5.000 ± 0%                     5.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/i=~"(1|2|3|4|5|6|20|55)"-48                                  31.00 ± 0%                     31.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/i!~"(1|2|3|4|5|6|20|55)"-48                                  34.00 ± 0%                     34.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/i=~"X|Y|Z"-48                                                15.00 ± 0%                     15.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/i!~"X|Y|Z"-48                                                18.00 ± 0%                     18.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/i=~".*"-48                                                   6.000 ± 0%                     6.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/i=~"1.*"-48                                                 11.14k ± 0%                    11.13k ± 0%   -0.01% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/i=~".*1"-48                                                  4.000 ± 0%                     4.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/i=~".+"-48                                                  100.0k ± 0%                    100.0k ± 0%   -0.00% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/i=~".+",j=~"X.+"-48                                         100.0k ± 0%                    100.0k ± 0%   -0.00% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/i=~""-48                                                    100.0k ± 0%                    100.0k ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/i!=""-48                                                    100.0k ± 0%                    100.0k ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i=~".*",j="foo"-48                                     10.00 ± 0%                     10.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="X",i=~".*",j="foo"-48                                     3.000 ± 0%                     3.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i=~".*",i!="2",j="foo"-48                              14.00 ± 0%                     14.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i!=""-48                                              100.0k ± 0%                    100.0k ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i!="",j="foo"-48                                      100.0k ± 0%                    100.0k ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i!="",j=~"X.+"-48                                     100.0k ± 0%                    100.0k ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i!="",j=~"XXX|YYY"-48                                 100.0k ± 0%                    100.0k ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i=~"X|Y|Z",j="foo"-48                                  18.00 ± 0%                     18.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i!~"X|Y|Z",j="foo"-48                                  22.00 ± 0%                     22.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/PostingsForMatchers/n="1",i=~".+",j="foo"-48                                    100.0k ± 0%                    100.0k ± 0%   -0.00% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~"1.+",j="foo"-48                                   11.14k ± 0%                    11.14k ± 0%   -0.01% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~".*1.*",j="foo"-48                                 40.99k ± 0%                    40.99k ± 0%   -0.00% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~".+",i!="2",j="foo"-48                             100.0k ± 0%                    100.0k ± 0%   -0.00% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~".+",i!~"2.*",j="foo"-48                           111.2k ± 0%                    111.2k ± 0%   -0.00% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="1",i=~".+",i!~".*2.*",j="foo"-48                         141.1k ± 0%                    141.1k ± 0%   -0.00% (p=0.002 n=6)
Querier/Head/PostingsForMatchers/n="X",i=~".+",i!~".*2.*",j="foo"-48                          3.000 ± 0%                     3.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1"-48                                                    4.000 ± 0%                     4.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="X"-48                                                    4.000 ± 0%                     4.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",j="foo"-48                                            9.000 ± 0%                     9.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="X",j="foo"-48                                            4.000 ± 0%                     4.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/j="foo",n="1"-48                                            9.000 ± 0%                     9.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",j!="foo"-48                                           9.000 ± 0%                     9.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i!="2"-48                                             9.000 ± 0%                     9.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="X",j!="foo"-48                                           4.000 ± 0%                     4.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/i=~"1[0-9]",j=~"foo|bar"-48                                 5.000 ± 0%                     4.000 ± 0%  -20.00% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/j=~"foo|bar"-48                                             13.00 ± 0%                     13.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/j=~"XXX|YYY"-48                                             9.000 ± 0%                     9.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/j=~"X.+"-48                                                 5.000 ± 0%                     4.000 ± 0%  -20.00% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/i=~"(1|2|3|4|5|6|20|55)"-48                                 23.00 ± 0%                     23.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/i!~"(1|2|3|4|5|6|20|55)"-48                                 27.00 ± 0%                     27.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/i=~"X|Y|Z"-48                                               12.00 ± 0%                     12.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/i!~"X|Y|Z"-48                                               16.00 ± 0%                     16.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/i=~".*"-48                                                  7.000 ± 0%                     7.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/i=~"1.*"-48                                                11.14k ± 0%                    11.13k ± 0%   -0.02% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/i=~".*1"-48                                                 4.000 ± 0%                     3.000 ± 0%  -25.00% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/i=~".+"-48                                                 100.0k ± 0%                    100.0k ± 0%   -0.00% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/i=~".+",j=~"X.+"-48                                        100.0k ± 0%                    100.0k ± 0%   -0.00% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/i=~""-48                                                   100.0k ± 0%                    100.0k ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/i!=""-48                                                   100.0k ± 0%                    100.0k ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i=~".*",j="foo"-48                                    12.00 ± 0%                     12.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="X",i=~".*",j="foo"-48                                    4.000 ± 0%                     4.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i=~".*",i!="2",j="foo"-48                             17.00 ± 0%                     17.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i!=""-48                                             100.0k ± 0%                    100.0k ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i!="",j="foo"-48                                     100.0k ± 0%                    100.0k ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i!="",j=~"X.+"-48                                    100.0k ± 0%                    100.0k ± 0%   -0.00% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="1",i!="",j=~"XXX|YYY"-48                                100.0k ± 0%                    100.0k ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i=~"X|Y|Z",j="foo"-48                                 16.00 ± 0%                     16.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i!~"X|Y|Z",j="foo"-48                                 21.00 ± 0%                     21.00 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/PostingsForMatchers/n="1",i=~".+",j="foo"-48                                   100.0k ± 0%                    100.0k ± 0%   -0.00% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~"1.+",j="foo"-48                                  11.14k ± 0%                    11.14k ± 0%   -0.02% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~".*1.*",j="foo"-48                                40.99k ± 0%                    40.99k ± 0%   -0.00% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~".+",i!="2",j="foo"-48                            100.0k ± 0%                    100.0k ± 0%   -0.00% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~".+",i!~"2.*",j="foo"-48                          111.2k ± 0%                    111.2k ± 0%   -0.00% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="1",i=~".+",i!~".*2.*",j="foo"-48                        141.1k ± 0%                    141.1k ± 0%   -0.00% (p=0.002 n=6)
Querier/Block/PostingsForMatchers/n="X",i=~".+",i!~".*2.*",j="foo"-48                         4.000 ± 0%                     4.000 ± 0%        ~ (p=1.000 n=6) ¹
geomean                                                                                       268.3                          265.8        -0.94%
¹ all samples are equal
labelValuesWithMatchers benchmark stats
goos: linux
goarch: amd64
pkg: github.com/prometheus/prometheus/tsdb
cpu: Intel(R) Xeon(R) Platinum 8280 CPU @ 2.70GHz
                                                                         │ main-labelvalues-6.txt │ postings-for-matcher-fast-labelvalues-6.txt │
                                                                         │         sec/op         │        sec/op          vs base              │
Querier/Head/labelValuesWithMatchers/i_with_i="1"-48                                 1.905m ±  3%             1.913m ± 2%       ~ (p=0.394 n=6)
Querier/Head/labelValuesWithMatchers/i_with_n="1"-48                                 174.1m ±  2%             174.0m ± 1%       ~ (p=0.818 n=6)
Querier/Head/labelValuesWithMatchers/i_with_n="^.+$"-48                              118.5m ±  2%             119.8m ± 3%       ~ (p=0.240 n=6)
Querier/Head/labelValuesWithMatchers/i_with_n="1",j!="foo"-48                        201.7m ±  4%             197.3m ± 2%       ~ (p=0.093 n=6)
Querier/Head/labelValuesWithMatchers/i_with_n="1",j=~"X.+"-48                        36.80m ±  1%             35.94m ± 2%  -2.32% (p=0.009 n=6)
Querier/Head/labelValuesWithMatchers/i_with_n="1",j=~"XXX|YYY"-48                    36.78m ±  2%             35.97m ± 1%  -2.20% (p=0.026 n=6)
Querier/Head/labelValuesWithMatchers/i_with_n="X",j!="foo"-48                        136.1m ±  2%             138.4m ± 1%       ~ (p=0.065 n=6)
Querier/Head/labelValuesWithMatchers/i_with_n="1",i=~"^.*$",j!="foo"-48              352.4m ±  2%             348.1m ± 2%       ~ (p=0.240 n=6)
Querier/Head/labelValuesWithMatchers/i_with_i="1aaa...ddd"-48                        1.913m ±  2%             1.901m ± 1%       ~ (p=0.240 n=6)
Querier/Head/labelValuesWithMatchers/i_with_i=~"1.+"-48                              12.04m ±  1%             11.90m ± 2%       ~ (p=0.180 n=6)
Querier/Head/labelValuesWithMatchers/i_with_i=~"1.+",i=~".+ddd"-48                   18.26m ±  1%             18.23m ± 0%       ~ (p=0.589 n=6)
Querier/Head/labelValuesWithMatchers/n_with_j!="foo"-48                              218.9m ±  4%             226.2m ± 1%       ~ (p=0.065 n=6)
Querier/Head/labelValuesWithMatchers/n_with_i="1"-48                                 8.238µ ±  2%             8.075µ ± 3%  -1.98% (p=0.026 n=6)
Querier/Head/labelValuesWithMatchers/n_with_i=~"1.+"-48                              28.91m ± 10%             28.41m ± 7%       ~ (p=0.394 n=6)
Querier/Head/labelValuesWithMatchers/none_with_i=~"1"-48                             270.9n ±  2%             272.9n ± 2%       ~ (p=0.485 n=6)
Querier/Block/labelValuesWithMatchers/i_with_i="1"-48                                2.357m ±  2%             2.352m ± 2%       ~ (p=0.937 n=6)
Querier/Block/labelValuesWithMatchers/i_with_n="1"-48                                176.2m ±  4%             176.9m ± 3%       ~ (p=0.589 n=6)
Querier/Block/labelValuesWithMatchers/i_with_n="^.+$"-48                             484.8m ±  2%             490.4m ± 4%       ~ (p=0.589 n=6)
Querier/Block/labelValuesWithMatchers/i_with_n="1",j!="foo"-48                       192.5m ±  4%             190.9m ± 2%       ~ (p=0.394 n=6)
Querier/Block/labelValuesWithMatchers/i_with_n="1",j=~"X.+"-48                       77.16m ±  1%             77.43m ± 3%       ~ (p=0.485 n=6)
Querier/Block/labelValuesWithMatchers/i_with_n="1",j=~"XXX|YYY"-48                   76.91m ±  1%             77.30m ± 2%       ~ (p=0.394 n=6)
Querier/Block/labelValuesWithMatchers/i_with_n="X",j!="foo"-48                       137.4m ±  2%             135.6m ± 1%  -1.31% (p=0.015 n=6)
Querier/Block/labelValuesWithMatchers/i_with_n="1",i=~"^.*$",j!="foo"-48             314.7m ±  1%             323.9m ± 1%  +2.92% (p=0.002 n=6)
Querier/Block/labelValuesWithMatchers/i_with_i="1aaa...ddd"-48                       2.363m ±  1%             2.342m ± 1%       ~ (p=0.394 n=6)
Querier/Block/labelValuesWithMatchers/i_with_i=~"1.+"-48                             9.473m ±  1%             9.488m ± 3%       ~ (p=0.589 n=6)
Querier/Block/labelValuesWithMatchers/i_with_i=~"1.+",i=~".+ddd"-48                  15.48m ±  4%             15.45m ± 1%       ~ (p=0.818 n=6)
Querier/Block/labelValuesWithMatchers/n_with_j!="foo"-48                             238.1m ±  3%             240.4m ± 5%       ~ (p=0.093 n=6)
Querier/Block/labelValuesWithMatchers/n_with_i="1"-48                                904.3µ ±  5%             940.7µ ± 6%  +4.03% (p=0.026 n=6)
Querier/Block/labelValuesWithMatchers/n_with_i=~"1.+"-48                             12.80m ±  2%             12.43m ± 3%  -2.90% (p=0.026 n=6)
Querier/Block/labelValuesWithMatchers/none_with_i=~"1"-48                            246.2n ±  3%             241.8n ± 2%       ~ (p=0.093 n=6)
geomean                                                                              13.44m                   13.42m       -0.15%

                                                                         │ main-labelvalues-6.txt │ postings-for-matcher-fast-labelvalues-6.txt │
                                                                         │          B/op          │        B/op         vs base                 │
Querier/Head/labelValuesWithMatchers/i_with_i="1"-48                               1.531Mi ± 0%           1.531Mi ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/i_with_n="1"-48                               16.14Mi ± 0%           16.14Mi ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/i_with_n="^.+$"-48                            16.15Mi ± 0%           16.14Mi ± 0%   -0.00% (p=0.002 n=6)
Querier/Head/labelValuesWithMatchers/i_with_n="1",j!="foo"-48                      16.14Mi ± 0%           16.14Mi ± 0%        ~ (p=0.455 n=6)
Querier/Head/labelValuesWithMatchers/i_with_n="1",j=~"X.+"-48                      10.70Mi ± 0%           10.70Mi ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/i_with_n="1",j=~"XXX|YYY"-48                  10.70Mi ± 0%           10.70Mi ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/i_with_n="X",j!="foo"-48                      16.14Mi ± 0%           16.14Mi ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/i_with_n="1",i=~"^.*$",j!="foo"-48            17.67Mi ± 0%           17.67Mi ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/i_with_i="1aaa...ddd"-48                      1.531Mi ± 0%           1.531Mi ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/i_with_i=~"1.+"-48                            1.531Mi ± 0%           1.531Mi ± 0%        ~ (p=0.080 n=6)
Querier/Head/labelValuesWithMatchers/i_with_i=~"1.+",i=~".+ddd"-48                 1.532Mi ± 0%           1.532Mi ± 0%        ~ (p=0.641 n=6)
Querier/Head/labelValuesWithMatchers/n_with_j!="foo"-48                            5.782Ki ± 0%           5.782Ki ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/n_with_i="1"-48                               4.695Ki ± 0%           4.695Ki ± 0%        ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/n_with_i=~"1.+"-48                            3.363Mi ± 0%           3.192Mi ± 0%   -5.11% (p=0.002 n=6)
Querier/Head/labelValuesWithMatchers/none_with_i=~"1"-48                             0.000 ± 0%             0.000 ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/i_with_i="1"-48                              1.531Mi ± 0%           1.531Mi ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/i_with_n="1"-48                              17.66Mi ± 0%           17.66Mi ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/i_with_n="^.+$"-48                           17.67Mi ± 0%           17.67Mi ± 0%   -0.01% (p=0.002 n=6)
Querier/Block/labelValuesWithMatchers/i_with_n="1",j!="foo"-48                     17.66Mi ± 0%           17.66Mi ± 0%        ~ (p=1.000 n=6)
Querier/Block/labelValuesWithMatchers/i_with_n="1",j=~"X.+"-48                     12.22Mi ± 0%           12.22Mi ± 0%   -0.01% (p=0.002 n=6)
Querier/Block/labelValuesWithMatchers/i_with_n="1",j=~"XXX|YYY"-48                 12.22Mi ± 0%           12.22Mi ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/i_with_n="X",j!="foo"-48                     17.66Mi ± 0%           17.66Mi ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/i_with_n="1",i=~"^.*$",j!="foo"-48           19.20Mi ± 0%           19.20Mi ± 0%        ~ (p=1.000 n=6)
Querier/Block/labelValuesWithMatchers/i_with_i="1aaa...ddd"-48                     1.531Mi ± 0%           1.531Mi ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/i_with_i=~"1.+"-48                           1.531Mi ± 0%           1.531Mi ± 0%        ~ (p=0.102 n=6)
Querier/Block/labelValuesWithMatchers/i_with_i=~"1.+",i=~".+ddd"-48                1.532Mi ± 0%           1.532Mi ± 0%        ~ (p=0.913 n=6)
Querier/Block/labelValuesWithMatchers/n_with_j!="foo"-48                           7.501Ki ± 0%           7.501Ki ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/n_with_i="1"-48                              6.398Ki ± 0%           6.398Ki ± 0%        ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/n_with_i=~"1.+"-48                           3.365Mi ± 0%           1.662Mi ± 0%  -50.61% (p=0.002 n=6)
Querier/Block/labelValuesWithMatchers/none_with_i=~"1"-48                            0.000 ± 0%             0.000 ± 0%        ~ (p=1.000 n=6) ¹
geomean                                                                                         ²                        -2.50%               ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                                                                         │ main-labelvalues-6.txt │ postings-for-matcher-fast-labelvalues-6.txt │
                                                                         │       allocs/op        │      allocs/op       vs base                │
Querier/Head/labelValuesWithMatchers/i_with_i="1"-48                                 1.000 ± 0%              1.000 ± 0%       ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/i_with_n="1"-48                                200.0k ± 0%             200.0k ± 0%       ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/i_with_n="^.+$"-48                             200.1k ± 0%             200.1k ± 0%  -0.00% (p=0.002 n=6)
Querier/Head/labelValuesWithMatchers/i_with_n="1",j!="foo"-48                       200.0k ± 0%             200.0k ± 0%       ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/i_with_n="1",j=~"X.+"-48                       200.0k ± 0%             200.0k ± 0%       ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/i_with_n="1",j=~"XXX|YYY"-48                   200.0k ± 0%             200.0k ± 0%       ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/i_with_n="X",j!="foo"-48                       200.0k ± 0%             200.0k ± 0%       ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/i_with_n="1",i=~"^.*$",j!="foo"-48             200.0k ± 0%             200.0k ± 0%       ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/i_with_i="1aaa...ddd"-48                        1.000 ± 0%              1.000 ± 0%       ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/i_with_i=~"1.+"-48                              1.000 ± 0%              1.000 ± 0%       ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/i_with_i=~"1.+",i=~".+ddd"-48                   1.000 ± 0%              1.000 ± 0%       ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/n_with_j!="foo"-48                              98.00 ± 0%              98.00 ± 0%       ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/n_with_i="1"-48                                 87.00 ± 0%              87.00 ± 0%       ~ (p=1.000 n=6) ¹
Querier/Head/labelValuesWithMatchers/n_with_i=~"1.+"-48                             11.23k ± 0%             11.23k ± 0%  -0.01% (p=0.002 n=6)
Querier/Head/labelValuesWithMatchers/none_with_i=~"1"-48                             0.000 ± 0%              0.000 ± 0%       ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/i_with_i="1"-48                                1.000 ± 0%              1.000 ± 0%       ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/i_with_n="1"-48                               300.0k ± 0%             300.0k ± 0%       ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/i_with_n="^.+$"-48                            300.1k ± 0%             300.1k ± 0%  -0.00% (p=0.002 n=6)
Querier/Block/labelValuesWithMatchers/i_with_n="1",j!="foo"-48                      300.0k ± 0%             300.0k ± 0%       ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/i_with_n="1",j=~"X.+"-48                      300.0k ± 0%             300.0k ± 0%  -0.00% (p=0.002 n=6)
Querier/Block/labelValuesWithMatchers/i_with_n="1",j=~"XXX|YYY"-48                  300.0k ± 0%             300.0k ± 0%       ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/i_with_n="X",j!="foo"-48                      300.0k ± 0%             300.0k ± 0%       ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/i_with_n="1",i=~"^.*$",j!="foo"-48            300.0k ± 0%             300.0k ± 0%       ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/i_with_i="1aaa...ddd"-48                       1.000 ± 0%              1.000 ± 0%       ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/i_with_i=~"1.+"-48                             1.000 ± 0%              1.000 ± 0%       ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/i_with_i=~"1.+",i=~".+ddd"-48                  1.000 ± 0%              1.000 ± 0%       ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/n_with_j!="foo"-48                             140.0 ± 0%              140.0 ± 0%       ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/n_with_i="1"-48                                128.0 ± 0%              128.0 ± 0%       ~ (p=1.000 n=6) ¹
Querier/Block/labelValuesWithMatchers/n_with_i=~"1.+"-48                            11.27k ± 0%             11.27k ± 0%  -0.02% (p=0.002 n=6)
Querier/Block/labelValuesWithMatchers/none_with_i=~"1"-48                            0.000 ± 0%              0.000 ± 0%       ~ (p=1.000 n=6) ¹
geomean                                                                                         ²                        -0.00%               ²
¹ all samples are equal
² summaries must be >0 to compute geomean

@aknuds1 aknuds1 force-pushed the arve/postings-for-matcher branch from 05c2717 to 4b2b2f6 Compare February 21, 2024 10:24
@aknuds1 aknuds1 marked this pull request as ready for review February 21, 2024 11:15
@aknuds1
Copy link
Contributor Author

aknuds1 commented Feb 21, 2024

@bwplotka @fpetkovski I hear you might want to review this?

Copy link
Member

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Nice. Thanks for this! One main question, otherwise LGTM, if prombench will be happy. I assume given you changed querier's postingForMatcher to used new index method, we should see improvement (or at least no regression, right?)

return
}

func FindSetMatches(pattern string) []string {
Copy link
Member

Choose a reason for hiding this comment

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

Missing comment for exported method 🤗

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, I can write one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wrote a basic doc string, is it good enough?

@bwplotka
Copy link
Member

/prombench main

@prombot
Copy link
Contributor

prombot commented Feb 21, 2024

⏱️ Welcome to Prometheus Benchmarking Tool. ⏱️

Compared versions: PR-13620 and main

After successful deployment, the benchmarking results can be viewed at:

Other Commands:
To stop benchmark: /prombench cancel
To restart benchmark: /prombench restart main

Co-authored-by: Bartlomiej Plotka <[email protected]>
Signed-off-by: Arve Knudsen <[email protected]>
@aknuds1
Copy link
Contributor Author

aknuds1 commented Feb 21, 2024

I assume given you changed querier's postingForMatcher to used new index method, we should see improvement (or at least no regression, right?)

@bwplotka there should only be improvement (for regexp matchers (as this should be the affected code path if I remember correctly, although not 100% sure), no longer having to load all label values before matching; if we see regressions I will be surprised. If you see the benchmark stats I included in the PR description, you can see that memory usage drops for one regexp matcher case for both head and block. In mimir-prometheus, there's also a second regexp matcher case that benefits from reduced memory. The stats also show some speedup, but I find that this changes between every time I bench (whereas memory reduction is constant) so I don't trust it.

aknuds1 and others added 4 commits February 21, 2024 16:46
Co-authored-by: Bartlomiej Plotka <[email protected]>
Signed-off-by: Arve Knudsen <[email protected]>
Signed-off-by: Arve Knudsen <[email protected]>
Signed-off-by: Arve Knudsen <[email protected]>
Signed-off-by: Arve Knudsen <[email protected]>
@aknuds1
Copy link
Contributor Author

aknuds1 commented Feb 21, 2024

I pushed a minor change to TestMemPostings_PostingsForMatcher, to make it order the series consistently with TestReader_PostingsForMatcher. Forgot to do this earlier.

Copy link
Contributor

@fpetkovski fpetkovski left a comment

Choose a reason for hiding this comment

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

Looks great to me.

@aknuds1 aknuds1 requested a review from fpetkovski February 23, 2024 12:47
Copy link
Contributor

@fpetkovski fpetkovski left a comment

Choose a reason for hiding this comment

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

Looking forward to trying this out.

@bwplotka
Copy link
Member

I don't have time to take a deeper look, but generally main looks better than this PR on prombench, unfortunately. Not significantly, but there is some diff:

CPU is 6% worse with this PR:

image

Plus queries are a bit slower on HTTP query_range (also engine avg):

image

http://prombench.prometheus.io/grafana/d/7gmLoNDmz/prombench?orgId=1&var-pr-number=13620

Any idea why? cc @bboreham

@prombot
Copy link
Contributor

prombot commented Feb 24, 2024

Benchmark tests are running for 3 days! If this is intended ignore this message otherwise you can cancel it by commenting: /prombench cancel

@aknuds1
Copy link
Contributor Author

aknuds1 commented Feb 24, 2024

Any idea why?

@bwplotka not really, but I'm also unfamiliar with prombench. Do you have any suggestions for how I might analyze the prombench results, to see e.g. what kind of queries are slower with my PR? Is it possible that calling ix.PostingsForMatcher (being an interface method) might be slightly slower than calling the free function postingsForMatcher, when one of its fast paths is hit?

@aknuds1
Copy link
Contributor Author

aknuds1 commented Feb 24, 2024

@bwplotka I just merged in some changes from main and pushed BTW. Among the changes coming in from main was a change from compress/gzip to github.com/klauspost/compress/gzip in util/httputil. I wonder if this type of change could make main perform better. Does prombench compare against the latest main, or the main revision that my PR is based on?

@bboreham
Copy link
Member

/prombench cancel

@aknuds1
Copy link
Contributor Author

aknuds1 commented Feb 26, 2024

@bwplotka @fpetkovski @bboreham after running BenchmarkQuerier/Head/PostingsForMatchers benchmarks on AMD64, I realized there is a performance regression (which doesn't appear on Apple ARM64, which I tried first). What happens is that regular expression label matching (actually calling strings.Suffix under the hood), while iterating over the p.m[m.Name] map, is slower (on AMD64) than doing the same while iterating over a slice of label values. I don't know why it would matter in practice whether you first make the slice iterating over the map or match while you iterate over the map. I tried benchmarking this particular execution path multiple times, and matching during map iteration was always slower than first making a slice and then matching over that.

I've tried solving the regression two ways:

  1. First make a slice of label values, for fast matching during iteration over slice
  2. Convert MemPostings to use SwissMap for the label value -> series refs map

The SwissMap solution is the fastest and uses the least memory, you can see my (AMD64) benchmark results in this Gist (one file for each solution).

Edit: I see now there's also a CockroachDB version of SwissMap,which I think is newer?

NB: I've drafted a PR for using dolthub SwissMap in MemPostings and one for the CockroachDB equivalent, so @bboreham can prombench them.

@aknuds1 aknuds1 changed the title tsdb.IndexReader: Add method PostingsForMatcher tsdb.IndexReader: Add method PostingsForLabelMatching Apr 8, 2024
@aknuds1
Copy link
Contributor Author

aknuds1 commented Apr 9, 2024

I pushed a major revision of the PR, after discussing with @bboreham and eventually realizing that label matching doesn't need to be pushed into tsdb/index and can stay in tsdb instead. Also included refactorings of test and non-test code, am OK with dropping those from the PR if preferable. Will fix up the PR description correspondingly tomorrow. Tests for PostingsForLabelMatching also need adding.

@aknuds1 aknuds1 changed the title tsdb.IndexReader: Add method PostingsForLabelMatching tsdb.IndexReader: Add method IndexReader.PostingsForLabelMatching Apr 9, 2024
@aknuds1 aknuds1 changed the title tsdb.IndexReader: Add method IndexReader.PostingsForLabelMatching tsdb.IndexReader: Add method PostingsForLabelMatching Apr 9, 2024
@aknuds1 aknuds1 requested a review from bboreham April 10, 2024 13:19

// traversePostingOffsets traverses r's posting offsets table, starting at off, and calls cb with every label value and postings offset.
// If cb returns false (or an error), the traversing is interrupted.
func (r *Reader) traversePostingOffsets(ctx context.Context, off int, cb func(string, uint64) (bool, error)) error {
Copy link
Member

Choose a reason for hiding this comment

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

Should this be named ...V2, since there seem to be alternate implementations for V1 every place it is called?

Copy link
Contributor Author

@aknuds1 aknuds1 May 9, 2024

Choose a reason for hiding this comment

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

My thinking for not giving it the V2 suffix is basically that tsdb/index.Reader uses the "V1" suffix for its legacy v1 postings offset table: postingsV1, while the non-legacy v2 table is just called postings. I figured it's an intentional naming scheme to use a version suffix on the legacy code. WDYT?

No big deal either way :) Just how I thought about it.

@aknuds1 aknuds1 requested a review from bboreham May 9, 2024 08:58
@aknuds1
Copy link
Contributor Author

aknuds1 commented May 9, 2024

On feedback from @bboreham - pushed a revision splitting out tsdb/index.Reader test/benchmark refactoring, since it's become irrelevant to this PR (after dropping new tsdb/index test code).

Copy link
Member

@bboreham bboreham left a comment

Choose a reason for hiding this comment

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

lgtm!

Signed-off-by: Arve Knudsen <[email protected]>
@bboreham bboreham merged commit 5c4310a into prometheus:main May 9, 2024
@aknuds1 aknuds1 deleted the arve/postings-for-matcher branch May 9, 2024 09:56
bboreham pushed a commit that referenced this pull request Nov 19, 2024
Simple follow-up to #13620. Modify `tsdb.PostingsForMatchers` to use the optimized tsdb.IndexReader.PostingsForLabelMatching method also for inverse matching.

Introduce method `PostingsForAllLabelValues`, to avoid changing the existing method.

The performance is much improved for a subset of the cases; there are up to
~60% CPU gains and ~12.5% reduction in memory usage. 

Remove `TestReader_InversePostingsForMatcherHonorsContextCancel` since
`inversePostingsForMatcher` only passes `ctx` to `IndexReader` implementations now.

Signed-off-by: Arve Knudsen <[email protected]>
julienduchesne pushed a commit to julienduchesne/prometheus that referenced this pull request Dec 13, 2024
Simple follow-up to prometheus#13620. Modify `tsdb.PostingsForMatchers` to use the optimized tsdb.IndexReader.PostingsForLabelMatching method also for inverse matching.

Introduce method `PostingsForAllLabelValues`, to avoid changing the existing method.

The performance is much improved for a subset of the cases; there are up to
~60% CPU gains and ~12.5% reduction in memory usage.

Remove `TestReader_InversePostingsForMatcherHonorsContextCancel` since
`inversePostingsForMatcher` only passes `ctx` to `IndexReader` implementations now.

Signed-off-by: Arve Knudsen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants