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

improvement: Use go1.21's slices package to avoid allocation in sorting #323

Merged
merged 1 commit into from
Sep 8, 2023

Conversation

bmoylan
Copy link
Contributor

@bmoylan bmoylan commented Sep 8, 2023

pkg: github.com/palantir/pkg/metrics
                                                 │   old.txt    │               new.txt               │
                                                 │    sec/op    │   sec/op     vs base                │
RegisterMetric/1_tag-10                            119.80n ± 2%   94.67n ± 1%  -20.98% (p=0.000 n=10)
RegisterMetric/10_tag-10                            278.8n ± 0%   266.6n ± 1%   -4.36% (p=0.000 n=10)
RegisterMetric/100_tag-10                           5.065µ ± 1%   6.884µ ± 0%  +35.93% (p=0.000 n=10)
Histogram/HistogramWithSample_with_cached_Tag-10    283.9n ± 1%   261.8n ± 0%   -7.78% (p=0.000 n=10)
Histogram/Histogram_with_cached_Tag-10              269.3n ± 1%   242.7n ± 0%   -9.88% (p=0.000 n=10)
Histogram/HistogramWithSample_with_NewTag-10        394.9n ± 1%   376.5n ± 1%   -4.67% (p=0.000 n=10)
Histogram/Histogram_with_NewTag-10                  379.4n ± 0%   352.0n ± 0%   -7.22% (p=0.000 n=10)
Histogram/cached_Histogram-10                       129.4n ± 0%   126.3n ± 0%   -2.40% (p=0.000 n=10)
geomean                                             354.7n        341.5n        -3.74%

                                                 │    old.txt     │                new.txt                 │
                                                 │      B/op      │     B/op      vs base                  │
RegisterMetric/1_tag-10                              96.00 ± 0%       72.00 ± 0%  -25.00% (p=0.000 n=10)
RegisterMetric/10_tag-10                             616.0 ± 0%       592.0 ± 0%   -3.90% (p=0.000 n=10)
RegisterMetric/100_tag-10                          6.023Ki ± 0%     6.000Ki ± 0%   -0.39% (p=0.000 n=10)
Histogram/HistogramWithSample_with_cached_Tag-10     208.0 ± 0%       184.0 ± 0%  -11.54% (p=0.000 n=10)
Histogram/Histogram_with_cached_Tag-10               184.0 ± 0%       160.0 ± 0%  -13.04% (p=0.000 n=10)
Histogram/HistogramWithSample_with_NewTag-10         229.0 ± 0%       205.0 ± 0%  -10.48% (p=0.000 n=10)
Histogram/Histogram_with_NewTag-10                   205.0 ± 0%       181.0 ± 0%  -11.71% (p=0.000 n=10)
Histogram/cached_Histogram-10                        0.000 ± 0%       0.000 ± 0%        ~ (p=1.000 n=10) ¹
geomean                                                         ²                  -9.84%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                                                 │   old.txt    │               new.txt                │
                                                 │  allocs/op   │ allocs/op   vs base                  │
RegisterMetric/1_tag-10                            3.000 ± 0%     2.000 ± 0%  -33.33% (p=0.000 n=10)
RegisterMetric/10_tag-10                           3.000 ± 0%     2.000 ± 0%  -33.33% (p=0.000 n=10)
RegisterMetric/100_tag-10                          3.000 ± 0%     2.000 ± 0%  -33.33% (p=0.000 n=10)
Histogram/HistogramWithSample_with_cached_Tag-10   5.000 ± 0%     4.000 ± 0%  -20.00% (p=0.000 n=10)
Histogram/Histogram_with_cached_Tag-10             4.000 ± 0%     3.000 ± 0%  -25.00% (p=0.000 n=10)
Histogram/HistogramWithSample_with_NewTag-10       8.000 ± 0%     7.000 ± 0%  -12.50% (p=0.000 n=10)
Histogram/Histogram_with_NewTag-10                 7.000 ± 0%     6.000 ± 0%  -14.29% (p=0.000 n=10)
Histogram/cached_Histogram-10                      0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
geomean                                                       ²               -22.26%                ²

This change is Reviewable

// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !go1.21
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can remove this after go1.22 releases and test-go-prev supports go1.21


// sortStrings is the default sort.Strings function.
// Unfortunately this forces the slice to escape to the heap.
// See https://github.com/golang/go/issues/17332
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 didn't feel like publicly linking to the github issue, but this describes the long-time problem.

@bmoylan bmoylan merged commit f9e2fb6 into master Sep 8, 2023
@bmoylan bmoylan deleted the bm/go-slices branch September 8, 2023 02:29
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