Refactor target label handling in target allocator for improved performance #4587
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
Refactor target label handling in target allocator. The intent is to improve performance on both main and in #4547, which otherwise leads to major regressions.
This PR makes two major changes:
The
target.Itemstruct no longer holds labels after relabelingInstead, when relabelling, we calculate the hash directly from the label builder and pass it to the
target.Itemconstructor. This way, we can avoid materializing the new labels and allocating memory for them. Processing labels using a builder rather than the raw labels data structure also makes this implementation independent of the underlying implementation of labels.Labels, which changes in recent Prometheus releases.As a minor optimization, we also reuse xxhash instances by storing them in a
sync.Pool.We initially construct labels using a
labels.ScratchBuilderThis is a straightforward optimization focused on minimizing memory allocations. I've left detailed comments in the code explaining the specific decisions.
Testing:
Benchstat comparing the results from this branch to main: