wonrax / rust-experiments

learning rust 🤓
0 stars 0 forks source link

async runtime consumes too much CPU while idle #2

Open wonrax opened 1 week ago

wonrax commented 1 week ago
// TODO since we're not using crossbeam channel's recv(), we don't get
// the benefit of yielding the thread when the channel is empty.
// Performance opportunities:
// - implement or use crossbeam's Backoff to yield the thread or spin
//   when the channel is empty
// - park the thread and use signal mechanism to wake up the thread when
//   there's a new task
wonrax commented 1 week ago

Looking through the Tokio codebase, it seems like the parking mechanism is actually implemented by the I/O driver or the timer driver. If neither driver is enabled, Tokio will fall back to parking the thread using a condition variable or thread parking.

If the I/O driver is enabled, it uses Linux's epoll syscall with a timeout parameter to park the thread until a point where a timer entry needs to be woken up (this is useful for our ongoing implementation of wheel timer). If there's a new task before the timer expires, Tokio will manually fire a fake (?) I/O event to make epoll return.

If the I/O driver is not enabled but the timer driver is, the timer driver will park the thread using a condition variable, similarly with a timeout of the duration until the next timer entry expires.