From 8faf1f951011f974b62f9226a725e2af59dd9a15 Mon Sep 17 00:00:00 2001 From: naoto Date: Thu, 27 Aug 2026 22:18:03 +0900 Subject: [PATCH] Avoid copying MetricFamily in Family::Collect 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 #741. --- core/src/family.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/src/family.cc b/core/src/family.cc index 6a994516..d8ac2921 100644 --- a/core/src/family.cc +++ b/core/src/family.cc @@ -86,8 +86,9 @@ template std::vector Family::Collect() const { std::lock_guard lock{mutex_}; + auto families = std::vector{}; if (metrics_.empty()) { - return {}; + return families; } auto family = MetricFamily{}; @@ -98,7 +99,8 @@ std::vector Family::Collect() const { for (const auto& [metric_labels, metric] : metrics_) { family.metric.push_back(CollectMetric(metric_labels, metric.get())); } - return {family}; + families.push_back(std::move(family)); + return families; } template