From 750d49c8a83cbf3f99b0fc576b8ea08eeced4688 Mon Sep 17 00:00:00 2001 From: Chad Parry Date: Tue, 8 Dec 2015 14:37:00 -0800 Subject: [PATCH] Avoid cost of cancelAll Summary: This is an experimental diff that may improve the performance of `HHWheelTimer::cancelAll`. If there are no timers to cancel, then it can bail early. Since perflab is unlikely to be conclusive, and since this diff looks like a strict improvement, I'll proceed with landing it. Reviewed By: djwatson Differential Revision: D2735297 fb-gh-sync-id: 9f5a811ee6d9fa9434576e9abd3ef3443a7579b7 --- folly/io/async/HHWheelTimer.cpp | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/folly/io/async/HHWheelTimer.cpp b/folly/io/async/HHWheelTimer.cpp index 65c3fff6..cd9f5c7e 100644 --- a/folly/io/async/HHWheelTimer.cpp +++ b/folly/io/async/HHWheelTimer.cpp @@ -218,30 +218,32 @@ void HHWheelTimer::timeoutExpired() noexcept { } size_t HHWheelTimer::cancelAll() { - decltype(buckets_) buckets; + size_t count = 0; + + if (count_ != 0) { + decltype(buckets_) buckets; // Work around std::swap() bug in libc++ // // http://llvm.org/bugs/show_bug.cgi?id=22106 #if FOLLY_USE_LIBCPP - for (size_t i = 0; i < WHEEL_BUCKETS; ++i) { - for (size_t ii = 0; ii < WHEEL_SIZE; ++ii) { - std::swap(buckets_[i][ii], buckets[i][ii]); + for (size_t i = 0; i < WHEEL_BUCKETS; ++i) { + for (size_t ii = 0; ii < WHEEL_SIZE; ++ii) { + std::swap(buckets_[i][ii], buckets[i][ii]); + } } - } #else - std::swap(buckets, buckets_); + std::swap(buckets, buckets_); #endif - size_t count = 0; - - for (auto& tick : buckets) { - for (auto& bucket : tick) { - while (!bucket.empty()) { - auto& cb = bucket.front(); - cb.cancelTimeout(); - cb.callbackCanceled(); - count++; + for (auto& tick : buckets) { + for (auto& bucket : tick) { + while (!bucket.empty()) { + auto& cb = bucket.front(); + cb.cancelTimeout(); + cb.callbackCanceled(); + count++; + } } } } -- 2.34.1