From e9e3d9b685715f91ec9527e11723e85fcee2a85e Mon Sep 17 00:00:00 2001 From: Praveen Kumar Date: Fri, 12 Jun 2015 14:44:00 -0700 Subject: [PATCH] Using emplace_back to avoid temporary Summary: Directly pass the arguments to respective constructors. Instead of first making temporary and then pass that. Closes #218 Reviewed By: @JoelMarcey Differential Revision: D2151582 Pulled By: @sgolemon --- folly/Padded.h | 4 ++-- folly/io/async/test/AsyncSocketTest2.cpp | 8 ++++---- folly/io/async/test/EventBaseTest.cpp | 8 ++++---- folly/io/async/test/HHWheelTimerTest.cpp | 4 ++-- folly/test/small_vector_test.cpp | 12 ++++++------ folly/test/sorted_vector_test.cpp | 2 +- folly/wangle/concurrent/PriorityLifoSemMPMCQueue.h | 2 +- 7 files changed, 20 insertions(+), 20 deletions(-) diff --git a/folly/Padded.h b/folly/Padded.h index c5b53a7c..e359ea11 100644 --- a/folly/Padded.h +++ b/folly/Padded.h @@ -350,7 +350,7 @@ class Adaptor { Adaptor(const Adaptor&) = default; Adaptor& operator=(const Adaptor&) = default; - Adaptor(Adaptor&& other) + Adaptor(Adaptor&& other) /* may throw */ : c_(std::move(other.c_)), lastCount_(other.lastCount_) { other.lastCount_ = Node::kElementCount; @@ -426,7 +426,7 @@ class Adaptor { void push_back(value_type x) { if (lastCount_ == Node::kElementCount) { - c_.push_back(Node()); + c_.emplace_back(); lastCount_ = 0; } c_.back().data()[lastCount_++] = std::move(x); diff --git a/folly/io/async/test/AsyncSocketTest2.cpp b/folly/io/async/test/AsyncSocketTest2.cpp index ca075164..bcbe5ec8 100644 --- a/folly/io/async/test/AsyncSocketTest2.cpp +++ b/folly/io/async/test/AsyncSocketTest2.cpp @@ -1334,28 +1334,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_(); diff --git a/folly/io/async/test/EventBaseTest.cpp b/folly/io/async/test/EventBaseTest.cpp index c0abf9b0..5e8520ec 100644 --- a/folly/io/async/test/EventBaseTest.cpp +++ b/folly/io/async/test/EventBaseTest.cpp @@ -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(); } diff --git a/folly/io/async/test/HHWheelTimerTest.cpp b/folly/io/async/test/HHWheelTimerTest.cpp index e2b5c096..6cabc028 100644 --- a/folly/io/async/test/HHWheelTimerTest.cpp +++ b/folly/io/async/test/HHWheelTimerTest.cpp @@ -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(); } diff --git a/folly/test/small_vector_test.cpp b/folly/test/small_vector_test.cpp index 616dfd2b..860fddc6 100644 --- a/folly/test/small_vector_test.cpp +++ b/folly/test/small_vector_test.cpp @@ -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& v) { - v.push_back(Thrower()); + v.emplace_back(); } ); @@ -232,9 +232,9 @@ TEST(small_vector, BasicGuarantee) { 3, [&] (folly::small_vector& v) { std::vector 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& v) { std::vector b; for (int i = 0; i < 6; ++i) { - b.push_back(Thrower()); + b.emplace_back(); } v.insert(v.begin() + 1, b.begin(), b.end()); diff --git a/folly/test/sorted_vector_test.cpp b/folly/test/sorted_vector_test.cpp index 939a085d..e1366787 100644 --- a/folly/test/sorted_vector_test.cpp +++ b/folly/test/sorted_vector_test.cpp @@ -280,7 +280,7 @@ TEST(SortedVectorTypes, GrowthPolicy) { std::list 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); diff --git a/folly/wangle/concurrent/PriorityLifoSemMPMCQueue.h b/folly/wangle/concurrent/PriorityLifoSemMPMCQueue.h index 583a9a34..3a831fa3 100644 --- a/folly/wangle/concurrent/PriorityLifoSemMPMCQueue.h +++ b/folly/wangle/concurrent/PriorityLifoSemMPMCQueue.h @@ -27,7 +27,7 @@ class PriorityLifoSemMPMCQueue : public BlockingQueue { explicit PriorityLifoSemMPMCQueue(uint8_t numPriorities, size_t capacity) { queues_.reserve(numPriorities); for (int8_t i = 0; i < numPriorities; i++) { - queues_.push_back(MPMCQueue(capacity)); + queues_.emplace_back(capacity); } } -- 2.34.1