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

perf(blooms): Avoid tiny string allocations for insert cache #13487

Merged
merged 1 commit into from
Jul 11, 2024

Conversation

chaudum
Copy link
Contributor

@chaudum chaudum commented Jul 11, 2024

What this PR does / why we need it:

Given that we see really high insert cache hit rate, we can void the string allocations for the cache ownership check.

screenshot_20240711_135526

sum(rate(loki_bloom_inserts_total{collision="cache"}[$__rate_interval]))
/
sum(rate(loki_bloom_inserts_total{}[$__rate_interval]))

Special notes for your reviewer:

Running benchmark with command:

$ gotest -v -count=10 ./pkg/storage/bloom/v1/... -test.run=^$ -test.bench=BenchmarkPopulateSeriesWithBloom

Results:

$ benchstat bench-old.txt bench-new.txt 
bench-old.txt:5: missing iteration count
bench-new.txt:5: missing iteration count
goos: linux
goarch: amd64
pkg: github.com/grafana/loki/v3/pkg/storage/bloom/v1
cpu: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
                          │ bench-old.txt │           bench-new.txt            │
                          │    sec/op     │   sec/op     vs base               │
PopulateSeriesWithBloom-8   1038.9µ ± 29%   973.7µ ± 2%  -6.28% (p=0.000 n=10)

                          │ bench-old.txt │            bench-new.txt            │
                          │     B/op      │     B/op      vs base               │
PopulateSeriesWithBloom-8    9.112Mi ± 0%   9.054Mi ± 0%  -0.64% (p=0.000 n=10)

                          │ bench-old.txt │           bench-new.txt            │
                          │   allocs/op   │ allocs/op   vs base                │
PopulateSeriesWithBloom-8     5269.0 ± 0%   877.0 ± 0%  -83.36% (p=0.000 n=10)

Checklist

  • Reviewed the CONTRIBUTING.md guide (required)
  • Documentation added
  • Tests updated
  • Title matches the required conventional commits format, see here
    • Note that Promtail is considered to be feature complete, and future development for logs collection will be in Grafana Alloy. As such, feat PRs are unlikely to be accepted unless a case can be made for the feature actually being a bug fix to existing behavior.
  • Changes that require user attention or interaction to upgrade are documented in docs/sources/setup/upgrade/_index.md
  • For Helm chart changes bump the Helm chart version in production/helm/loki/Chart.yaml and update production/helm/loki/CHANGELOG.md and production/helm/loki/README.md. Example PR
  • If the change is deprecating or removing a configuration option, update the deprecated-config.yaml and deleted-config.yaml files respectively in the tools/deprecated-config-checker directory. Example PR

Given that we see really high insert cache hit rate, we can void the
string allocations for the cache ownership check.

Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
@chaudum chaudum marked this pull request as ready for review July 11, 2024 12:16
@chaudum chaudum requested a review from a team as a code owner July 11, 2024 12:16
@chaudum
Copy link
Contributor Author

chaudum commented Jul 11, 2024

@owen-d Can you take a look as well? Feel free to merge it thereafter.

_, found := bt.cache[str] // A cache is used ahead of the SBF, as it cuts out the costly operations of scaling bloom filters
if found {
// To avoid allocations, an unsafe string can be used to check ownership in cache.
str := unsafe.String(unsafe.SliceData(tok), len(tok))
Copy link
Member

Choose a reason for hiding this comment

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

note: The []byte is reused across Next calls. I wonder if this impacts us here.

// The Token iterator uses shared buffers for performance. The []byte returned by At()
// is not safe for use after subsequent calls to Next()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The unsafe string is only used for the membership check, not the assignment in the map.

Copy link
Member

Choose a reason for hiding this comment

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

Oh good point. LGTM

_, found := bt.cache[str] // A cache is used ahead of the SBF, as it cuts out the costly operations of scaling bloom filters
if found {
// To avoid allocations, an unsafe string can be used to check ownership in cache.
str := unsafe.String(unsafe.SliceData(tok), len(tok))
Copy link
Member

Choose a reason for hiding this comment

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

Oh good point. LGTM

@owen-d owen-d merged commit 652ad24 into main Jul 11, 2024
61 checks passed
@owen-d owen-d deleted the chaudum/avoid-tiny-allocs-for-bloom-insert-cache branch July 11, 2024 23:19
grobinson-grafana pushed a commit that referenced this pull request Jul 12, 2024
Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants