Commit c605a4cb authored by Juho Snellman's avatar Juho Snellman

Further ticks_to_next_event optimization

- Stop recursing to the outer wheels if the minimum range of the wheel
  is higher than the maximum acceptable return value.
parent 244e4ac2
...@@ -219,7 +219,7 @@ class TimerWheel { ...@@ -219,7 +219,7 @@ class TimerWheel {
public: public:
TimerWheel(Tick now = 0) TimerWheel(Tick now = 0)
: now_(now), : now_(now),
out_(new TimerWheel(WIDTH_BITS, this)), out_(new TimerWheel(now, WIDTH_BITS, this)),
core_(NULL) { core_(NULL) {
} }
...@@ -261,18 +261,21 @@ private: ...@@ -261,18 +261,21 @@ private:
TimerWheel(const TimerWheel& other) = delete; TimerWheel(const TimerWheel& other) = delete;
TimerWheel& operator=(const TimerWheel& other) = delete; TimerWheel& operator=(const TimerWheel& other) = delete;
TimerWheel(int offset, TimerWheel* down) TimerWheel(Tick now, int offset, TimerWheel* down)
: now_(0), : now_(now >> offset),
shift_(offset),
core_(down) { core_(down) {
if (offset + WIDTH_BITS < 64) { if (offset + WIDTH_BITS < 64) {
out_.reset(new TimerWheel(offset + WIDTH_BITS, down)); out_.reset(new TimerWheel(now, offset + WIDTH_BITS, down));
} }
} }
// The current timestamp for this wheel. This will be right-shifted // The current timestamp for this wheel. This will be right-shifted
// such that each slot is separated by exactly one tick even on // such that each slot is separated by exactly one tick even on
// the outermost wheels. // the outermost wheels.
Tick now_; Tick now_;
// The amount that the tick has been right-shifted by.
int shift_ = 0;
static const int WIDTH_BITS = 8; static const int WIDTH_BITS = 8;
static const int NUM_SLOTS = 1 << WIDTH_BITS; static const int NUM_SLOTS = 1 << WIDTH_BITS;
...@@ -453,10 +456,12 @@ Tick TimerWheel::ticks_to_next_event(const Tick& max) { ...@@ -453,10 +456,12 @@ Tick TimerWheel::ticks_to_next_event(const Tick& max) {
} }
} }
// Nothing found on this wheel, try the next one. // Nothing found on this wheel, try the next one (unless the wheel can't
if (out_) { // possibly contain an event scheduled earlier than "max").
if (out_ && (max >> out_->shift_) > 0) {
return out_->ticks_to_next_event(max); return out_->ticks_to_next_event(max);
} }
return max; return max;
} }
......
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