Skip to content

Commit fa2f77c

Browse files
committed
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.
1 parent e0cbaed commit fa2f77c

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

‎core/src/family.cc‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,21 @@ template <typename T>
8989
std::vector<MetricFamily> Family<T>::Collect() const {
9090
std::lock_guard<std::mutex> lock{mutex_};
9191

92+
std::vector<MetricFamily> families;
9293
if (metrics_.empty()) {
93-
return {};
94+
return families;
9495
}
9596

96-
auto family = MetricFamily{};
97+
MetricFamily family;
9798
family.name = name_;
9899
family.help = help_;
99100
family.type = T::metric_type;
100101
family.metric.reserve(metrics_.size());
101102
for (const auto& m : metrics_) {
102103
family.metric.push_back(CollectMetric(m.first, m.second.get()));
103104
}
104-
return {family};
105+
families.push_back(std::move(family));
106+
return families;
105107
}
106108

107109
template <typename T>

0 commit comments

Comments
 (0)