Using emplace_back to avoid temporary
authorPraveen Kumar <cpp.fool@gmail.com>
Tue, 16 Jun 2015 17:02:00 +0000 (10:02 -0700)
committerSara Golemon <sgolemon@fb.com>
Wed, 17 Jun 2015 17:26:16 +0000 (10:26 -0700)
Summary: Directly pass the arguments to respective constructors.
Instead of first making temporary and then pass that.

Closes #218

Reviewed By: @yfeldblum

Differential Revision: D2156978

Pulled By: @sgolemon

folly/io/async/test/AsyncSocketTest2.cpp
folly/io/async/test/EventBaseTest.cpp
folly/io/async/test/HHWheelTimerTest.cpp
folly/test/small_vector_test.cpp
folly/test/sorted_vector_test.cpp
folly/wangle/concurrent/PriorityLifoSemMPMCQueue.h

index 2f31124b6e959f06ebfe057320868f81642231b3..f44d4fd52779806a9f47de97541f32e066529f8a 100644 (file)
@@ -1443,28 +1443,28 @@ class TestAcceptCallback : public AsyncServerSocket::AcceptCallback {
 
   void connectionAccepted(int fd, const folly::SocketAddress& clientAddr)
       noexcept {
-    events_.push_back(EventInfo(fd, clientAddr));
+    events_.emplace_back(fd, clientAddr);
 
     if (connectionAcceptedFn_) {
       connectionAcceptedFn_(fd, clientAddr);
     }
   }
   void acceptError(const std::exception& ex) noexcept {
-    events_.push_back(EventInfo(ex.what()));
+    events_.emplace_back(ex.what());
 
     if (acceptErrorFn_) {
       acceptErrorFn_(ex);
     }
   }
   void acceptStarted() noexcept {
-    events_.push_back(EventInfo(TYPE_START));
+    events_.emplace_back(TYPE_START);
 
     if (acceptStartedFn_) {
       acceptStartedFn_();
     }
   }
   void acceptStopped() noexcept {
-    events_.push_back(EventInfo(TYPE_STOP));
+    events_.emplace_back(TYPE_STOP);
 
     if (acceptStoppedFn_) {
       acceptStoppedFn_();
index c0abf9b0135e77db6370e234bc2bdd1ae909455a..5e8520eca9165f1c35fe1c5d005e6f80cdde0707 100644 (file)
@@ -155,7 +155,7 @@ class TestHandler : public EventHandler {
       bytesWritten = writeUntilFull(fd_);
     }
 
-    log.push_back(EventRecord(events, bytesRead, bytesWritten));
+    log.emplace_back(events, bytesRead, bytesWritten);
   }
 
   struct EventRecord {
@@ -648,7 +648,7 @@ class PartialReadHandler : public TestHandler {
   virtual void handlerReady(uint16_t events) noexcept {
     assert(events == EventHandler::READ);
     ssize_t bytesRead = readFromFD(fd_, readLength_);
-    log.push_back(EventRecord(events, bytesRead, 0));
+    log.emplace_back(events, bytesRead, 0);
   }
 
  private:
@@ -713,7 +713,7 @@ class PartialWriteHandler : public TestHandler {
   virtual void handlerReady(uint16_t events) noexcept {
     assert(events == EventHandler::WRITE);
     ssize_t bytesWritten = writeToFD(fd_, writeLength_);
-    log.push_back(EventRecord(events, 0, bytesWritten));
+    log.emplace_back(events, 0, bytesWritten);
   }
 
  private:
@@ -934,7 +934,7 @@ class ReschedulingTimeout : public AsyncTimeout {
   }
 
   virtual void timeoutExpired() noexcept {
-    timestamps.push_back(TimePoint());
+    timestamps.emplace_back();
     reschedule();
   }
 
index e2b5c096291601200d97dbc962ec9627f66e98b5..6cabc028174dd9b2f81cce3b6796458e5b81c77a 100644 (file)
@@ -37,14 +37,14 @@ class TestTimeout : public HHWheelTimer::Callback {
   }
 
   void timeoutExpired() noexcept override {
-    timestamps.push_back(TimePoint());
+    timestamps.emplace_back();
     if (fn) {
       fn();
     }
   }
 
   void callbackCanceled() noexcept override {
-    canceledTimestamps.push_back(TimePoint());
+    canceledTimestamps.emplace_back();
     if (fn) {
       fn();
     }
index 616dfd2bbf32d6ee67941ef2b9bf08430fc42767..860fddc6b49ef1f777198d4c7bdc3611396fde44 100644 (file)
@@ -159,7 +159,7 @@ struct TestBasicGuarantee {
   {
     throwCounter = 1000;
     for (int i = 0; i < prepopulate; ++i) {
-      vec.push_back(Thrower());
+      vec.emplace_back();
     }
   }
 
@@ -203,7 +203,7 @@ TEST(small_vector, BasicGuarantee) {
     (TestBasicGuarantee(prepop))( // parens or a mildly vexing parse :(
       1,
       [&] (folly::small_vector<Thrower,3>& v) {
-        v.push_back(Thrower());
+        v.emplace_back();
       }
     );
 
@@ -232,9 +232,9 @@ TEST(small_vector, BasicGuarantee) {
     3,
     [&] (folly::small_vector<Thrower,3>& v) {
       std::vector<Thrower> b;
-      b.push_back(Thrower());
-      b.push_back(Thrower());
-      b.push_back(Thrower());
+      b.emplace_back();
+      b.emplace_back();
+      b.emplace_back();
 
       /*
        * Apparently if you do the following initializer_list instead
@@ -251,7 +251,7 @@ TEST(small_vector, BasicGuarantee) {
     [&] (folly::small_vector<Thrower,3>& v) {
       std::vector<Thrower> b;
       for (int i = 0; i < 6; ++i) {
-        b.push_back(Thrower());
+        b.emplace_back();
       }
 
       v.insert(v.begin() + 1, b.begin(), b.end());
index 939a085df2ac77e97c48c437d9525fd95b95fc95..e1366787ab00a4df74b5a71d17259f47c8b9a51e 100644 (file)
@@ -280,7 +280,7 @@ TEST(SortedVectorTypes, GrowthPolicy) {
 
   std::list<CountCopyCtor> v;
   for (int i = 0; i < 20; ++i) {
-    v.push_back(CountCopyCtor(20 + i));
+    v.emplace_back(20 + i);
   }
   a.insert(v.begin(), v.end());
   check_invariant(a);
index 583a9a34b34001a9c9718e674e9d617c1caead55..3a831fa33ceaf765aa0afab51f7ba23033553aa9 100644 (file)
@@ -27,7 +27,7 @@ class PriorityLifoSemMPMCQueue : public BlockingQueue<T> {
   explicit PriorityLifoSemMPMCQueue(uint8_t numPriorities, size_t capacity) {
     queues_.reserve(numPriorities);
     for (int8_t i = 0; i < numPriorities; i++) {
-      queues_.push_back(MPMCQueue<T>(capacity));
+      queues_.emplace_back(capacity);
     }
   }