perf(channel): avoid unnecessary allocations

This commit is contained in:
Alex Pasmantier 2025-07-25 01:31:40 +02:00
parent a0e3063702
commit a019f18229
5 changed files with 17 additions and 14 deletions

View File

@ -498,7 +498,8 @@ pub fn draw(c: &mut Criterion) {
std::thread::sleep(std::time::Duration::from_millis(10));
}
tv.move_cursor(Movement::Next, 10);
let _ = tv.update_preview_state(&tv.get_selected_entry());
let selected_entry = tv.get_selected_entry();
let _ = tv.update_preview_state(&selected_entry);
let _ = tv.update(&Action::Tick);
(tv, terminal)
},

View File

@ -107,7 +107,7 @@ impl Channel {
entries
}
pub fn get_result(&self, index: u32) -> Option<Entry> {
pub fn get_result(&mut self, index: u32) -> Option<Entry> {
if let Some(item) = self.matcher.get_result(index) {
let mut entry = Entry::new(item.inner.clone())
.with_display(item.matched_string)
@ -177,6 +177,8 @@ impl Channel {
}
}
const DEFAULT_LINE_BUFFER_SIZE: usize = 512;
#[allow(clippy::unused_async)]
async fn load_candidates(
source: SourceSpec,
@ -197,7 +199,7 @@ async fn load_candidates(
if let Some(out) = child.stdout.take() {
let mut produced_output = false;
let mut reader = BufReader::new(out);
let mut buf = Vec::new();
let mut buf = Vec::with_capacity(DEFAULT_LINE_BUFFER_SIZE);
let delimiter = source
.entry_delimiter

View File

@ -155,7 +155,7 @@ impl RemoteControl {
.collect()
}
pub fn get_result(&self, index: u32) -> CableEntry {
pub fn get_result(&mut self, index: u32) -> CableEntry {
let item = self.matcher.get_result(index).expect("Invalid index");
item.inner.with_match_indices(&item.match_indices)
}

View File

@ -226,23 +226,23 @@ where
/// }
/// ```
pub fn get_result(
&self,
&mut self,
index: u32,
) -> Option<matched_item::MatchedItem<I>> {
let snapshot = self.inner.snapshot();
let mut col_indices = Vec::new();
let mut matcher = lazy::MATCHER.lock();
self.col_indices_buffer.clear();
snapshot.get_matched_item(index).map(|item| {
snapshot.pattern().column_pattern(0).indices(
item.matcher_columns[0].slice(..),
&mut matcher,
&mut col_indices,
&mut self.col_indices_buffer,
);
col_indices.sort_unstable();
col_indices.dedup();
self.col_indices_buffer.sort_unstable();
self.col_indices_buffer.dedup();
let indices = col_indices.drain(..);
let indices = self.col_indices_buffer.drain(..);
let matched_string = item.matcher_columns[0].to_string();
matched_item::MatchedItem {

View File

@ -389,7 +389,7 @@ impl Television {
}
}
pub fn get_selected_entry(&self) -> Option<Entry> {
pub fn get_selected_entry(&mut self) -> Option<Entry> {
if self.channel.result_count() == 0 {
return None;
}
@ -398,7 +398,7 @@ impl Television {
.and_then(|entry| entry)
}
pub fn get_selected_cable_entry(&self) -> Option<CableEntry> {
pub fn get_selected_cable_entry(&mut self) -> Option<CableEntry> {
if self
.remote_control
.as_ref()
@ -408,7 +408,7 @@ impl Television {
return None;
}
self.selected_index().and_then(|idx| {
self.remote_control.as_ref().map(|rc| rc.get_result(idx))
self.remote_control.as_mut().map(|rc| rc.get_result(idx))
})
}
@ -423,7 +423,7 @@ impl Television {
}
#[must_use]
pub fn get_selected_entries(&self) -> Option<FxHashSet<Entry>> {
pub fn get_selected_entries(&mut self) -> Option<FxHashSet<Entry>> {
// if nothing is selected, return the currently hovered entry
if self.channel.selected_entries().is_empty() {
return self