From af7afa42b2bf703a9dc9642a4fe8bb350da3bc87 Mon Sep 17 00:00:00 2001 From: Praveen Kumar Date: Fri, 12 Jun 2015 14:44:14 -0700 Subject: [PATCH] Needless temporary gone Summary: We might be doing: 1) Create a temporary 2) Copy/Move out of it 3) Destroy that temporary. Which isn't needed in many places. And copy/move elision doesn't work for a temporary bound to a reference. We can forward arguments, directly. To get the work done three constructors were added. Closes #222 Reviewed By: @JoelMarcey, @yfeldblum Differential Revision: D2151731 Pulled By: @sgolemon --- folly/Subprocess.cpp | 2 +- folly/Subprocess.h | 1 + folly/experimental/JSONSchema.cpp | 9 ++++----- folly/experimental/fibers/SimpleLoopController.h | 2 +- folly/experimental/fibers/test/FibersTest.cpp | 2 +- folly/experimental/test/EventCountTest.cpp | 2 +- folly/futures/Future-inl.h | 2 +- folly/io/async/SSLContext.h | 2 ++ folly/io/async/test/EventBaseTest.cpp | 2 +- folly/test/DeterministicSchedule.cpp | 2 +- folly/test/DynamicTest.cpp | 10 ++++------ folly/wangle/ssl/SSLContextConfig.h | 8 ++++++-- folly/wangle/ssl/SSLSessionCacheManager.cpp | 3 +-- 13 files changed, 25 insertions(+), 22 deletions(-) diff --git a/folly/Subprocess.cpp b/folly/Subprocess.cpp index 04866e1d..7ecc8de4 100644 --- a/folly/Subprocess.cpp +++ b/folly/Subprocess.cpp @@ -819,7 +819,7 @@ void Subprocess::closeParentFd(int childFd) { std::vector Subprocess::takeOwnershipOfPipes() { std::vector pipes; for (auto& p : pipes_) { - pipes.emplace_back(ChildPipe{p.childFd, std::move(p.pipe)}); + pipes.emplace_back(p.childFd, std::move(p.pipe)); } pipes_.clear(); return pipes; diff --git a/folly/Subprocess.h b/folly/Subprocess.h index da2b5602..b1f6707f 100644 --- a/folly/Subprocess.h +++ b/folly/Subprocess.h @@ -714,6 +714,7 @@ class Subprocess { * No, you may NOT call this from a communicate() callback. */ struct ChildPipe { + ChildPipe(int fd, folly::File&& ppe) : childFd(fd), pipe(std::move(ppe)) {} int childFd; folly::File pipe; // Owns the parent FD }; diff --git a/folly/experimental/JSONSchema.cpp b/folly/experimental/JSONSchema.cpp index 5297c28e..51a6d20a 100644 --- a/folly/experimental/JSONSchema.cpp +++ b/folly/experimental/JSONSchema.cpp @@ -381,8 +381,8 @@ struct PropertiesValidator final : IValidator { for (const auto& pair : patternProperties->items()) { if (pair.first.isString()) { patternPropertyValidators_.emplace_back( - make_pair(boost::regex(pair.first.getString().toStdString()), - SchemaValidator::make(context, pair.second))); + boost::regex(pair.first.getString().toStdString()), + SchemaValidator::make(context, pair.second)); } } } @@ -466,9 +466,8 @@ struct DependencyValidator final : IValidator { propertyDep_.emplace_back(std::move(p)); } if (pair.second.isObject()) { - schemaDep_.emplace_back( - make_pair(pair.first.getString(), - SchemaValidator::make(context, pair.second))); + schemaDep_.emplace_back(pair.first.getString(), + SchemaValidator::make(context, pair.second)); } } } diff --git a/folly/experimental/fibers/SimpleLoopController.h b/folly/experimental/fibers/SimpleLoopController.h index de074bbd..e22480b5 100644 --- a/folly/experimental/fibers/SimpleLoopController.h +++ b/folly/experimental/fibers/SimpleLoopController.h @@ -77,7 +77,7 @@ class SimpleLoopController : public LoopController { } void timedSchedule(std::function func, TimePoint time) override { - scheduledFuncs_.push_back({time, std::move(func)}); + scheduledFuncs_.emplace_back(time, std::move(func)); } private: diff --git a/folly/experimental/fibers/test/FibersTest.cpp b/folly/experimental/fibers/test/FibersTest.cpp index 72f351cc..878fc64c 100644 --- a/folly/experimental/fibers/test/FibersTest.cpp +++ b/folly/experimental/fibers/test/FibersTest.cpp @@ -581,7 +581,7 @@ TEST(FiberManager, forEach) { std::vector> results; forEach(funcs.begin(), funcs.end(), [&results](size_t id, int result) { - results.push_back(std::make_pair(id, result)); + results.emplace_back(id, result); }); EXPECT_EQ(3, results.size()); EXPECT_TRUE(pendingFibers.empty()); diff --git a/folly/experimental/test/EventCountTest.cpp b/folly/experimental/test/EventCountTest.cpp index e166c536..ced73283 100644 --- a/folly/experimental/test/EventCountTest.cpp +++ b/folly/experimental/test/EventCountTest.cpp @@ -68,7 +68,7 @@ void randomPartition(Random& random, T key, int n, int m = std::min(n, 1000); std::uniform_int_distribution u(1, m); int cut = u(random); - out.push_back(std::make_pair(key, cut)); + out.emplace_back(key, cut); n -= cut; } } diff --git a/folly/futures/Future-inl.h b/folly/futures/Future-inl.h index 446a88f3..2189585b 100644 --- a/folly/futures/Future-inl.h +++ b/folly/futures/Future-inl.h @@ -704,7 +704,7 @@ collectN(InputIterator first, InputIterator last, size_t n) { auto c = ++ctx->completed; if (c <= n) { assert(ctx->v.size() < n); - ctx->v.push_back(std::make_pair(i, std::move(t))); + ctx->v.emplace_back(i, std::move(t)); if (c == n) { ctx->p.setTry(Try(std::move(ctx->v))); } diff --git a/folly/io/async/SSLContext.h b/folly/io/async/SSLContext.h index 7101b561..20df0c87 100644 --- a/folly/io/async/SSLContext.h +++ b/folly/io/async/SSLContext.h @@ -81,6 +81,8 @@ class SSLContext { }; struct NextProtocolsItem { + NextProtocolsItem(int wt, const std::list& ptcls): + weight(wt), protocols(ptcls) {} int weight; std::list protocols; }; diff --git a/folly/io/async/test/EventBaseTest.cpp b/folly/io/async/test/EventBaseTest.cpp index 349391a0..c0abf9b0 100644 --- a/folly/io/async/test/EventBaseTest.cpp +++ b/folly/io/async/test/EventBaseTest.cpp @@ -1106,7 +1106,7 @@ struct RunInThreadArg { }; void runInThreadTestFunc(RunInThreadArg* arg) { - arg->data->values.push_back(make_pair(arg->thread, arg->value)); + arg->data->values.emplace_back(arg->thread, arg->value); RunInThreadData* data = arg->data; delete arg; diff --git a/folly/test/DeterministicSchedule.cpp b/folly/test/DeterministicSchedule.cpp index 3e0233c0..1b6e9a2e 100644 --- a/folly/test/DeterministicSchedule.cpp +++ b/folly/test/DeterministicSchedule.cpp @@ -247,7 +247,7 @@ Futex::futexWaitImpl( futexLock.lock(); if (data == expected) { auto& queue = futexQueues[this]; - queue.push_back(std::make_pair(waitMask, &awoken)); + queue.emplace_back(waitMask, &awoken); auto ours = queue.end(); ours--; while (!awoken) { diff --git a/folly/test/DynamicTest.cpp b/folly/test/DynamicTest.cpp index 6579578e..aa43c0a9 100644 --- a/folly/test/DynamicTest.cpp +++ b/folly/test/DynamicTest.cpp @@ -52,17 +52,15 @@ TEST(Dynamic, ObjectBasics) { EXPECT_EQ(*newObject.keys().begin(), newObject.items().begin()->first); EXPECT_EQ(*newObject.values().begin(), newObject.items().begin()->second); std::vector> found; - found.push_back(std::make_pair( - newObject.keys().begin()->asString(), - *newObject.values().begin())); + found.emplace_back(newObject.keys().begin()->asString(), + *newObject.values().begin()); EXPECT_EQ(*boost::next(newObject.keys().begin()), boost::next(newObject.items().begin())->first); EXPECT_EQ(*boost::next(newObject.values().begin()), boost::next(newObject.items().begin())->second); - found.push_back(std::make_pair( - boost::next(newObject.keys().begin())->asString(), - *boost::next(newObject.values().begin()))); + found.emplace_back(boost::next(newObject.keys().begin())->asString(), + *boost::next(newObject.values().begin())); std::sort(found.begin(), found.end()); diff --git a/folly/wangle/ssl/SSLContextConfig.h b/folly/wangle/ssl/SSLContextConfig.h index 9c6987af..47aa3f11 100644 --- a/folly/wangle/ssl/SSLContextConfig.h +++ b/folly/wangle/ssl/SSLContextConfig.h @@ -31,6 +31,10 @@ struct SSLContextConfig { ~SSLContextConfig() {} struct CertificateInfo { + CertificateInfo(const std::string& crtPath, + const std::string& kyPath, + const std::string& passwdPath) + : certPath(crtPath), keyPath(kyPath), passwordPath(passwdPath) {} std::string certPath; std::string keyPath; std::string passwordPath; @@ -49,7 +53,7 @@ struct SSLContextConfig { void addCertificate(const std::string& certPath, const std::string& keyPath, const std::string& passwordPath) { - certificates.emplace_back(CertificateInfo{certPath, keyPath, passwordPath}); + certificates.emplace_back(certPath, keyPath, passwordPath); } /** @@ -58,7 +62,7 @@ struct SSLContextConfig { */ void setNextProtocols(const std::list& inNextProtocols) { nextProtocols.clear(); - nextProtocols.push_back({1, inNextProtocols}); + nextProtocols.emplace_back(1, inNextProtocols); } typedef std::function SNINoMatchFn; diff --git a/folly/wangle/ssl/SSLSessionCacheManager.cpp b/folly/wangle/ssl/SSLSessionCacheManager.cpp index 2b1f8a48..82449105 100644 --- a/folly/wangle/ssl/SSLSessionCacheManager.cpp +++ b/folly/wangle/ssl/SSLSessionCacheManager.cpp @@ -251,8 +251,7 @@ SSL_SESSION* SSLSessionCacheManager::getSession(SSL* ssl, SSLUtil::hexlify(sessionId); std::unique_ptr dg( new DelayedDestruction::DestructorGuard(sslSocket)); - pit->second.waiters.push_back( - std::make_pair(sslSocket, std::move(dg))); + pit->second.waiters.emplace_back(sslSocket, std::move(dg)); *copyflag = SSL_SESSION_CB_WOULD_BLOCK; return nullptr; } -- 2.34.1