diff options
author | Andreas Kling <kling@serenityos.org> | 2022-02-15 14:47:32 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-15 23:24:41 +0100 |
commit | 1a323ec8d4e493177e6e4ace995357d86b7016a2 (patch) | |
tree | 8a10e5cc2fd186e568edd4362d5541be5d6568e8 /Userland/Libraries/LibCore | |
parent | 06948df39314e13794bbdaf2cd083f32448f76b2 (diff) | |
download | serenity-1a323ec8d4e493177e6e4ace995357d86b7016a2.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibCore')
-rw-r--r-- | Userland/Libraries/LibCore/EventLoop.cpp | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/EventLoop.cpp b/Userland/Libraries/LibCore/EventLoop.cpp index 42734be04d..ed755ceeb6 100644 --- a/Userland/Libraries/LibCore/EventLoop.cpp +++ b/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; } |