From 92d510df9827c34f0384cd289c957a6983ce8f4a Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Wed, 3 Jan 2018 13:41:22 -0800 Subject: [PATCH] Avoid allocs in dtors in futures collect Summary: [Folly] Avoid allocs in dtors in futures `collect`. `CollectContext`, a detail helper type, allocates storage for a results vector in its dtor. This is an awkward situation and should be avoided. Reviewed By: ericniebler Differential Revision: D6649299 fbshipit-source-id: 87746fcc78fa080f63505d7bb864aca6c4a3d7cb --- folly/futures/Future-inl.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/folly/futures/Future-inl.h b/folly/futures/Future-inl.h index 56b98410..cb94e037 100644 --- a/folly/futures/Future-inl.h +++ b/folly/futures/Future-inl.h @@ -953,12 +953,12 @@ struct CollectContext { Nothing, std::vector>>::type; - explicit CollectContext(size_t n) : result(n) {} + explicit CollectContext(size_t n) : result(n) { + finalResult.reserve(n); + } ~CollectContext() { if (!threw.exchange(true)) { // map Optional -> T - std::vector finalResult; - finalResult.reserve(result.size()); std::transform(result.begin(), result.end(), std::back_inserter(finalResult), [](Optional& o) { return std::move(o.value()); }); @@ -970,6 +970,7 @@ struct CollectContext { } Promise p; InternalResult result; + Result finalResult; std::atomic threw {false}; }; -- 2.34.1