Repository navigation
model: hash sorted names in place in LabelSet.FingerprintForLabels - #1053
Merged
bwplotka merged 5 commits intoNov 25, 2026
Merged
Conversation
FingerprintForLabels collected the present names into a stack buffer and
sorted them even when the caller passed them sorted, which is what a
caller keeping the names for repeated use does. Hash them straight from
the set instead. The comparison that skips repeated names also spots an
unsorted one and falls back to unsortedLabelsToFingerprint, which is
shared with LabelSet.Fingerprint so that unsorted names still use the
stack buffer and do not allocate. Sorted names avoid collecting,
sorting, and looking up each present label twice in the map, and never
allocate regardless of count.
goos: linux, goarch: amd64, cpu: AMD EPYC, 12-label set:
before after
ForLabels/1_names_sorted 40.41n ± 1% 21.52n ± 0% -46.75%
ForLabels/3_names_sorted 117.25n ± 1% 67.57n ± 0% -42.37%
ForLabels/3_names_unsorted 124.8n ± 1% 149.7n ± 1% +19.95%
ForLabels/8_names_sorted 331.7n ± 5% 187.5n ± 1% -43.47%
ForLabels/8_names_unsorted 455.0n ± 2% 455.2n ± 1% ~
MetricToFingerprintSingle 75.42n ± 1% 74.32n ± 1% -1.45%
MetricToFingerprintTriple 172.6n ± 1% 170.8n ± 1% -1.04%
MetricToFingerprintTwenty 1.117µ ± 1% 1.118µ ± 1% ~
Signed-off-by: Guido Trotter <guido@hudson-trading.com>
Co-Authored-By: Claude Code <noreply@anthropic.com>
7 tasks done
Test skipping a repeated name before another sorted label in TestLabelSetFingerprintForLabels, and include a repeated present name in both the sorted and unsorted TestFingerprintAllocations checks. Signed-off-by: Guido Trotter <guido@hudson-trading.com>
bwplotka
reviewed
Nov 25, 2026
| if labelName == names[i-1] { | ||
| continue // Repeated name. | ||
| } | ||
| return unsortedLabelsToFingerprint(ls, names) |
Member
There was a problem hiding this comment.
Hashing before checking order wastes map lookups and hashes before throwing sum away, which explains the +20% hit on 3 unsorted names and gets much worse when an out-of-order name appears near the end, such as ["alertname", "job", "instance"]. Checking if !slices.IsSorted(names) up front avoids that unsorted regression and lets the loop check just labelName == names[i-1].
added 2 commits
November 25, 2026 04:10
Signed-off-by: Guido Trotter <guido@hudson-trading.com>
Signed-off-by: Guido Trotter <guido@hudson-trading.com>
Signed-off-by: Guido Trotter <guido@hudson-trading.com>
bwplotka
approved these changes
Nov 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
FingerprintForLabels collected the present names into a stack buffer and sorted them even when the caller passed them sorted, which is what a caller keeping the names for repeated use does. Hash them straight from the set instead. The comparison that skips repeated names also spots an unsorted one and falls back to unsortedLabelsToFingerprint, which is shared with LabelSet.Fingerprint so that unsorted names still use the stack buffer and do not allocate. Sorted names avoid collecting, sorting, and looking up each present label twice in the map, and never allocate regardless of count.