Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mimir query engine: avoid copying native histograms where possible #8677

Merged
merged 11 commits into from
Jul 29, 2024

Conversation

charleskorn
Copy link
Contributor

@charleskorn charleskorn commented Jul 11, 2024

What this PR does

This PR improves the performance of queries over native histograms in MQE. It does this by avoiding creating copies of FloatHistograms where possible.

This improves query latency for queries over native histograms in MQE by up to 20%. This has no noticeable impact on queries over only floats, and makes MQE up to 50% faster than Prometheus' engine in some cases.

For example, previously, while evaluating a sum over native histograms, we would always copy FloatHistogram instances that were the first value for an output group. However, this is not necessary: we only need to make a copy if the same FloatHistogram will be used as the first value for subsequent points.

Performance comparison of this PR vs Prometheus' engine
goos: darwin
goarch: arm64
pkg: github.com/grafana/mimir/pkg/streamingpromql/benchmarks
                                                                 │      Prometheus      │                Mimir                │
                                                                 │        sec/op        │    sec/op     vs base               │
Query/nh_1,_instant_query                                                  158.8µ ±  8%   140.2µ ±  2%  -11.68% (p=0.002 n=6)
Query/nh_1,_range_query_with_100_steps                                     221.2µ ±  6%   192.1µ ±  2%  -13.15% (p=0.002 n=6)
Query/nh_1,_range_query_with_1000_steps                                    774.4µ ±  2%   637.9µ ±  1%  -17.63% (p=0.002 n=6)
Query/nh_100,_instant_query                                                2.118m ±  8%   1.946m ±  3%   -8.10% (p=0.009 n=6)
Query/nh_100,_range_query_with_100_steps                                   7.724m ± 18%   6.305m ±  3%  -18.38% (p=0.041 n=6)
Query/nh_100,_range_query_with_1000_steps                                  55.77m ±  1%   44.35m ±  6%  -20.48% (p=0.002 n=6)
Query/nh_2000,_instant_query                                               38.62m ± 14%   32.80m ±  7%  -15.08% (p=0.004 n=6)
Query/nh_2000,_range_query_with_100_steps                                  139.8m ±  1%   114.0m ± 12%  -18.45% (p=0.002 n=6)
Query/nh_2000,_range_query_with_1000_steps                                1031.3m ±  1%   809.3m ±  1%  -21.52% (p=0.002 n=6)
Query/sum(nh_1),_instant_query                                             145.0µ ±  5%   140.8µ ±  6%   -2.91% (p=0.041 n=6)
Query/sum(nh_1),_range_query_with_100_steps                                225.9µ ±  0%   195.3µ ±  1%  -13.52% (p=0.002 n=6)
Query/sum(nh_1),_range_query_with_1000_steps                               862.6µ ±  3%   650.6µ ±  3%  -24.57% (p=0.002 n=6)
Query/sum(nh_100),_instant_query                                           1.986m ±  2%   1.943m ±  1%   -2.16% (p=0.002 n=6)
Query/sum(nh_100),_range_query_with_100_steps                              7.069m ±  1%   6.813m ±  0%   -3.62% (p=0.002 n=6)
Query/sum(nh_100),_range_query_with_1000_steps                             55.44m ±  1%   48.88m ±  1%  -11.83% (p=0.002 n=6)
Query/sum(nh_2000),_instant_query                                          33.83m ±  1%   32.70m ±  1%   -3.36% (p=0.002 n=6)
Query/sum(nh_2000),_range_query_with_100_steps                             170.3m ±  1%   126.3m ±  1%  -25.87% (p=0.002 n=6)
Query/sum(nh_2000),_range_query_with_1000_steps                           1546.6m ±  1%   929.0m ±  1%  -39.93% (p=0.002 n=6)
Query/sum_by_(l)(nh_1),_instant_query                                      146.1µ ±  4%   140.1µ ±  1%   -4.08% (p=0.002 n=6)
Query/sum_by_(l)(nh_1),_range_query_with_100_steps                         232.3µ ±  3%   193.8µ ±  3%  -16.56% (p=0.002 n=6)
Query/sum_by_(l)(nh_1),_range_query_with_1000_steps                        859.1µ ±  1%   636.1µ ±  1%  -25.96% (p=0.002 n=6)
Query/sum_by_(l)(nh_100),_instant_query                                    2.032m ±  1%   1.959m ±  0%   -3.59% (p=0.002 n=6)
Query/sum_by_(l)(nh_100),_range_query_with_100_steps                       8.550m ±  1%   6.377m ±  0%  -25.42% (p=0.002 n=6)
Query/sum_by_(l)(nh_100),_range_query_with_1000_steps                      68.18m ±  2%   44.88m ±  1%  -34.16% (p=0.002 n=6)
Query/sum_by_(l)(nh_2000),_instant_query                                   35.04m ±  1%   34.18m ±  1%   -2.45% (p=0.002 n=6)
Query/sum_by_(l)(nh_2000),_range_query_with_100_steps                      198.4m ±  1%   115.3m ±  2%  -41.89% (p=0.002 n=6)
Query/sum_by_(l)(nh_2000),_range_query_with_1000_steps                    1759.5m ±  1%   819.6m ±  4%  -53.42% (p=0.002 n=6)

                                                                 │      Prometheus      │                Mimir                │
                                                                 │          B           │      B        vs base               │
Query/nh_1,_instant_query                                                  78.95Mi ± 2%   79.27Mi ± 1%        ~ (p=0.699 n=6)
Query/nh_1,_range_query_with_100_steps                                     72.15Mi ± 1%   71.22Mi ± 1%   -1.29% (p=0.002 n=6)
Query/nh_1,_range_query_with_1000_steps                                    70.04Mi ± 1%   70.20Mi ± 2%        ~ (p=0.699 n=6)
Query/nh_100,_instant_query                                                66.00Mi ± 1%   65.48Mi ± 0%   -0.78% (p=0.002 n=6)
Query/nh_100,_range_query_with_100_steps                                   70.69Mi ± 1%   70.78Mi ± 1%        ~ (p=0.416 n=6)
Query/nh_100,_range_query_with_1000_steps                                  117.8Mi ± 1%   118.0Mi ± 1%        ~ (p=0.485 n=6)
Query/nh_2000,_instant_query                                               68.50Mi ± 1%   67.95Mi ± 2%        ~ (p=0.093 n=6)
Query/nh_2000,_range_query_with_100_steps                                  173.9Mi ± 1%   175.8Mi ± 1%   +1.09% (p=0.015 n=6)
Query/nh_2000,_range_query_with_1000_steps                                 1.063Gi ± 2%   1.090Gi ± 3%        ~ (p=0.065 n=6)
Query/sum(nh_1),_instant_query                                             79.20Mi ± 1%   78.90Mi ± 1%        ~ (p=0.310 n=6)
Query/sum(nh_1),_range_query_with_100_steps                                71.27Mi ± 1%   72.12Mi ± 1%   +1.19% (p=0.026 n=6)
Query/sum(nh_1),_range_query_with_1000_steps                               70.38Mi ± 1%   70.33Mi ± 1%        ~ (p=0.615 n=6)
Query/sum(nh_100),_instant_query                                           66.10Mi ± 1%   65.51Mi ± 1%        ~ (p=0.056 n=6)
Query/sum(nh_100),_range_query_with_100_steps                              71.01Mi ± 1%   65.30Mi ± 1%   -8.04% (p=0.002 n=6)
Query/sum(nh_100),_range_query_with_1000_steps                            118.10Mi ± 1%   67.77Mi ± 1%  -42.61% (p=0.002 n=6)
Query/sum(nh_2000),_instant_query                                          68.60Mi ± 1%   66.36Mi ± 1%   -3.27% (p=0.002 n=6)
Query/sum(nh_2000),_range_query_with_100_steps                            172.78Mi ± 2%   66.95Mi ± 1%  -61.25% (p=0.002 n=6)
Query/sum(nh_2000),_range_query_with_1000_steps                           718.55Mi ± 4%   74.10Mi ± 1%  -89.69% (p=0.002 n=6)
Query/sum_by_(l)(nh_1),_instant_query                                      78.72Mi ± 2%   78.98Mi ± 1%        ~ (p=0.909 n=6)
Query/sum_by_(l)(nh_1),_range_query_with_100_steps                         71.11Mi ± 1%   71.42Mi ± 1%        ~ (p=0.260 n=6)
Query/sum_by_(l)(nh_1),_range_query_with_1000_steps                        70.97Mi ± 1%   70.41Mi ± 1%        ~ (p=0.093 n=6)
Query/sum_by_(l)(nh_100),_instant_query                                    65.93Mi ± 1%   65.91Mi ± 1%        ~ (p=0.818 n=6)
Query/sum_by_(l)(nh_100),_range_query_with_100_steps                       76.09Mi ± 0%   71.05Mi ± 1%   -6.62% (p=0.002 n=6)
Query/sum_by_(l)(nh_100),_range_query_with_1000_steps                      169.9Mi ± 1%   118.2Mi ± 1%  -30.40% (p=0.002 n=6)
Query/sum_by_(l)(nh_2000),_instant_query                                   70.18Mi ± 1%   69.38Mi ± 1%   -1.15% (p=0.024 n=6)
Query/sum_by_(l)(nh_2000),_range_query_with_100_steps                      280.8Mi ± 2%   177.7Mi ± 1%  -36.74% (p=0.002 n=6)
Query/sum_by_(l)(nh_2000),_range_query_with_1000_steps                     1.110Gi ± 0%   1.093Gi ± 5%        ~ (p=1.000 n=6)
Performance comparison of main vs this PR

