Needless temporary gone
authorPraveen Kumar <cpp.fool@gmail.com>
Fri, 12 Jun 2015 21:44:14 +0000 (14:44 -0700)
committerSara Golemon <sgolemon@fb.com>
Fri, 12 Jun 2015 21:54:35 +0000 (14:54 -0700)
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

13 files changed:
folly/Subprocess.cpp
folly/Subprocess.h
folly/experimental/JSONSchema.cpp
folly/experimental/fibers/SimpleLoopController.h
folly/experimental/fibers/test/FibersTest.cpp
folly/experimental/test/EventCountTest.cpp
folly/futures/Future-inl.h
folly/io/async/SSLContext.h
folly/io/async/test/EventBaseTest.cpp
folly/test/DeterministicSchedule.cpp
folly/test/DynamicTest.cpp
folly/wangle/ssl/SSLContextConfig.h
folly/wangle/ssl/SSLSessionCacheManager.cpp

index 04866e1dde2ec52079d29b70aa3296a741fa8826..7ecc8de486634aa315187438808b9a358eb5393d 100644 (file)
@@ -819,7 +819,7 @@ void Subprocess::closeParentFd(int childFd) {
 std::vector<Subprocess::ChildPipe> Subprocess::takeOwnershipOfPipes() {
   std::vector<Subprocess::ChildPipe> 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;
index da2b560241fdf80eb9160648d6f5746487b18cc4..b1f6707f95891002e28e8714d04d6aa5df14a10b 100644 (file)
@@ -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
   };
index 5297c28e4f4c9e71071eb688f1cc6f0edcaa0740..51a6d20a0752d25139048074507b2c694a25ce0b 100644 (file)
@@ -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));
       }
     }
   }
index de074bbd9c7b172144799fc012e48915a2a2c156..e22480b5a90ed727d495f5ba57010e80e9af082c 100644 (file)
@@ -77,7 +77,7 @@ class SimpleLoopController : public LoopController {
   }
 
   void timedSchedule(std::function<void()> func, TimePoint time) override {
-    scheduledFuncs_.push_back({time, std::move(func)});
+    scheduledFuncs_.emplace_back(time, std::move(func));
   }
 
  private:
index 72f351cc1e15d4245138d1aa30c1d40a61987802..878fc64cdf5090ebf63214d7ad74c92dab59a251 100644 (file)
@@ -581,7 +581,7 @@ TEST(FiberManager, forEach) {
           std::vector<std::pair<size_t, int>> 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());
index e166c53653c3f650ff175c9499ec26c5ad2e4d74..ced73283cc3eed233118168cad56910f98fe9a3c 100644 (file)
@@ -68,7 +68,7 @@ void randomPartition(Random& random, T key, int n,
     int m = std::min(n, 1000);
     std::uniform_int_distribution<uint32_t> u(1, m);
     int cut = u(random);
-    out.push_back(std::make_pair(key, cut));
+    out.emplace_back(key, cut);
     n -= cut;
   }
 }
index 446a88f34baa6c139b0519d53113ee7bfdfb01fd..2189585bec8ea238dbc8b37e396d45c1a410799d 100644 (file)
@@ -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<V>(std::move(ctx->v)));
         }
index 7101b561592d4861a1986bc215f9db60ca678bfe..20df0c877944f425fac55a6e81fc46ae9ec7ea45 100644 (file)
@@ -81,6 +81,8 @@ class SSLContext {
   };
 
   struct NextProtocolsItem {
+    NextProtocolsItem(int wt, const std::list<std::string>& ptcls):
+      weight(wt), protocols(ptcls) {}
     int weight;
     std::list<std::string> protocols;
   };
index 349391a01190893fba38c43c1216b258beca1b6e..c0abf9b0135e77db6370e234bc2bdd1ae909455a 100644 (file)
@@ -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;
 
index 3e0233c0ade29be1900917c70b163b93e8e49353..1b6e9a2ed3f5d7974df5b7b61cd82619d9cebdfd 100644 (file)
@@ -247,7 +247,7 @@ Futex<DeterministicAtomic>::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) {
index 6579578e4cbbea58e7802b6223b9217132a61d3b..aa43c0a9b2c6dc0f0a99e41dc27dd8a17677c73d 100644 (file)
@@ -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<std::pair<folly::fbstring, dynamic>> 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());
 
index 9c6987af5952b389bac36d26aff80ebf8a512ae0..47aa3f111aff87966c19f510a1daa25d18d0c1db 100644 (file)
@@ -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<std::string>& inNextProtocols) {
     nextProtocols.clear();
-    nextProtocols.push_back({1, inNextProtocols});
+    nextProtocols.emplace_back(1, inNextProtocols);
   }
 
   typedef std::function<bool(char const* server_name)> SNINoMatchFn;
index 2b1f8a48113c90e8d9fb31d63c30ff7d63c3e1d9..82449105e181651ac2c5d14b0570a157320aea5f 100644 (file)
@@ -251,8 +251,7 @@ SSL_SESSION* SSLSessionCacheManager::getSession(SSL* ssl,
             SSLUtil::hexlify(sessionId);
           std::unique_ptr<DelayedDestruction::DestructorGuard> 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;
         }