From abf66230b7ca38a406fd87d60148df73ea6485af Mon Sep 17 00:00:00 2001 From: Misha Shneerson Date: Tue, 29 Mar 2016 22:42:54 -0700 Subject: [PATCH] fix stack usage in HHWhileTimer Summary:We should be able to use HHWheelTimer in fibers. So it should use way less stack. Alternative solution to D3112305 Reviewed By: haijunz Differential Revision: D3112558 fb-gh-sync-id: 3a52a37d9f9347639005fdf84524f7f8c3041918 fbshipit-source-id: 3a52a37d9f9347639005fdf84524f7f8c3041918 --- folly/io/async/HHWheelTimer.cpp | 42 +++++++++++++++++---------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/folly/io/async/HHWheelTimer.cpp b/folly/io/async/HHWheelTimer.cpp index 0078ae24..f4110b02 100644 --- a/folly/io/async/HHWheelTimer.cpp +++ b/folly/io/async/HHWheelTimer.cpp @@ -229,29 +229,31 @@ size_t HHWheelTimer::cancelAll() { 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]); - } - } -#else - std::swap(buckets, buckets_); -#endif - - for (auto& tick : buckets) { + const size_t numElements = WHEEL_BUCKETS * WHEEL_SIZE; + size_t maxBuckets = std::min(numElements, count_); + auto buckets = folly::make_unique(maxBuckets); + size_t countBuckets = 0; + for (auto& tick : buckets_) { for (auto& bucket : tick) { - while (!bucket.empty()) { - auto& cb = bucket.front(); - cb.cancelTimeout(); - cb.callbackCanceled(); + if (bucket.empty()) { + continue; + } + for (auto& cb : bucket) { count++; } + std::swap(bucket, buckets[countBuckets++]); + if (count >= count_) { + break; + } + } + } + + for (size_t i = 0; i < countBuckets; ++i) { + auto& bucket = buckets[i]; + while (!bucket.empty()) { + auto& cb = bucket.front(); + cb.cancelTimeout(); + cb.callbackCanceled(); } } } -- 2.34.1