LibCore: Only use coarse time in the Unix event loop wait_for_events()

A typo in the changes to our userland timekeeping classes caused us to
make a syscall every time we want to check whether a timer is ready to
fire in `EventLoopManagerUnix::wait_for_events()`. Instead, only use
coarse time, and get it immediately before it is used in both cases.

This reduces CPU usage by an (eyeballed) 20-30% while playing back
video with VideoPlayer.
This commit is contained in:
Zaggy1024 2023-08-07 05:28:46 -05:00 committed by Tim Schumacher
parent 742eff26a9
commit db2a8725c6

View file

@ -169,13 +169,12 @@ retry:
// Figure out how long to wait at maximum.
// This mainly depends on the PumpMode and whether we have pending events, but also the next expiring timer.
MonotonicTime now = MonotonicTime::now_coarse();
struct timeval timeout = { 0, 0 };
bool should_wait_forever = false;
if (mode == EventLoopImplementation::PumpMode::WaitForEvents && !has_pending_events) {
auto next_timer_expiration = get_next_timer_expiration();
if (next_timer_expiration.has_value()) {
now = MonotonicTime::now();
auto now = MonotonicTime::now_coarse();
auto computed_timeout = next_timer_expiration.value() - now;
if (computed_timeout.is_negative())
computed_timeout = Duration::zero();
@ -228,11 +227,10 @@ try_select_again:
goto retry;
}
if (!thread_data.timers.is_empty()) {
now = MonotonicTime::now_coarse();
}
// Handle expired timers.
if (!thread_data.timers.is_empty()) {
auto now = MonotonicTime::now_coarse();
for (auto& it : thread_data.timers) {
auto& timer = *it.value;
if (!timer.has_expired(now))
@ -252,6 +250,7 @@ try_select_again:
VERIFY_NOT_REACHED();
}
}
}
if (!marked_fd_count)
return;