From e19c7a417ea4ef456b02958ace4563a87fc1cb62 Mon Sep 17 00:00:00 2001 From: pythops Date: Sun, 15 Sep 2024 21:58:26 +0200 Subject: [PATCH] fix high cpu when fuzzy search is used --- oryx-tui/src/app.rs | 18 ++++++++++++++---- oryx-tui/src/filters/fuzzy.rs | 10 ++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/oryx-tui/src/app.rs b/oryx-tui/src/app.rs index 4312301..0e97f2d 100644 --- a/oryx-tui/src/app.rs +++ b/oryx-tui/src/app.rs @@ -139,14 +139,24 @@ impl App { thread::spawn({ let fuzzy = fuzzy.clone(); let packets = packets.clone(); - move || loop { - thread::sleep(Duration::from_millis(100)); - { + move || { + let mut last_index = 0; + let mut pattern = String::new(); + loop { + thread::sleep(Duration::from_millis(100)); let packets = packets.lock().unwrap(); let mut fuzzy = fuzzy.lock().unwrap(); if fuzzy.is_enabled() && !fuzzy.filter.value().is_empty() { - fuzzy.find(packets.as_slice()); + let current_pattern = fuzzy.filter.value().to_owned(); + if current_pattern != pattern { + fuzzy.find(packets.as_slice()); + pattern = current_pattern; + last_index = packets.len(); + } else { + fuzzy.append(&packets.as_slice()[last_index..]); + last_index = packets.len(); + } } } } diff --git a/oryx-tui/src/filters/fuzzy.rs b/oryx-tui/src/filters/fuzzy.rs index e1a77b4..6d315fa 100644 --- a/oryx-tui/src/filters/fuzzy.rs +++ b/oryx-tui/src/filters/fuzzy.rs @@ -26,6 +26,16 @@ impl Fuzzy { .collect::>(); } + pub fn append(&mut self, packets: &[AppPacket]) { + self.packets.append( + &mut packets + .iter() + .copied() + .filter(|p| p.to_string().contains(self.filter.value())) + .collect::>(), + ); + } + pub fn enable(&mut self) { self.enabled = true; }