mirror of
https://github.com/alexpasmantier/television.git
synced 2025-07-29 14:21:43 +00:00
perf(channel): avoid unnecessary allocations
This commit is contained in:
parent
a0e3063702
commit
a019f18229
@ -498,7 +498,8 @@ pub fn draw(c: &mut Criterion) {
|
|||||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||||
}
|
}
|
||||||
tv.move_cursor(Movement::Next, 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);
|
let _ = tv.update(&Action::Tick);
|
||||||
(tv, terminal)
|
(tv, terminal)
|
||||||
},
|
},
|
||||||
|
@ -107,7 +107,7 @@ impl Channel {
|
|||||||
entries
|
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) {
|
if let Some(item) = self.matcher.get_result(index) {
|
||||||
let mut entry = Entry::new(item.inner.clone())
|
let mut entry = Entry::new(item.inner.clone())
|
||||||
.with_display(item.matched_string)
|
.with_display(item.matched_string)
|
||||||
@ -177,6 +177,8 @@ impl Channel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DEFAULT_LINE_BUFFER_SIZE: usize = 512;
|
||||||
|
|
||||||
#[allow(clippy::unused_async)]
|
#[allow(clippy::unused_async)]
|
||||||
async fn load_candidates(
|
async fn load_candidates(
|
||||||
source: SourceSpec,
|
source: SourceSpec,
|
||||||
@ -197,7 +199,7 @@ async fn load_candidates(
|
|||||||
if let Some(out) = child.stdout.take() {
|
if let Some(out) = child.stdout.take() {
|
||||||
let mut produced_output = false;
|
let mut produced_output = false;
|
||||||
let mut reader = BufReader::new(out);
|
let mut reader = BufReader::new(out);
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::with_capacity(DEFAULT_LINE_BUFFER_SIZE);
|
||||||
|
|
||||||
let delimiter = source
|
let delimiter = source
|
||||||
.entry_delimiter
|
.entry_delimiter
|
||||||
|
@ -155,7 +155,7 @@ impl RemoteControl {
|
|||||||
.collect()
|
.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");
|
let item = self.matcher.get_result(index).expect("Invalid index");
|
||||||
item.inner.with_match_indices(&item.match_indices)
|
item.inner.with_match_indices(&item.match_indices)
|
||||||
}
|
}
|
||||||
|
@ -226,23 +226,23 @@ where
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn get_result(
|
pub fn get_result(
|
||||||
&self,
|
&mut self,
|
||||||
index: u32,
|
index: u32,
|
||||||
) -> Option<matched_item::MatchedItem<I>> {
|
) -> Option<matched_item::MatchedItem<I>> {
|
||||||
let snapshot = self.inner.snapshot();
|
let snapshot = self.inner.snapshot();
|
||||||
let mut col_indices = Vec::new();
|
|
||||||
let mut matcher = lazy::MATCHER.lock();
|
let mut matcher = lazy::MATCHER.lock();
|
||||||
|
self.col_indices_buffer.clear();
|
||||||
|
|
||||||
snapshot.get_matched_item(index).map(|item| {
|
snapshot.get_matched_item(index).map(|item| {
|
||||||
snapshot.pattern().column_pattern(0).indices(
|
snapshot.pattern().column_pattern(0).indices(
|
||||||
item.matcher_columns[0].slice(..),
|
item.matcher_columns[0].slice(..),
|
||||||
&mut matcher,
|
&mut matcher,
|
||||||
&mut col_indices,
|
&mut self.col_indices_buffer,
|
||||||
);
|
);
|
||||||
col_indices.sort_unstable();
|
self.col_indices_buffer.sort_unstable();
|
||||||
col_indices.dedup();
|
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();
|
let matched_string = item.matcher_columns[0].to_string();
|
||||||
|
|
||||||
matched_item::MatchedItem {
|
matched_item::MatchedItem {
|
||||||
|
@ -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 {
|
if self.channel.result_count() == 0 {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
@ -398,7 +398,7 @@ impl Television {
|
|||||||
.and_then(|entry| entry)
|
.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
|
if self
|
||||||
.remote_control
|
.remote_control
|
||||||
.as_ref()
|
.as_ref()
|
||||||
@ -408,7 +408,7 @@ impl Television {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
self.selected_index().and_then(|idx| {
|
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]
|
#[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 nothing is selected, return the currently hovered entry
|
||||||
if self.channel.selected_entries().is_empty() {
|
if self.channel.selected_entries().is_empty() {
|
||||||
return self
|
return self
|
||||||
|
Loading…
x
Reference in New Issue
Block a user