Skip to content

Commit be5635b

Browse files
Reduce CPU load from try_read_inner() by performing a sleeping wait
Currently TryStream::try_read_inner() are part of a busy-loop that waits for characters. We can make that wait use significantly less CPU by sleeping until characters are available instead. This should have no impact on performance providing characters or EOF are received before reaching the timeout. The 100ms timeout could impact the responsiveness of Sessions with a short timeouts. Happily these sessions should be uncommon and their needs could be met by disabling the polling feature.
1 parent 667c8c3 commit be5635b

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

src/session/sync_session.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,12 @@ where
508508

509509
// non-buffered && non-blocking read
510510
fn try_read_inner(&mut self, buf: &mut [u8]) -> io::Result<usize> {
511+
// We don't know what the session timeout is but it is unlikely to be
512+
// less than 100ms (and if it is then polling is optional anyway).
513+
if !self.stream.get_mut().ready(Duration::from_millis(100))? {
514+
return Err(io::Error::from(io::ErrorKind::WouldBlock));
515+
}
516+
511517
self.stream.get_mut().set_blocking(false)?;
512518

513519
let result = self.stream.get_mut().read(buf);

0 commit comments

Comments
 (0)