Skip to content

model: add LabelSet.FingerprintForLabels - #1049

Merged
bwplotka merged 5 commits into
prometheus:mainfrom
ultrotter:labelset-subset-fingerprint
Nov 24, 2026
Merged

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

Conversation

@ultrotter

@ultrotter ultrotter commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

LabelSet.FingerprintForLabels fingerprints just a subset of the labels in a set, which makes it possible to get the fingerprint without allocating a new labelset for the subset.

Also add a stack buffer for the sorting, which avoids allocations on Fingerprint for sets up to 32 labels, which should cover a majority of alerts and pays off even on small label sets.

benchstat n=10:

                               base          new
MetricToFingerprintTriple     841.3n ± 3%   781.9n ± 4%   -7.07% (p=0.000)
MetricToFingerprintTwenty     1.994µ ± 4%   1.767µ ± 3%  -11.38% (p=0.000)
LabelSetFingerprintForLabels                99.22n ± 3%   0 B/op  0 allocs/op

@ultrotter
ultrotter force-pushed the labelset-subset-fingerprint branch from f975b8b to 24eb923 Compare November 20, 2026 11:58
LabelSet.FingerprintForLabels fingerprints just a subset of the labels
in a set, which makes it possible to get the fingerprint without
allocating a new labelset for the subset.

Also add a stack buffer for the sorting, which avoids allocations on
Fingerprint for sets up to 32 labels, which should cover a majority of
alerts and pays off even on small label sets.

benchstat n=10:

                               base          new
MetricToFingerprintTriple     837.2n ± 5%   780.8n ± 2%   -6.74% (p=0.000)
MetricToFingerprintTwenty     1.984µ ± 2%   1.757µ ± 3%  -11.42% (p=0.000)
LabelSetFingerprintForLabels                98.60n ± 2%   0 B/op  0 allocs/op

Signed-off-by: Guido Trotter <guido@hudson-trading.com>
@ultrotter
ultrotter force-pushed the labelset-subset-fingerprint branch from 24eb923 to 99de756 Compare November 20, 2026 13:55

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

Thanks

