Skip to content

fix: avoid memory allocations when loading symbolizer from a flushed chunk - #17953

Merged
sandeepsukhani merged 2 commits into
mainfrom
sm-symbolizer-improvements
Jun 4, 2025
Merged

fix: avoid memory allocations when loading symbolizer from a flushed chunk#17953
sandeepsukhani merged 2 commits into
mainfrom
sm-symbolizer-improvements

Conversation

@sandeepsukhani

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:
When loading a symbolizer from a flushed chunk, we unnecessarily allocate a map that holds entries only to dedupe the data while writing to a chunk. This PR fixes the issue by removing those unwanted allocations and rejecting any writes when the symbolizer is loaded from a flushed chunk.

Checklist

  • Tests updated
@sandeepsukhani
sandeepsukhani requested a review from a team as a code owner June 3, 2025 07:37
Comment thread pkg/chunkenc/symbols.go
Comment on lines -53 to -63
// Reset resets all the data and makes the symbolizer ready for reuse
func (s *symbolizer) Reset() {
s.mtx.Lock()
defer s.mtx.Unlock()

s.symbolsMap = map[string]uint32{}
s.labels = s.labels[:0]
s.size = 0
s.compressedSize = 0
s.normalizedNames = map[uint32]string{}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

surprised to see this removed? do we not use it?

Fine with removing it just wanted to double check

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the symbolizer is created per chunk, and since we do not reuse chunk references, we do not reuse symbolizer either. We will have to use a pool to reuse symbolizer, but we will have to see how much it would help since the volume of structured metadata won't be balanced.

Comment thread pkg/chunkenc/unordered.go Outdated
}
e.entries = append(displaced[0].(*nsEntries).entries, nsEntry{line, hb.symbolizer.Add(structuredMetadata)})
var symbols symbols
if !structuredMetadata.IsEmpty() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

not sure I understand this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It was to fix some broken tests after my changes. However, it seems I can remove that check since the symbolizer is never nil in non-test code. I have fixed the broken tests to pass a non-nil symbolizer.

@slim-bean slim-bean left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM but I did have a couple questions though they don't have to block merging.

Also it would be nice to add a few comments that the symbolsMap is only necessary when the chunk is being written to, maybe explain what it's purpose is, this is what confused me when I made the last optimization.

@sandeepsukhani
sandeepsukhani enabled auto-merge (squash) June 4, 2025 06:03
@sandeepsukhani
sandeepsukhani merged commit 97ecc47 into main Jun 4, 2025
@sandeepsukhani
sandeepsukhani deleted the sm-symbolizer-improvements branch June 4, 2025 06:08
@sandeepsukhani sandeepsukhani added type/bug Somehing is not working as expected backport k257 labels Jun 4, 2025
loki-gh-app Bot pushed a commit that referenced this pull request Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport k257 size/L type/bug Somehing is not working as expected

2 participants