mirror of
https://github.com/apple/foundationdb.git
synced 2025-06-01 18:56:00 +08:00
Merge pull request #3024 from atn34/atn34/reserve
Avoid some unnecessary copies in generic actors
This commit is contained in:
commit
cd95c8e191
@ -69,6 +69,8 @@ ACTOR Future<Void> timeoutWarningCollector( FutureStream<Void> input, double log
|
||||
ACTOR Future<bool> quorumEqualsTrue( std::vector<Future<bool>> futures, int required ) {
|
||||
state std::vector< Future<Void> > true_futures;
|
||||
state std::vector< Future<Void> > false_futures;
|
||||
true_futures.reserve(futures.size());
|
||||
false_futures.reserve(futures.size());
|
||||
for(int i=0; i<futures.size(); i++) {
|
||||
true_futures.push_back( onEqual( futures[i], true ) );
|
||||
false_futures.push_back( onEqual( futures[i], false ) );
|
||||
@ -87,6 +89,7 @@ ACTOR Future<bool> quorumEqualsTrue( std::vector<Future<bool>> futures, int requ
|
||||
ACTOR Future<bool> shortCircuitAny( std::vector<Future<bool>> f )
|
||||
{
|
||||
std::vector<Future<Void>> sc;
|
||||
sc.reserve(f.size());
|
||||
for(Future<bool> fut : f) {
|
||||
sc.push_back(returnIfTrue(fut));
|
||||
}
|
||||
@ -96,7 +99,7 @@ ACTOR Future<bool> shortCircuitAny( std::vector<Future<bool>> f )
|
||||
// Handle a possible race condition? If the _last_ term to
|
||||
// be evaluated triggers the waitForAll before bubbling
|
||||
// out of the returnIfTrue quorum
|
||||
for ( auto fut : f ) {
|
||||
for (const auto& fut : f) {
|
||||
if ( fut.get() ) {
|
||||
return true;
|
||||
}
|
||||
|
@ -312,8 +312,8 @@ template<class T, class F>
|
||||
std::vector<Future<std::invoke_result_t<F, T>>> mapAsync(std::vector<Future<T>> const& what, F const& actorFunc)
|
||||
{
|
||||
std::vector<std::invoke_result_t<F, T>> ret;
|
||||
for(auto f : what)
|
||||
ret.push_back(mapAsync( f, actorFunc ));
|
||||
ret.reserve(what.size());
|
||||
for (const auto& f : what) ret.push_back(mapAsync(f, actorFunc));
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -371,8 +371,8 @@ template<class T, class F>
|
||||
std::vector<Future<std::invoke_result_t<F, T>>> map(std::vector<Future<T>> const& what, F const& func)
|
||||
{
|
||||
std::vector<Future<std::invoke_result_t<F, T>>> ret;
|
||||
for(auto f : what)
|
||||
ret.push_back(map( f, func ));
|
||||
ret.reserve(what.size());
|
||||
for (const auto& f : what) ret.push_back(map(f, func));
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -585,6 +585,7 @@ public:
|
||||
}
|
||||
std::vector<K> getKeys() {
|
||||
std::vector<K> keys;
|
||||
keys.reserve(items.size());
|
||||
for(auto i = items.begin(); i != items.end(); ++i)
|
||||
keys.push_back( i->first );
|
||||
return keys;
|
||||
@ -887,6 +888,7 @@ Future<Void> streamHelper( PromiseStream<T> output, PromiseStream<Error> errors,
|
||||
template <class T>
|
||||
Future<Void> makeStream( const std::vector<Future<T>>& futures, PromiseStream<T>& stream, PromiseStream<Error>& errors ) {
|
||||
std::vector<Future<Void>> forwarders;
|
||||
forwarders.reserve(futures.size());
|
||||
for(int f=0; f<futures.size(); f++)
|
||||
forwarders.push_back( streamHelper( stream, errors, futures[f] ) );
|
||||
return cancelOnly(forwarders);
|
||||
@ -1016,6 +1018,7 @@ Future<std::vector<T>> getAll( std::vector<Future<T>> input ) {
|
||||
wait( quorum( input, input.size() ) );
|
||||
|
||||
std::vector<T> output;
|
||||
output.reserve(input.size());
|
||||
for(int i=0; i<input.size(); i++)
|
||||
output.push_back( input[i].get() );
|
||||
return output;
|
||||
@ -1026,6 +1029,12 @@ Future<std::vector<T>> appendAll( std::vector<Future<std::vector<T>>> input ) {
|
||||
wait( quorum( input, input.size() ) );
|
||||
|
||||
std::vector<T> output;
|
||||
size_t sz = 0;
|
||||
for (const auto& f : input) {
|
||||
sz += f.get().size();
|
||||
}
|
||||
output.reserve(sz);
|
||||
|
||||
for(int i=0; i<input.size(); i++) {
|
||||
auto const& r = input[i].get();
|
||||
output.insert( output.end(), r.begin(), r.end() );
|
||||
@ -1167,10 +1176,7 @@ inline Future<Void> operator &&( Future<Void> const& lhs, Future<Void> const& rh
|
||||
else return lhs;
|
||||
}
|
||||
|
||||
std::vector<Future<Void>> v;
|
||||
v.push_back( lhs );
|
||||
v.push_back( rhs );
|
||||
return waitForAll(v);
|
||||
return waitForAll(std::vector<Future<Void>>{ lhs, rhs });
|
||||
}
|
||||
|
||||
// error || unset -> error
|
||||
@ -1626,8 +1632,7 @@ public:
|
||||
return futures[0];
|
||||
|
||||
Future<Void> f = waitForAll(futures);
|
||||
futures = std::vector<Future<Void>>();
|
||||
futures.push_back(f);
|
||||
futures = std::vector<Future<Void>>{ f };
|
||||
return f;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user