perf: avoid copying MetricFamily in Family::Collect - #758
Merged
Conversation
Contributor
Author
|
Could you please review it? The Bazel CI failure is unrelated to this change — it fails while computing the main repo mapping, before anything is built. |
Collect() built the family in a local variable and returned it as
`return {family};`. A braced-init-list copies its elements, so every
collection deep-copied the whole metric vector, including each
ClientMetric and all of its labels, and temporarily required twice the
memory of the collected family.
Move the family into the returned vector instead. Registry::Collect()
already moves the families out, so this removes the last copy on the
collection path.
Relates to jupp0r#741.
gjasny
force-pushed
the
avoid-metric-family-copy
branch
from
August 29, 2026 17:35
fa2f77c to
8faf1f9
Compare
Collaborator
|
Thanks for fixing! |
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.
perf: avoid copying MetricFamily in Family::Collect
Summary
Family<T>::Collect()built the family in a local variable and returned it viareturn {family};. A braced-init-list copies its elements, so this deep-copiedthe whole
metricvector (everyClientMetricand all of its labels) on everyscrape, temporarily requiring twice the memory of the collected family.
The family is now constructed in place inside the returned vector, so no copy is
made. The redundant
std::move()around the prvalue returned byCollectMetric()is dropped as well; a prvalue already binds tostd::vector::push_back(T&&).Changes
core/src/family.cc: build the returnedstd::vector<MetricFamily>in placeinstead of copy-constructing it from a braced-init-list.
core/src/family.cc: remove the redundantstd::move()in the collection loop.Behavior
No functional change. The returned data is identical; only the number of
allocations and copies performed during a collection is reduced.
Registry::Collect()already moves the families out ofFamily<T>::Collect()via
std::make_move_iterator, so this removes the last copy on that path.Relates to #741
In #741 a
std::bad_allocis thrown from theMetricFamilycopy constructorwhile collecting metrics, which is exactly the copy removed here. Cutting the
peak memory required for a collection makes that failure less likely, but this
change does not add exception handling, so it does not fully implement the
request in #741 to keep the process alive when an allocation fails.