Skip to content

Commit

Permalink
fix high cpu when fuzzy search is used
Browse files Browse the repository at this point in the history
  • Loading branch information
pythops committed Sep 15, 2024
1 parent 68eab7c commit e19c7a4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
18 changes: 14 additions & 4 deletions oryx-tui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions oryx-tui/src/filters/fuzzy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ impl Fuzzy {
.collect::<Vec<AppPacket>>();
}

pub fn append(&mut self, packets: &[AppPacket]) {
self.packets.append(
&mut packets
.iter()
.copied()
.filter(|p| p.to_string().contains(self.filter.value()))
.collect::<Vec<AppPacket>>(),
);
}

pub fn enable(&mut self) {
self.enabled = true;
}
Expand Down

0 comments on commit e19c7a4

Please sign in to comment.