Comment thread model/labelset.go Resolved
Comment thread model/signature.go Resolved
Comment thread model/signature.go Outdated
// names only make the result smaller.
n := len(ls)
if names != nil {
n = len(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.

When names has more than 32 entries but ls is small or few names match, this allocates make([]LabelName, 0, len(names)) on the heap unnecessarily. Starting with buf[:0] whenever names != nil and only preallocating for names == nil && len(ls) > fingerprintNamesBuffer keeps small matches allocation-free, and note the comment above is slightly off since repeated present names are still appended before post-sort deduplication.

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.

Starting with buf[:0] whenever names != nil and not having the make(...) call means that if there were to be many names we could incur in a reallocation+copy, which is more expensive than the make-once option. I guess it's rare enough, but I'm not sure it would be common either to have more than 32 names, or indeed to have duplications in them.

I can fix the comment though, for now, and then we can decide what to do.

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.

I fixed the comment and now size the buffer by the smaller of len(ls) and len(names), which keeps many names against a small set allocation-free. The only way to still trigger a grow-and-copy is more than 32 names, AND more names than labels, AND enough duplicates of present names to push the count past the buffer. We promise that works, but I don't think it's worth optimizing for, since it's not something we expect on a hot path.

If someone knew they wanted to optimize for those situations they also would have the option of building a new LabelSet and then calling Fingerprint on that, but again I don't expect that to be common, and the speed is about twice for all other cases.

Comment thread model/signature_test.go Resolved
Comment thread model/signature_test.go Resolved
Comment thread model/signature_test.go Resolved
Comment thread model/signature_test.go Resolved
Size the names buffer by the smaller of the label set and the requested
names, so that many names against a small set stay allocation-free while
large matches still allocate once. Document that names is not modified
and may be unsorted or repeated.

Tests: compare against LabelsToSignature as an independent reference,
assert names is left untouched, exercise sort and dedup on the heap
path, cover an empty label value, and pin allocations at the stack
buffer boundary. Drop the per-iteration require from the fingerprint
benchmark helper, which added allocations and time of its own.

benchstat n=10, main (0675c9e) with the new helper vs this branch:

                               main          this
MetricToFingerprintScalar     3.278n ± 2%   2.172n ± 0%  -33.73% (p=0.000)
MetricToFingerprintSingle     77.77n ± 3%   83.08n ± 1%   +6.83% (p=0.000)
MetricToFingerprintDouble     123.3n ± 3%   130.6n ± 0%   +5.88% (p=0.001)
MetricToFingerprintTriple     203.3n ± 1%   179.9n ± 1%  -11.53% (p=0.000)
MetricToFingerprintTwenty     1.300µ ± 2%   1.111µ ± 2%  -14.54% (p=0.000)
LabelSetFingerprintForLabels                101.6n ± 1%   0 B/op  0 allocs/op

Single and Double already ran allocation-free on main, as the compiler
keeps variable-sized makes of up to 32 bytes on the stack, so there they
only pay for zeroing the larger buffer. Triple and above drop their heap
allocation.

Signed-off-by: Guido Trotter <guido@hudson-trading.com>
@ultrotter
ultrotter requested a review from bwplotka November 20, 2026 20:47
…sets

Route sets of up to two names through a plain make, which the compiler
keeps on the stack by itself, and only declare the stack buffer on the
path that uses it, so that neither tiny nor large sets pay for zeroing
its 512 bytes.

benchstat n=10, previous commit vs this:

                               before        after
MetricToFingerprintSingle     83.06n ± 1%   73.60n ± 3%  -11.39% (p=0.000)
MetricToFingerprintDouble     131.5n ± 1%   122.6n ± 2%   -6.77% (p=0.000)
MetricToFingerprintTriple     179.2n ± 2%   179.8n ± 2%        ~ (p=0.402)
MetricToFingerprintTwenty     1.109µ ± 3%   1.131µ ± 4%        ~ (p=0.142)
LabelSetFingerprintForLabels  101.9n ± 2%   95.78n ± 3%   -5.95% (p=0.000)

benchstat n=10, main (0675c9e) with the new benchmark helper vs this:

                               main          this
MetricToFingerprintScalar     3.264n ± 2%   2.452n ± 1%  -24.90% (p=0.000)
MetricToFingerprintSingle     77.66n ± 0%   73.74n ± 2%   -5.04% (p=0.002)
MetricToFingerprintDouble     122.3n ± 0%   123.0n ± 1%        ~ (p=0.254)
MetricToFingerprintTriple     198.5n ± 1%   180.6n ± 1%   -8.99% (p=0.000)
MetricToFingerprintTwenty     1.258µ ± 1%   1.104µ ± 2%  -12.21% (p=0.000)
LabelSetFingerprintForLabels                97.33n ± 3%   0 B/op  0 allocs/op

Single and Double already ran allocation-free on main and are now at or
below their main timings. Triple and above drop their heap allocation.

Signed-off-by: Guido Trotter <guido@hudson-trading.com>
@ultrotter

Copy link
Copy Markdown
Contributor Author

I was able to bring back the "one or two labels" case to the baseline at main, instead of +5ms. The change was very simple: moving the var declaration one level deeper and adding a condition to the if branch. If we don't want it we can stop at commit two, but like this I think we get good gains:

benchstat n=10, previous commit vs this:

                               before        after
MetricToFingerprintSingle     83.06n ± 1%   73.60n ± 3%  -11.39% (p=0.000)
MetricToFingerprintDouble     131.5n ± 1%   122.6n ± 2%   -6.77% (p=0.000)
MetricToFingerprintTriple     179.2n ± 2%   179.8n ± 2%        ~ (p=0.402)
MetricToFingerprintTwenty     1.109µ ± 3%   1.131µ ± 4%        ~ (p=0.142)
LabelSetFingerprintForLabels  101.9n ± 2%   95.78n ± 3%   -5.95% (p=0.000)

benchstat n=10, main (https://github.com/prometheus/common/commit/0675c9eff1ec4c23616e2998b9faf3c85d090224) with the new benchmark helper vs this:

                               main          this
MetricToFingerprintScalar     3.264n ± 2%   2.452n ± 1%  -24.90% (p=0.000)
MetricToFingerprintSingle     77.66n ± 0%   73.74n ± 2%   -5.04% (p=0.002)
MetricToFingerprintDouble     122.3n ± 0%   123.0n ± 1%        ~ (p=0.254)
MetricToFingerprintTriple     198.5n ± 1%   180.6n ± 1%   -8.99% (p=0.000)
MetricToFingerprintTwenty     1.258µ ± 1%   1.104µ ± 2%  -12.21% (p=0.000)
LabelSetFingerprintForLabels                97.33n ± 3%   0 B/op  0 allocs/op

Open question if we can optimize up-to-64 without damaging this, but I may leave this for later discussion in case.

@ultrotter

Copy link
Copy Markdown
Contributor Author

I put a 64-labels version in a separate PR so we can decide.

@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 nits, otherwise LGTM!

Comment thread model/signature.go Resolved
Comment thread model/signature_test.go Resolved
Comment thread model/signature_test.go Resolved
Guido Trotter added 2 commits November 24, 2026 02:46
Use an explicit two-name stack buffer for sets of up to two names instead
of relying on the compiler keeping a small variable-sized make on the
stack. TestFingerprintAllocations now alsonpasses under -gcflags=-N.

Tests: cover repeated present names growing labelNames past the small
and the regular buffer, and make the many-names allocation check match
two labels instead of none, so that it exercises the append, sort and
hash path.

benchstat n=10, previous commit vs this:

                              before        after
MetricToFingerprintSingle    73.91n ± 1%   74.06n ± 1%       ~ (p=0.172)
MetricToFingerprintDouble    122.4n ± 4%   123.0n ± 3%       ~ (p=0.695)
MetricToFingerprintTriple    180.6n ± 6%   178.6n ± 1%  -1.08% (p=0.001)
MetricToFingerprintTwenty    1.115µ ± 3%   1.101µ ± 2%       ~ (p=0.209)
LabelSetFingerprintForLabels 96.12n ± 1%   95.88n ± 1%       ~ (p=0.325)

Signed-off-by: Guido Trotter <guido@hudson-trading.com>
Given we are manually allocating the "small" buffer, I made measures to
find the "best" size, and 8 seems to be winning overall in zeroing-time
vs effect on label sets.

benchstat n=10, previous commit vs this:

                              before        after
MetricToFingerprintSingle    74.24n ± 2%   74.20n ± 1%       ~ (p=0.353)
MetricToFingerprintDouble    122.2n ± 1%   120.8n ± 0%  -1.06% (p=0.000)
MetricToFingerprintTriple    179.5n ± 0%   169.7n ± 1%  -5.51% (p=0.000)
MetricToFingerprintTwenty    1.105µ ± 4%   1.110µ ± 2%       ~ (p=0.956)
LabelSetFingerprintForLabels 95.79n ± 0%   98.15n ± 1%  +2.46% (p=0.000)

A sweep of the small buffer at 2, 4 and 8 names over sets of 1 to 20
labels showed 8 at or ahead of the others at every size, with 4- and
5-label sets 9% and 7% faster than at 2, and no difference from 9 up.

Signed-off-by: Guido Trotter <guido@hudson-trading.com>
@bwplotka
bwplotka merged commit d0223c5 into prometheus:main Nov 24, 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.

3 participants