Skip to content

model: hash sorted names in place in LabelSet.FingerprintForLabels - #1053

Merged
bwplotka merged 5 commits into
prometheus:mainfrom
ultrotter:labelset-fingerprint-sorted-names
Nov 25, 2026
Merged

bwplotka merged 5 commits into
prometheus:mainfrom
ultrotter:labelset-fingerprint-sorted-names

Conversation

@ultrotter

Copy link
Copy Markdown
Contributor

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%        ~

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>
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 bwplotka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Some comments

Comment thread model/signature.go
if labelName == names[i-1] {
continue // Repeated name.
}
return unsortedLabelsToFingerprint(ls, names)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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].

Comment thread model/signature_test.go Resolved
Comment thread model/signature_test.go Resolved
Comment thread model/signature_test.go Resolved
Guido Trotter 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>

@bwplotka bwplotka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thx!

Signed-off-by: Guido Trotter <guido@hudson-trading.com>
@bwplotka
bwplotka merged commit 12f58d9 into prometheus:main Nov 25, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants