From bf31f20657094dbc5960203b9d5598bad9ac939b Mon Sep 17 00:00:00 2001 From: Kieran Siek Date: Sat, 12 Aug 2023 09:28:59 -0700 Subject: [PATCH] Fix external editor delay. (#1579) The default polling rate of 1 second causes a 1 second delay when queuing the event to launch the external editor, causing latency. However, a slower polling helps reduce CPU usage, so let's have a short polling duration as long as there are input events, and slow poll otherwise. Since the external editor among other components (not tested) is always launched in response to an input event, we reduce the latency to ~100ms, which is the fast poll duration. Co-authored-by: extrawurst <776816+extrawurst@users.noreply.github.com> --- src/input.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/input.rs b/src/input.rs index 6c794955..47370445 100644 --- a/src/input.rs +++ b/src/input.rs @@ -11,7 +11,8 @@ use std::{ time::Duration, }; -static POLL_DURATION: Duration = Duration::from_millis(1000); +static FAST_POLL_DURATION: Duration = Duration::from_millis(100); +static SLOW_POLL_DURATION: Duration = Duration::from_millis(1000); /// #[derive(Clone, Copy, Debug)] @@ -103,6 +104,7 @@ impl Input { arc_current: &Arc, tx: &Sender, ) -> Result<()> { + let mut poll_duration = SLOW_POLL_DURATION; loop { if arc_desired.get() { if !arc_current.load(Ordering::Relaxed) { @@ -112,14 +114,18 @@ impl Input { } arc_current.store(true, Ordering::Relaxed); - if let Some(e) = Self::poll(POLL_DURATION)? { + if let Some(e) = Self::poll(poll_duration)? { // windows send key release too, only process key press if let Key(key) = e { if key.kind != KeyEventKind::Press { continue; } } + tx.send(InputEvent::Input(e))?; + poll_duration = FAST_POLL_DURATION; + } else { + poll_duration = SLOW_POLL_DURATION; } } else { if arc_current.load(Ordering::Relaxed) {