Commit be55c933 authored by Stepan Palamarchuk's avatar Stepan Palamarchuk Committed by Facebook Github Bot

Avoid repeated calls to std::chrono::steady_clock::now in cascading logic

Summary: There's no need to extensively call into steady_clock, because the time is unlikely to change. And if it does change, then it means that we'd do incorrect math (like firing timeout 10ms earlier).

Reviewed By: djwatson, stevegury

Differential Revision: D13541503

fbshipit-source-id: cc46c8a6bd6a72544803a22d16235be54fa94cc4
parent 65ad7e9d
......@@ -177,11 +177,12 @@ void HHWheelTimer::scheduleTimeout(Callback* callback) {
bool HHWheelTimer::cascadeTimers(int bucket, int tick) {
CallbackList cbs;
cbs.swap(buckets_[bucket][tick]);
auto now = getCurTime();
auto nextTick = calcNextTick(now);
while (!cbs.empty()) {
auto* cb = &cbs.front();
cbs.pop_front();
scheduleTimeoutImpl(
cb, cb->getTimeRemaining(getCurTime()), expireTick_, calcNextTick());
scheduleTimeoutImpl(cb, cb->getTimeRemaining(now), expireTick_, nextTick);
}
// If tick is zero, timeoutExpired will cascade the next bucket.
......@@ -321,7 +322,12 @@ size_t HHWheelTimer::cancelTimeoutsFromList(CallbackList& timeouts) {
}
int64_t HHWheelTimer::calcNextTick() {
return (getCurTime() - startTime_) / interval_;
return calcNextTick(getCurTime());
}
int64_t HHWheelTimer::calcNextTick(
std::chrono::steady_clock::time_point curTime) {
return (curTime - startTime_) / interval_;
}
} // namespace folly
......@@ -298,6 +298,7 @@ class HHWheelTimer : private folly::AsyncTimeout,
std::chrono::steady_clock::time_point startTime_;
int64_t calcNextTick();
int64_t calcNextTick(std::chrono::steady_clock::time_point curTime);
/**
* Schedule a given timeout by putting it into the appropriate bucket of the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment