Commit 244e4ac2 authored by Juho Snellman's avatar Juho Snellman

Optimize ticks_to_next_event a bit

- When on the core wheel, use time of first event in slot rather than
  walking through whole chain. (All guaranteed to have same time).
- When on the core wheel and processing slot 0, only check outer wheel
  if slot 0 is empty. (Any events in slot 0 are guaranteed to be
  scheduled no later than anything on the outer wheel).
parent 0f379ac1
...@@ -422,19 +422,32 @@ Tick TimerWheel::ticks_to_next_event(const Tick& max) { ...@@ -422,19 +422,32 @@ Tick TimerWheel::ticks_to_next_event(const Tick& max) {
// check the best result there against the next slot on // check the best result there against the next slot on
// this wheel. // this wheel.
if (slot_index == 0 && out_) { if (slot_index == 0 && out_) {
// Exception: If we're in the core wheel, and slot 0 is
// not empty, there's no point in looking in the outer wheel.
// It's guaranteed that the events actually in slot 0 will be
// executed no later than anything in the outer wheel.
if (core_ || !slots_[0].events()) {
const auto& slot = out_->slots_[(out_->now_ + 1) & MASK]; const auto& slot = out_->slots_[(out_->now_ + 1) & MASK];
for (auto event = slot.events(); event != NULL; for (auto event = slot.events(); event != NULL;
event = event->next_) { event = event->next_) {
min = std::min(min, event->scheduled_at() - now); min = std::min(min, event->scheduled_at() - now);
} }
} }
}
bool found = false; bool found = false;
const auto& slot = slots_[slot_index]; const auto& slot = slots_[slot_index];
for (auto event = slot.events(); event != NULL; for (auto event = slot.events(); event != NULL;
event = event->next_) { event = event->next_) {
min = std::min(min, event->scheduled_at() - now); min = std::min(min, event->scheduled_at() - now);
// In the core wheel all the events in a slot are guaranteed to
// run at the same time, so it's enough to just look at the first
// one.
if (!core_) {
return min;
} else {
found = true; found = true;
} }
}
if (found) { if (found) {
return min; return min;
} }
......
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