From a53001260e34459f5d74d0c165d1813be4078679 Mon Sep 17 00:00:00 2001 From: Karl Rikte Date: Sat, 13 Jan 2024 00:19:35 +0100 Subject: [PATCH] Improve response times When the network interface can't make any progress, it's probably a good idea to yeild the task so that the wifi task may run and proceed with actually sending/receiving data. This seems to greatly improve response times, especially with low tick_rate_hz values. At 100Hz, ping time goes down from 25ms to <2ms. A simple http server test saw a response time reduction from 45ms to 6ms. At 1000Hz tick rate the gains are diminishing but there are some small gains. I ran my tests on esp32s3. This might also increase throughput. I have not tested that. It also may have some unforseen adverse effects. Ideally, we would have a more fine grained return value from interface.poll() than a bool so we have a better idea if yeilding is appropriate or not. --- esp-wifi/src/wifi_interface.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/esp-wifi/src/wifi_interface.rs b/esp-wifi/src/wifi_interface.rs index 8af82416..bf293f44 100644 --- a/esp-wifi/src/wifi_interface.rs +++ b/esp-wifi/src/wifi_interface.rs @@ -443,6 +443,7 @@ impl<'a, MODE: WifiDeviceMode> WifiStack<'a, MODE> { sockets, ) }) { + crate::timer::yield_task(); break; } } @@ -713,7 +714,9 @@ impl<'s, 'n: 's, MODE: WifiDeviceMode> Read for Socket<'s, 'n, MODE> { use smoltcp::socket::tcp::RecvError; loop { - interface.poll(timestamp(), device, sockets); + if interface.poll(timestamp(), device, sockets) == false { + crate::timer::yield_task(); + } let socket = sockets.get_mut::(self.socket_handle); match socket.recv_slice(buf) { @@ -733,11 +736,14 @@ impl<'s, 'n: 's, MODE: WifiDeviceMode> Write for Socket<'s, 'n, MODE> { loop { let (may_send, is_open, can_send) = self.network.with_mut(|interface, device, sockets| { - interface.poll( + if interface.poll( Instant::from_millis((self.network.current_millis_fn)() as i64), device, sockets, - ); + ) == false + { + crate::timer::yield_task(); + } let socket = sockets.get_mut::(self.socket_handle); @@ -784,6 +790,7 @@ impl<'s, 'n: 's, MODE: WifiDeviceMode> Write for Socket<'s, 'n, MODE> { }); if let false = res { + crate::timer::yield_task(); break; } }