Skip to content

Commit

Permalink
feat: avoid clone() reallocating memory
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Apr 29, 2024
1 parent 0f02b9e commit 1091b0c
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions yazi-core/src/which/commands/show.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashSet, str::FromStr};

use yazi_config::{keymap::{Control, ControlCow, Key}, KEYMAP};
use yazi_config::{keymap::{Control, Key}, KEYMAP};
use yazi_shared::{event::Cmd, render, Layer};

use crate::which::{Which, WhichSorter};
Expand Down Expand Up @@ -43,23 +43,21 @@ impl Which {
}

pub fn show_with(&mut self, key: &Key, layer: Layer) {
let mut seen = HashSet::new();

self.layer = layer;
self.times = 1;
self.cands = keymap_candidates(layer, key);
self.cands = KEYMAP
.get(layer)
.iter()
.filter(|c| c.on.len() > 1 && &c.on[0] == key)
.filter(|&c| seen.insert(&c.on))
.map(|c| c.into())
.collect();

WhichSorter::default().sort(&mut self.cands);
self.visible = true;
self.silent = false;
render!();
}
}

fn keymap_candidates(layer: Layer, key: &Key) -> Vec<ControlCow> {
let mut seen = HashSet::new();

let mut matches =
KEYMAP.get(layer).iter().filter(|c| c.on.len() > 1 && &c.on[0] == key).collect::<Vec<_>>();

matches.retain(|c| seen.insert(c.on.clone()));
matches.into_iter().map(|c| c.into()).collect()
}

0 comments on commit 1091b0c

Please sign in to comment.