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

Reduce newSeriesChunkRefsSet() introducing a memory pool #3666

Merged
merged 7 commits into from
Dec 13, 2022

Conversation

pracucci
Copy link
Collaborator

@pracucci pracucci commented Dec 7, 2022

What this PR does

In this PR I'm proposing to reduce newSeriesChunkRefsSet() introducing a memory pool. The contract proposed in this PR is:

  • The caller to seriesChunkRefsSetIterator.At() is responsible to release() a seriesChunkRefsSet once not used anymore.
  • A seriesChunkRefsSet can be declared non releseable when created. If so, the release() has no effect. This is useful, for example, in tests where the set could be retained and reused.
  • In every place where I call release() I added a comment to explain why it's safe (at least, why I think it is...).

I've added a couple of new tests with concurrency. These tests also ensure all slices requested from pool are also released back (assert on balance = 0). I've tried to manually introduce a bug, releasing the same slice twice to the pool in different places of the code, and the new concurrency tests failed in all cases.

Benchmark results:

I've run 3 benchmarks:

  • Bucket_Series
  • Bucket_Series_WithSkipChunks
  • MergedSeriesChunkRefsSetIterators (new)
name                                                                                                                 old time/op    new time/op    delta
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(1K_per_batch)/1of1000000-12                             74.6ms ± 1%    69.3ms ± 1%   -7.11%  (p=0.001 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(1K_per_batch)/10of1000000-12                            80.7ms ± 8%    69.7ms ± 1%     ~     (p=0.073 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(1K_per_batch)/1000000of1000000-12                        1.46s ± 6%     1.29s ± 2%  -11.54%  (p=0.047 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(10K_per_batch)/1of1000000-12                            70.4ms ± 2%    57.5ms ± 3%  -18.27%  (p=0.000 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(10K_per_batch)/10of1000000-12                           66.6ms ± 8%    57.7ms ± 1%     ~     (p=0.084 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(10K_per_batch)/1000000of1000000-12                       1.30s ± 3%     1.18s ± 3%   -9.07%  (p=0.010 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_default_options/1of1000000-12                                              101ms ± 1%     103ms ± 2%     ~     (p=0.157 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_default_options/10of1000000-12                                             101ms ± 0%     101ms ± 0%     ~     (p=0.725 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_default_options/1000000of1000000-12                                        1.72s ± 5%     1.55s ± 4%     ~     (p=0.052 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_default_options/1of10000000-12                                           5.79ms ± 2%    5.83ms ± 3%     ~     (p=0.778 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_default_options/100of10000000-12                                         5.79ms ± 2%    5.68ms ± 1%     ~     (p=0.194 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_default_options/10000000of10000000-12                                     164ms ± 3%     162ms ± 3%     ~     (p=0.629 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(1K_per_batch)/1of10000000-12                           8.62ms ± 8%    7.39ms ± 1%     ~     (p=0.067 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(1K_per_batch)/100of10000000-12                         8.26ms ± 3%    7.31ms ± 1%  -11.53%  (p=0.013 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(1K_per_batch)/10000000of10000000-12                     152ms ± 4%     138ms ± 1%   -9.32%  (p=0.039 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(10K_per_batch)/1of10000000-12                          6.51ms ± 2%    5.91ms ± 0%   -9.10%  (p=0.007 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(10K_per_batch)/100of10000000-12                        6.41ms ± 2%    5.87ms ± 0%   -8.31%  (p=0.016 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(10K_per_batch)/10000000of10000000-12                    159ms ± 1%     152ms ± 1%   -4.12%  (p=0.002 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(1K_per_batch)/1of10000000-12                            246µs ± 1%     230µs ± 0%   -6.19%  (p=0.012 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(1K_per_batch)/100of10000000-12                          255µs ± 4%     230µs ± 0%   -9.66%  (p=0.042 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(1K_per_batch)/10000000of10000000-12                    86.0ms ± 3%    84.6ms ± 2%     ~     (p=0.422 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(10K_per_batch)/1of10000000-12                           349µs ± 2%     232µs ± 0%  -33.61%  (p=0.001 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(10K_per_batch)/100of10000000-12                         378µs ± 7%     233µs ± 0%  -38.40%  (p=0.009 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(10K_per_batch)/10000000of10000000-12                   84.5ms ± 1%    84.5ms ± 1%     ~     (p=0.885 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_default_options/1of10000000-12                                            230µs ± 2%     220µs ± 0%   -4.26%  (p=0.034 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_default_options/100of10000000-12                                          231µs ± 3%     223µs ± 1%     ~     (p=0.209 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_default_options/10000000of10000000-12                                    77.6ms ± 2%    76.7ms ± 2%     ~     (p=0.366 n=3+3)
Bucket_Series_WithSkipChunks/1000000SeriesWith1Samples/with_default_options/1000000of1000000-12                         1.23s ± 0%     1.21s ± 3%     ~     (p=0.326 n=3+3)
Bucket_Series_WithSkipChunks/1000000SeriesWith1Samples/with_series_streaming_(1K_per_batch)/1000000of1000000-12         982ms ± 2%     915ms ± 2%   -6.81%  (p=0.012 n=3+3)
Bucket_Series_WithSkipChunks/1000000SeriesWith1Samples/with_series_streaming_(10K_per_batch)/1000000of1000000-12        928ms ± 1%     896ms ± 1%   -3.48%  (p=0.004 n=3+3)
Bucket_Series_WithSkipChunks/100000SeriesWith100Samples/with_series_streaming_(10K_per_batch)/10000000of10000000-12     119ms ± 3%     108ms ± 1%   -9.17%  (p=0.031 n=3+3)
Bucket_Series_WithSkipChunks/100000SeriesWith100Samples/with_default_options/10000000of10000000-12                      116ms ± 2%     112ms ± 1%   -3.85%  (p=0.036 n=3+3)
Bucket_Series_WithSkipChunks/100000SeriesWith100Samples/with_series_streaming_(1K_per_batch)/10000000of10000000-12      104ms ± 1%      95ms ± 0%   -8.08%  (p=0.004 n=3+3)
Bucket_Series_WithSkipChunks/1SeriesWith10000000Samples/with_default_options/10000000of10000000-12                      393µs ± 2%     384µs ± 0%     ~     (p=0.100 n=3+3)
Bucket_Series_WithSkipChunks/1SeriesWith10000000Samples/with_series_streaming_(1K_per_batch)/10000000of10000000-12      697µs ± 1%     567µs ± 1%  -18.69%  (p=0.000 n=3+3)
Bucket_Series_WithSkipChunks/1SeriesWith10000000Samples/with_series_streaming_(10K_per_batch)/10000000of10000000-12    1.59ms ± 1%    0.55ms ± 0%  -65.07%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_1-12                             57.6µs ± 0%     2.4µs ± 0%  -95.81%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_2-12                              211µs ± 1%       8µs ± 0%  -96.00%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_4-12                              506µs ± 3%      20µs ± 1%  -95.99%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_8-12                             1.06ms ± 1%    0.04ms ± 1%  -95.77%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_16-12                            2.15ms ± 1%    0.09ms ± 3%  -95.65%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_32-12                            4.53ms ± 2%    0.19ms ± 1%  -95.84%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_64-12                            8.19ms ± 3%    0.38ms ± 1%  -95.37%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_1-12                            53.4µs ± 9%     2.4µs ± 0%  -95.52%  (p=0.002 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_2-12                             218µs ± 6%      12µs ± 1%  -94.70%  (p=0.001 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_4-12                             542µs ± 5%      35µs ± 1%  -93.45%  (p=0.001 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_8-12                            1.17ms ± 3%    0.10ms ± 1%  -91.37%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_16-12                           2.24ms ± 7%    0.28ms ± 1%  -87.52%  (p=0.002 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_32-12                           5.37ms ± 1%    0.78ms ± 0%  -85.44%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_64-12                           10.8ms ± 1%     2.3ms ± 0%  -79.06%  (p=0.000 n=3+3)

name                                                                                                                 old alloc/op   new alloc/op   delta
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(1K_per_batch)/1of1000000-12                             96.3MB ± 0%    84.0MB ± 0%  -12.78%  (p=0.000 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(1K_per_batch)/10of1000000-12                            96.3MB ± 0%    84.1MB ± 0%  -12.76%  (p=0.000 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(1K_per_batch)/1000000of1000000-12                       1.57GB ± 0%    1.37GB ± 0%  -12.48%  (p=0.000 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(10K_per_batch)/1of1000000-12                            71.5MB ± 0%    59.0MB ± 0%  -17.38%  (p=0.000 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(10K_per_batch)/10of1000000-12                           71.5MB ± 0%    59.0MB ± 0%  -17.36%  (p=0.000 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(10K_per_batch)/1000000of1000000-12                      1.43GB ± 0%    1.24GB ± 0%  -13.19%  (p=0.000 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_default_options/1of1000000-12                                             65.9MB ± 0%    65.9MB ± 0%     ~     (p=0.955 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_default_options/10of1000000-12                                            65.9MB ± 0%    65.9MB ± 0%     ~     (p=0.739 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_default_options/1000000of1000000-12                                       1.79GB ± 0%    1.79GB ± 0%     ~     (p=0.813 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_default_options/1of10000000-12                                           5.04MB ± 0%    5.04MB ± 0%     ~     (p=0.114 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_default_options/100of10000000-12                                         5.05MB ± 0%    5.05MB ± 0%     ~     (p=0.516 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_default_options/10000000of10000000-12                                     210MB ± 1%     209MB ± 0%     ~     (p=0.605 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(1K_per_batch)/1of10000000-12                           9.48MB ± 0%    8.23MB ± 0%  -13.25%  (p=0.000 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(1K_per_batch)/100of10000000-12                         9.49MB ± 0%    8.23MB ± 0%  -13.32%  (p=0.000 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(1K_per_batch)/10000000of10000000-12                     193MB ± 0%     173MB ± 0%  -10.10%  (p=0.000 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(10K_per_batch)/1of10000000-12                          7.43MB ± 0%    5.76MB ± 0%  -22.46%  (p=0.000 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(10K_per_batch)/100of10000000-12                        7.43MB ± 0%    5.77MB ± 0%  -22.30%  (p=0.000 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(10K_per_batch)/10000000of10000000-12                    186MB ± 0%     168MB ± 0%   -9.57%  (p=0.000 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(1K_per_batch)/1of10000000-12                            266kB ± 0%     219kB ± 0%  -17.69%  (p=0.000 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(1K_per_batch)/100of10000000-12                          267kB ± 0%     219kB ± 0%  -17.86%  (p=0.000 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(1K_per_batch)/10000000of10000000-12                     226MB ± 0%     226MB ± 0%   -0.15%  (p=0.000 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(10K_per_batch)/1of10000000-12                           705kB ± 0%     242kB ± 0%  -65.67%  (p=0.000 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(10K_per_batch)/100of10000000-12                         705kB ± 0%     242kB ± 0%  -65.61%  (p=0.000 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(10K_per_batch)/10000000of10000000-12                    230MB ± 0%     227MB ± 0%   -1.49%  (p=0.004 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_default_options/1of10000000-12                                            217kB ± 0%     217kB ± 0%     ~     (p=0.788 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_default_options/100of10000000-12                                          217kB ± 0%     217kB ± 0%     ~     (p=0.628 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_default_options/10000000of10000000-12                                     223MB ± 0%     223MB ± 0%     ~     (p=0.926 n=3+3)
Bucket_Series_WithSkipChunks/1000000SeriesWith1Samples/with_default_options/1000000of1000000-12                        1.95GB ± 0%    1.95GB ± 0%     ~     (p=0.863 n=3+3)
Bucket_Series_WithSkipChunks/1000000SeriesWith1Samples/with_series_streaming_(1K_per_batch)/1000000of1000000-12        1.14GB ± 0%    0.95GB ± 0%  -17.16%  (p=0.000 n=3+3)
Bucket_Series_WithSkipChunks/1000000SeriesWith1Samples/with_series_streaming_(10K_per_batch)/1000000of1000000-12       1.04GB ± 0%    0.85GB ± 0%  -18.27%  (p=0.000 n=3+3)
Bucket_Series_WithSkipChunks/100000SeriesWith100Samples/with_series_streaming_(10K_per_batch)/10000000of10000000-12     105MB ± 0%      86MB ± 0%  -17.82%  (p=0.000 n=3+3)
Bucket_Series_WithSkipChunks/100000SeriesWith100Samples/with_default_options/10000000of10000000-12                      179MB ± 0%     179MB ± 0%   -0.01%  (p=0.018 n=3+3)
Bucket_Series_WithSkipChunks/100000SeriesWith100Samples/with_series_streaming_(1K_per_batch)/10000000of10000000-12      113MB ± 0%      94MB ± 0%  -17.21%  (p=0.000 n=3+3)
Bucket_Series_WithSkipChunks/1SeriesWith10000000Samples/with_default_options/10000000of10000000-12                      709kB ± 0%     709kB ± 0%     ~     (p=0.660 n=3+3)
Bucket_Series_WithSkipChunks/1SeriesWith10000000Samples/with_series_streaming_(1K_per_batch)/10000000of10000000-12     1.19MB ± 0%    0.72MB ± 0%  -39.39%  (p=0.000 n=3+3)
Bucket_Series_WithSkipChunks/1SeriesWith10000000Samples/with_series_streaming_(10K_per_batch)/10000000of10000000-12    5.53MB ± 0%    0.86MB ± 0%  -84.40%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_1-12                              246kB ± 0%       0kB ± 1%  -99.91%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_2-12                              984kB ± 0%       1kB ± 0%  -99.93%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_4-12                             2.46MB ± 0%    0.00MB ± 1%  -99.93%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_8-12                             5.41MB ± 0%    0.00MB ± 0%  -99.94%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_16-12                            11.3MB ± 0%     0.0MB ± 1%  -99.94%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_32-12                            23.1MB ± 0%     0.0MB ± 1%  -99.94%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_64-12                            46.7MB ± 0%     0.0MB ± 1%  -99.94%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_1-12                             246kB ± 0%       0kB ± 0%  -99.91%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_2-12                             984kB ± 0%       1kB ± 1%  -99.93%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_4-12                            2.46MB ± 0%    0.00MB ± 1%  -99.93%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_8-12                            5.41MB ± 0%    0.00MB ± 0%  -99.93%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_16-12                           11.3MB ± 0%     0.0MB ± 3%  -99.94%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_32-12                           23.1MB ± 0%     0.0MB ± 2%  -99.93%  (p=0.000 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_64-12                           47.0MB ± 0%     0.0MB ± 5%  -99.94%  (p=0.000 n=3+3)

name                                                                                                                 old allocs/op  new allocs/op  delta
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(1K_per_batch)/1of1000000-12                              14.3k ± 0%     14.3k ± 0%     ~     (p=1.000 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(1K_per_batch)/10of1000000-12                             14.5k ± 0%     14.5k ± 0%     ~     (p=0.503 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(1K_per_batch)/1000000of1000000-12                        17.1M ± 0%     17.1M ± 0%     ~     (p=0.862 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(10K_per_batch)/1of1000000-12                             6.21k ± 0%     6.20k ± 0%     ~     (p=0.837 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(10K_per_batch)/10of1000000-12                            6.36k ± 0%     6.34k ± 0%     ~     (p=0.373 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_series_streaming_(10K_per_batch)/1000000of1000000-12                       17.0M ± 0%     17.0M ± 0%     ~     (p=0.189 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_default_options/1of1000000-12                                              9.75k ± 0%     9.75k ± 0%     ~     (p=0.950 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_default_options/10of1000000-12                                             9.92k ± 0%     9.91k ± 0%     ~     (p=0.892 n=3+3)
Bucket_Series/1000000SeriesWith1Samples/with_default_options/1000000of1000000-12                                        18.0M ± 0%     18.0M ± 0%     ~     (p=0.803 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_default_options/1of10000000-12                                            1.14k ± 0%     1.14k ± 0%     ~     (p=0.337 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_default_options/100of10000000-12                                          1.20k ± 0%     1.20k ± 0%     ~     (p=0.725 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_default_options/10000000of10000000-12                                     1.81M ± 0%     1.81M ± 0%     ~     (p=0.873 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(1K_per_batch)/1of10000000-12                            1.64k ± 0%     1.64k ± 0%     ~     (p=0.150 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(1K_per_batch)/100of10000000-12                          1.70k ± 0%     1.70k ± 0%   +0.16%  (p=0.015 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(1K_per_batch)/10000000of10000000-12                     1.71M ± 0%     1.71M ± 0%     ~     (p=0.373 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(10K_per_batch)/1of10000000-12                             822 ± 0%       823 ± 0%     ~     (p=0.468 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(10K_per_batch)/100of10000000-12                           874 ± 0%       876 ± 0%     ~     (p=0.474 n=3+3)
Bucket_Series/100000SeriesWith100Samples/with_series_streaming_(10K_per_batch)/10000000of10000000-12                    1.70M ± 0%     1.70M ± 0%     ~     (p=0.070 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(1K_per_batch)/1of10000000-12                              250 ± 0%       250 ± 0%     ~     (zero variance)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(1K_per_batch)/100of10000000-12                            250 ± 0%       250 ± 0%     ~     (zero variance)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(1K_per_batch)/10000000of10000000-12                      334k ± 0%      334k ± 0%   +0.00%  (p=0.000 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(10K_per_batch)/1of10000000-12                             251 ± 0%       250 ± 0%     ~     (zero variance)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(10K_per_batch)/100of10000000-12                           251 ± 0%       250 ± 0%     ~     (zero variance)
Bucket_Series/1SeriesWith10000000Samples/with_series_streaming_(10K_per_batch)/10000000of10000000-12                     334k ± 0%      334k ± 0%   +0.00%  (p=0.020 n=3+3)
Bucket_Series/1SeriesWith10000000Samples/with_default_options/1of10000000-12                                              244 ± 0%       244 ± 0%     ~     (zero variance)
Bucket_Series/1SeriesWith10000000Samples/with_default_options/100of10000000-12                                            244 ± 0%       244 ± 0%     ~     (zero variance)
Bucket_Series/1SeriesWith10000000Samples/with_default_options/10000000of10000000-12                                      334k ± 0%      334k ± 0%     ~     (p=0.777 n=3+3)
Bucket_Series_WithSkipChunks/1000000SeriesWith1Samples/with_default_options/1000000of1000000-12                         11.0M ± 0%     11.0M ± 0%     ~     (p=0.903 n=3+3)
Bucket_Series_WithSkipChunks/1000000SeriesWith1Samples/with_series_streaming_(1K_per_batch)/1000000of1000000-12         10.1M ± 0%     10.1M ± 0%     ~     (p=0.897 n=3+3)
Bucket_Series_WithSkipChunks/1000000SeriesWith1Samples/with_series_streaming_(10K_per_batch)/1000000of1000000-12        10.0M ± 0%     10.0M ± 0%     ~     (p=0.629 n=3+3)
Bucket_Series_WithSkipChunks/100000SeriesWith100Samples/with_series_streaming_(10K_per_batch)/10000000of10000000-12     1.00M ± 0%     1.00M ± 0%   +0.00%  (p=0.022 n=3+3)
Bucket_Series_WithSkipChunks/100000SeriesWith100Samples/with_default_options/10000000of10000000-12                      1.10M ± 0%     1.10M ± 0%   -0.00%  (p=0.018 n=3+3)
Bucket_Series_WithSkipChunks/100000SeriesWith100Samples/with_series_streaming_(1K_per_batch)/10000000of10000000-12      1.01M ± 0%     1.01M ± 0%     ~     (p=0.241 n=3+3)
Bucket_Series_WithSkipChunks/1SeriesWith10000000Samples/with_default_options/10000000of10000000-12                        798 ± 0%       798 ± 0%     ~     (zero variance)
Bucket_Series_WithSkipChunks/1SeriesWith10000000Samples/with_series_streaming_(1K_per_batch)/10000000of10000000-12        618 ± 0%       620 ± 0%     ~     (zero variance)
Bucket_Series_WithSkipChunks/1SeriesWith10000000Samples/with_series_streaming_(10K_per_batch)/10000000of10000000-12       621 ± 0%       619 ± 0%   -0.32%  (p=0.013 n=3+3)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_1-12                               5.00 ± 0%      5.00 ± 0%     ~     (zero variance)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_2-12                               15.0 ± 0%      15.0 ± 0%     ~     (zero variance)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_4-12                               35.0 ± 0%      35.0 ± 0%     ~     (zero variance)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_8-12                               75.0 ± 0%      75.0 ± 0%     ~     (zero variance)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_16-12                               155 ± 0%       155 ± 0%     ~     (zero variance)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_32-12                               315 ± 0%       315 ± 0%     ~     (zero variance)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_true_number_of_iterators_=_64-12                               635 ± 0%       635 ± 0%     ~     (zero variance)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_1-12                              5.00 ± 0%      5.00 ± 0%     ~     (zero variance)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_2-12                              15.0 ± 0%      15.0 ± 0%     ~     (zero variance)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_4-12                              35.0 ± 0%      35.0 ± 0%     ~     (zero variance)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_8-12                              75.0 ± 0%      75.0 ± 0%     ~     (zero variance)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_16-12                              155 ± 0%       155 ± 0%     ~     (zero variance)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_32-12                              315 ± 0%       315 ± 0%     ~     (zero variance)
MergedSeriesChunkRefsSetIterators/with_duplicated_series_=_false_number_of_iterators_=_64-12                              636 ± 0%       636 ± 0%     ~     (zero variance)

Which issue(s) this PR fixes or relates to

Fixes #

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

Copy link
Contributor

@dimitarvdimitrov dimitarvdimitrov left a comment

Choose a reason for hiding this comment

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

pooling LGTM, i think it's missing one .reset

copying my comment from a private convo

I have a concern around forgetting to release a set. Right now whoever uses newSeriesChunkRefsSet needs to be aware of who is going to later release this set. It’s difficult to catch when we don’t release

i have two ideas but you’ll tell me if they are not overengineering or worrying too much. maybe easier to explain on a call. my ideas:

  • use the iterator approach to release seriesChunkRefsSet back into the pool. We know which iterators are supposed to use newSeriesChunkRefsSet . Put an iterator before them, which does seriesChunkRefsSet.reset on the previous set every time it receives a Next call (every time after the first). This way we keep the logic of when to release something and when to allocate one slightly less coupled. This should make it easier to reason when something should be freed.
  • have a per-request pool of these batches that we release after the request. Very similar to how pool.BatchBytes works. And instead of calling newSeriesChunkRefsSet directly, you use this pool to get a new set. This way we make sure we don’t leak from the pool between requests. It’s also harder to make a new allocation, because you need the pool, can’t just call a local function.

pkg/storegateway/series_refs.go Show resolved Hide resolved
pkg/storegateway/series_refs.go Show resolved Hide resolved
pkg/storegateway/series_refs_test.go Outdated Show resolved Hide resolved
Base automatically changed from dimitar/store-gateway-async-series to main December 7, 2022 16:54
@pracucci pracucci force-pushed the reduce-newSeriesChunkRefsSet-allocations branch 2 times, most recently from 0127282 to a4f3f81 Compare December 12, 2022 15:26
Fixed the optimization for the case series are not duplicated among sets
Added TestMergedSeriesChunkRefsSet_Concurrency

Signed-off-by: Marco Pracucci <marco@pracucci.com>
@pracucci pracucci force-pushed the reduce-newSeriesChunkRefsSet-allocations branch from a4f3f81 to a5ec2c2 Compare December 12, 2022 15:37
Signed-off-by: Marco Pracucci <marco@pracucci.com>
Signed-off-by: Marco Pracucci <marco@pracucci.com>
Signed-off-by: Marco Pracucci <marco@pracucci.com>
@pracucci pracucci marked this pull request as ready for review December 12, 2022 18:21
@pracucci pracucci requested a review from a team as a code owner December 12, 2022 18:21
@pracucci
Copy link
Collaborator Author

I have a concern around forgetting to release a set. Right now whoever uses newSeriesChunkRefsSet needs to be aware of who is going to later release this set. It’s difficult to catch when we don’t release

I agree it's hard, but I've introduced some assertions in the new tests to detect it.

use the iterator approach to release seriesChunkRefsSet back into the pool. We know which iterators are supposed to use newSeriesChunkRefsSet

I tried this approach, but ended up not being useable in every place. Try to take another look at the final version of the code. I personally think it's easier to follow than if the release is done through a wrapping iterator and the release() at the end is called just in few places.

Signed-off-by: Marco Pracucci <marco@pracucci.com>
Signed-off-by: Marco Pracucci <marco@pracucci.com>
// release the internal series slice to a memory pool. This function call has no effect
// if seriesChunkRefsSet was created to be not releasable.
//
// This function is not idempotent. Calling it twice would introduce subtle bugs.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note to reviewers: since seriesChunkRefsSet is passed around as a copy, I can't set b.series = nil to make this function idempotent. I will try to pass this around as a pointer in a follow up PR, to see what's the impact on performance and code changes, and then we can take an informed decision.

Copy link
Contributor

@dimitarvdimitrov dimitarvdimitrov left a comment

Choose a reason for hiding this comment

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

LGTM, thank you for fixing this! Looks pretty cool. I have only a few minor comments

pkg/util/pool/pool.go Outdated Show resolved Hide resolved
pkg/storegateway/series_refs_test.go Outdated Show resolved Hide resolved
pkg/storegateway/series_refs.go Show resolved Hide resolved
pkg/storegateway/series_refs_test.go Show resolved Hide resolved
Signed-off-by: Marco Pracucci <marco@pracucci.com>
Copy link
Contributor

@dimitarvdimitrov dimitarvdimitrov left a comment

Choose a reason for hiding this comment

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

LGTM, thank you for you patience

@pracucci pracucci enabled auto-merge (squash) December 13, 2022 17:33
@pracucci pracucci merged commit 8c7970c into main Dec 13, 2022
@pracucci pracucci deleted the reduce-newSeriesChunkRefsSet-allocations branch December 13, 2022 17:40
masonmei pushed a commit to udmire/mimir that referenced this pull request Dec 16, 2022
* Reduce newSeriesChunkRefsSet() introducing a memory pool
Fixed the optimization for the case series are not duplicated among sets
Added TestMergedSeriesChunkRefsSet_Concurrency

Signed-off-by: Marco Pracucci <marco@pracucci.com>

* Added TestBucket_Series_Concurrency

Signed-off-by: Marco Pracucci <marco@pracucci.com>

* Added assertions on seriesChunkRefsSetPool balance

Signed-off-by: Marco Pracucci <marco@pracucci.com>

* Fixed BenchmarkBucket_Series_WithSkipChunks

Signed-off-by: Marco Pracucci <marco@pracucci.com>

* Removed TODO

Signed-off-by: Marco Pracucci <marco@pracucci.com>

* Delete spurious file

Signed-off-by: Marco Pracucci <marco@pracucci.com>

* Address review feedback

Signed-off-by: Marco Pracucci <marco@pracucci.com>

Signed-off-by: Marco Pracucci <marco@pracucci.com>
Co-authored-by: Dimitar Dimitrov <dimitar.dimitrov@grafana.com>
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.

None yet

2 participants