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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pkg/storage/bloom/v1/bloom_tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1

import (
"math"
"unsafe"

"github.com/go-kit/log/level"

Expand Down Expand Up @@ -216,10 +217,12 @@ outer:
for itr.Next() {
tok := itr.At()
tokens++

// TODO[owen-d]: [n]byte this
str := string(tok)
_, 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

// A cache is used ahead of the SBF, as it cuts out the costly operations of scaling bloom filters
if _, found := bt.cache[str]; found {
cachedInserts++
continue
}
Expand All @@ -246,6 +249,7 @@ outer:

// only register the key in the cache if it was successfully added to the bloom
// as can prevent us from trying subsequent copies
str = string(tok)
bt.cache[str] = nil
if len(bt.cache) >= cacheSize { // While crude, this has proven efficient in performance testing. This speaks to the similarity in log lines near each other
clear(bt.cache)
Expand Down
Loading