From: Yedidya Feldblum Date: Wed, 3 Jan 2018 21:41:22 +0000 (-0800) Subject: Avoid allocs in dtors in futures collect X-Git-Tag: v2018.01.08.00~16 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=92d510df9827c34f0384cd289c957a6983ce8f4a;p=folly.git 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 --- 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}; };