Commit 92d510df authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

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
parent eba5e7f2
...@@ -953,12 +953,12 @@ struct CollectContext { ...@@ -953,12 +953,12 @@ struct CollectContext {
Nothing, Nothing,
std::vector<Optional<T>>>::type; std::vector<Optional<T>>>::type;
explicit CollectContext(size_t n) : result(n) {} explicit CollectContext(size_t n) : result(n) {
finalResult.reserve(n);
}
~CollectContext() { ~CollectContext() {
if (!threw.exchange(true)) { if (!threw.exchange(true)) {
// map Optional<T> -> T // map Optional<T> -> T
std::vector<T> finalResult;
finalResult.reserve(result.size());
std::transform(result.begin(), result.end(), std::transform(result.begin(), result.end(),
std::back_inserter(finalResult), std::back_inserter(finalResult),
[](Optional<T>& o) { return std::move(o.value()); }); [](Optional<T>& o) { return std::move(o.value()); });
...@@ -970,6 +970,7 @@ struct CollectContext { ...@@ -970,6 +970,7 @@ struct CollectContext {
} }
Promise<Result> p; Promise<Result> p;
InternalResult result; InternalResult result;
Result finalResult;
std::atomic<bool> threw {false}; std::atomic<bool> threw {false};
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment