From b60704ccc1f84420be6e0c1939a1c9e2967e8ded Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Fri, 31 Dec 2021 17:41:08 -0500 Subject: [PATCH] drop poll in input thread; may revert but it improves performance The issue seems to be related to a crossterm update, but not sure. --- src/app.rs | 9 +++++++ src/bin/main.rs | 3 +-- src/lib.rs | 68 ++++++++++++++++++++----------------------------- 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/src/app.rs b/src/app.rs index 1f086c1e..a810b9b8 100644 --- a/src/app.rs +++ b/src/app.rs @@ -192,6 +192,14 @@ impl AppState { // FIXME: Redraw with new screen, save old screen state if main } } + + fn toggle_freeze(&mut self) -> Status { + self.frozen_state = match self.frozen_state { + FrozenState::NotFrozen => FrozenState::Frozen(Box::new(self.data_collection.clone())), + FrozenState::Frozen(_) => FrozenState::NotFrozen, + }; + Status::Captured + } } impl Application for AppState { @@ -296,6 +304,7 @@ impl Application for AppState { Event::Keyboard(event) => { if event.modifiers.is_empty() { match event.code { + KeyCode::Char('f') | KeyCode::Char('F') => self.toggle_freeze(), KeyCode::Char('q') | KeyCode::Char('Q') => on_quit(messages), _ => Status::Ignored, } diff --git a/src/bin/main.rs b/src/bin/main.rs index 73ac1732..3f3318ff 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -38,7 +38,7 @@ fn main() -> Result<()> { // Set up input handling let (sender, receiver) = mpsc::channel(); - let input_thread = create_input_thread(sender.clone(), thread_termination_lock.clone()); + let _input_thread = create_input_thread(sender.clone()); // Cleaning loop // TODO: [Refactor, Optimization (Potentially, maybe not)] Probably worth spinning this off into an async thread or something... @@ -95,7 +95,6 @@ fn main() -> Result<()> { *thread_termination_lock.lock().unwrap() = true; thread_termination_cvar.notify_all(); - let _ = input_thread.join(); cleanup_terminal(&mut terminal); Ok(()) diff --git a/src/lib.rs b/src/lib.rs index 8ba970aa..f7b6771f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -183,7 +183,6 @@ pub fn panic_hook(panic_info: &PanicInfo<'_>) { pub fn create_input_thread( sender: std::sync::mpsc::Sender>, - termination_ctrl_lock: Arc>, ) -> std::thread::JoinHandle<()> { thread::spawn(move || { // TODO: [Optimization, Input] Maybe experiment with removing these timers. Look into using buffers instead? @@ -191,51 +190,38 @@ pub fn create_input_thread( let mut keyboard_timer = Instant::now(); loop { - if let Ok(is_terminated) = termination_ctrl_lock.try_lock() { - // We don't block. - if *is_terminated { - drop(is_terminated); - break; - } - } - - if let Ok(poll) = poll(Duration::from_millis(20)) { - if poll { - if let Ok(event) = read() { - match event { - crossterm::event::Event::Key(event) => { - if Instant::now().duration_since(keyboard_timer).as_millis() >= 20 { - if sender - .send(RuntimeEvent::UserInterface(Event::Keyboard(event))) - .is_err() - { - break; - } - keyboard_timer = Instant::now(); - } + if let Ok(event) = read() { + match event { + crossterm::event::Event::Key(event) => { + if Instant::now().duration_since(keyboard_timer).as_millis() >= 20 { + if sender + .send(RuntimeEvent::UserInterface(Event::Keyboard(event))) + .is_err() + { + break; } - crossterm::event::Event::Mouse(event) => match &event.kind { - MouseEventKind::Drag(_) => {} - MouseEventKind::Moved => {} - _ => { - if Instant::now().duration_since(mouse_timer).as_millis() >= 20 - { - if sender - .send(RuntimeEvent::UserInterface(Event::Mouse(event))) - .is_err() - { - break; - } - mouse_timer = Instant::now(); - } - } - }, - crossterm::event::Event::Resize(width, height) => { - if sender.send(RuntimeEvent::Resize { width, height }).is_err() { + keyboard_timer = Instant::now(); + } + } + crossterm::event::Event::Mouse(event) => match &event.kind { + MouseEventKind::Drag(_) => {} + MouseEventKind::Moved => {} + _ => { + if Instant::now().duration_since(mouse_timer).as_millis() >= 20 { + if sender + .send(RuntimeEvent::UserInterface(Event::Mouse(event))) + .is_err() + { break; } + mouse_timer = Instant::now(); } } + }, + crossterm::event::Event::Resize(width, height) => { + if sender.send(RuntimeEvent::Resize { width, height }).is_err() { + break; + } } } }