Sfoglia il codice sorgente

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.
Andreas Kling 3 anni fa
parent
commit
1a323ec8d4
1 ha cambiato i file con 5 aggiunte e 0 eliminazioni
  1. 5 0
      Userland/Libraries/LibCore/EventLoop.cpp

+ 5 - 0
Userland/Libraries/LibCore/EventLoop.cpp

@@ -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;
     }