LibCore: Exit get_next_timer_expiration() sooner if possible

If we find a timer that needs to be fired immediately, we can stop
looking through the remaining timers.

This significantly reduces time spent in get_next_timer_expiration()
on ACID3. Of course, with a better data structure, we could reduce
time spent further. I've left a FIXME about that.
This commit is contained in:
Andreas Kling 2022-02-15 14:47:32 +01:00
parent 06948df393
commit 1a323ec8d4
Notes: sideshowbarker 2024-07-17 18:44:24 +09:00

View file

@ -793,6 +793,7 @@ void EventLoopTimer::reload(const Time& now)
Optional<Time> EventLoop::get_next_timer_expiration()
{
auto now = Time::now_monotonic_coarse();
Optional<Time> soonest {};
for (auto& it : *s_timers) {
auto& fire_time = it.value->fire_time;
@ -801,6 +802,10 @@ Optional<Time> EventLoop::get_next_timer_expiration()
&& owner && !owner->is_visible_for_timer_purposes()) {
continue;
}
// OPTIMIZATION: If we have a timer that needs to fire right away, we can stop looking here.
// FIXME: This whole operation could be O(1) with a better data structure.
if (fire_time < now)
return now;
if (!soonest.has_value() || fire_time < soonest.value())
soonest = fire_time;
}