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,10 +422,16 @@ Tick TimerWheel::ticks_to_next_event(const Tick& max) {
// check the best result there against the next slot on
// this wheel.
if (slot_index == 0 && out_) {
const auto& slot = out_->slots_[(out_->now_ + 1) & MASK];
for (auto event = slot.events(); event != NULL;
event = event->next_) {
min = std::min(min, event->scheduled_at() - now);
// 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];
for (auto event = slot.events(); event != NULL;
event = event->next_) {
min = std::min(min, event->scheduled_at() - now);
}
}
}
bool found = false;
......@@ -433,7 +439,14 @@ Tick TimerWheel::ticks_to_next_event(const Tick& max) {
for (auto event = slot.events(); event != NULL;
event = event->next_) {
min = std::min(min, event->scheduled_at() - now);
found = true;
// 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;
}
}
if (found) {
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