Peak memory consumption is unchanged within the margin of error for the benchmarks.

goos: darwin
goarch: arm64
pkg: github.com/grafana/mimir/pkg/streamingpromql/benchmarks
                                                                          │   main.txt    │       with-copy-avoidance.txt       │
                                                                          │    sec/op     │    sec/op     vs base               │
Query/nh_1,_instant_query/Mimir-10                                           140.8µ ±  2%   140.2µ ±  2%        ~ (p=0.258 n=6)
Query/nh_1,_range_query_with_100_steps/Mimir-10                              193.1µ ±  2%   192.1µ ±  2%        ~ (p=0.394 n=6)
Query/nh_1,_range_query_with_1000_steps/Mimir-10                             639.6µ ±  2%   637.9µ ±  1%        ~ (p=0.937 n=6)
Query/nh_100,_instant_query/Mimir-10                                         1.949m ±  1%   1.946m ±  3%        ~ (p=0.818 n=6)
Query/nh_100,_range_query_with_100_steps/Mimir-10                            6.337m ±  4%   6.305m ±  3%        ~ (p=0.240 n=6)
Query/nh_100,_range_query_with_1000_steps/Mimir-10                           44.77m ±  3%   44.35m ±  6%        ~ (p=0.699 n=6)
Query/nh_2000,_instant_query/Mimir-10                                        32.90m ±  1%   32.80m ±  7%        ~ (p=0.394 n=6)
Query/nh_2000,_range_query_with_100_steps/Mimir-10                           112.8m ±  1%   114.0m ± 12%        ~ (p=0.180 n=6)
Query/nh_2000,_range_query_with_1000_steps/Mimir-10                          815.0m ±  3%   809.3m ±  1%        ~ (p=0.589 n=6)
Query/sum(nh_1),_instant_query/Mimir-10                                      141.4µ ±  2%   140.8µ ±  6%        ~ (p=0.240 n=6)
Query/sum(nh_1),_range_query_with_100_steps/Mimir-10                         220.6µ ±  7%   195.3µ ±  1%  -11.44% (p=0.002 n=6)
Query/sum(nh_1),_range_query_with_1000_steps/Mimir-10                        793.8µ ±  3%   650.6µ ±  3%  -18.04% (p=0.002 n=6)
Query/sum(nh_100),_instant_query/Mimir-10                                    1.965m ±  1%   1.943m ±  1%   -1.13% (p=0.002 n=6)
Query/sum(nh_100),_range_query_with_100_steps/Mimir-10                       6.883m ±  1%   6.813m ±  0%   -1.02% (p=0.002 n=6)
Query/sum(nh_100),_range_query_with_1000_steps/Mimir-10                      49.23m ±  1%   48.88m ±  1%        ~ (p=0.065 n=6)
Query/sum(nh_2000),_instant_query/Mimir-10                                   32.82m ±  0%   32.70m ±  1%        ~ (p=0.132 n=6)
Query/sum(nh_2000),_range_query_with_100_steps/Mimir-10                      126.7m ±  1%   126.3m ±  1%        ~ (p=0.394 n=6)
Query/sum(nh_2000),_range_query_with_1000_steps/Mimir-10                     944.9m ±  1%   929.0m ±  1%   -1.68% (p=0.004 n=6)
Query/sum_by_(l)(nh_1),_instant_query/Mimir-10                               148.7µ ±  3%   140.1µ ±  1%   -5.79% (p=0.002 n=6)
Query/sum_by_(l)(nh_1),_range_query_with_100_steps/Mimir-10                  213.8µ ±  3%   193.8µ ±  3%   -9.33% (p=0.002 n=6)
Query/sum_by_(l)(nh_1),_range_query_with_1000_steps/Mimir-10                 772.0µ ±  1%   636.1µ ±  1%  -17.60% (p=0.002 n=6)
Query/sum_by_(l)(nh_100),_instant_query/Mimir-10                             1.982m ±  0%   1.959m ±  0%   -1.16% (p=0.002 n=6)
Query/sum_by_(l)(nh_100),_range_query_with_100_steps/Mimir-10                7.617m ±  1%   6.377m ±  0%  -16.28% (p=0.002 n=6)
Query/sum_by_(l)(nh_100),_range_query_with_1000_steps/Mimir-10               53.80m ±  1%   44.88m ±  1%  -16.58% (p=0.002 n=6)
Query/sum_by_(l)(nh_2000),_instant_query/Mimir-10                            34.17m ± 18%   34.18m ±  1%        ~ (p=0.937 n=6)
Query/sum_by_(l)(nh_2000),_range_query_with_100_steps/Mimir-10               135.7m ±  2%   115.3m ±  2%  -15.02% (p=0.002 n=6)
Query/sum_by_(l)(nh_2000),_range_query_with_1000_steps/Mimir-10             1037.8m ±  3%   819.6m ±  4%  -21.03% (p=0.002 n=6)

Which issue(s) this PR fixes or relates to

Fixes #8360 (comment)

Checklist

  • Tests updated.
  • [n/a] Documentation added.
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX].
  • [n/a] about-versioning.md updated with experimental features.

@charleskorn charleskorn force-pushed the charleskorn/avoid-histogram-copies branch from ac35ff2 to 8ac0155 Compare July 11, 2024 06:49
Copy link
Contributor

@jhesketh jhesketh left a comment

Choose a reason for hiding this comment

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

Nice stuff. I think this makes sense to me. Some more tests would be good, but otherwise I think we should proceed with this approach.

@charleskorn charleskorn force-pushed the charleskorn/avoid-histogram-copies branch from 8ac0155 to 962d549 Compare July 26, 2024 06:36
@charleskorn charleskorn force-pushed the charleskorn/avoid-histogram-copies branch from 5d61f05 to 3275480 Compare July 29, 2024 03:24
@charleskorn charleskorn marked this pull request as ready for review July 29, 2024 03:25
@charleskorn charleskorn requested a review from a team as a code owner July 29, 2024 03:25
Copy link
Contributor

@jhesketh jhesketh left a comment

Choose a reason for hiding this comment

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

lgtm, just some nits/queries

Copy link
Contributor

@jhesketh jhesketh left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM!

@charleskorn charleskorn merged commit 17dfb34 into main Jul 29, 2024
29 checks passed
@charleskorn charleskorn deleted the charleskorn/avoid-histogram-copies branch July 29, 2024 06:35
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