perf: remove unnecessary clone() calls (#83)

perf: remove redundant clone() calls
This commit is contained in:
Bertrand Chardon 2024-11-27 23:52:35 +01:00 committed by Alexandre Pasmantier
parent 30f1940815
commit 4bea114635
13 changed files with 29 additions and 31 deletions

View File

@ -85,7 +85,7 @@ impl OnAir for Channel {
let mut entry =
Entry::new(item.inner.name.clone(), PreviewType::EnvVar)
.with_value(item.inner.value.clone())
.with_value(item.inner.value)
.with_icon(self.file_icon);
if should_add_name_indices {
@ -111,7 +111,7 @@ impl OnAir for Channel {
fn get_result(&self, index: u32) -> Option<Entry> {
self.matcher.get_result(index).map(|item| {
Entry::new(item.inner.name.clone(), PreviewType::EnvVar)
.with_value(item.inner.value.clone())
.with_value(item.inner.value)
.with_icon(self.file_icon)
})
}

View File

@ -65,7 +65,7 @@ impl OnAir for Channel {
let mut entry =
Entry::new(item.inner.name.clone(), PreviewType::EnvVar)
.with_value(item.inner.value.clone())
.with_value(item.inner.value)
.with_icon(self.file_icon);
if should_add_name_indices {
@ -91,7 +91,7 @@ impl OnAir for Channel {
fn get_result(&self, index: u32) -> Option<Entry> {
self.matcher.get_result(index).map(|item| {
Entry::new(item.inner.name.clone(), PreviewType::EnvVar)
.with_value(item.inner.value.clone())
.with_value(item.inner.value)
.with_icon(self.file_icon)
})
}

View File

@ -50,7 +50,7 @@ impl OnAir for Channel {
.into_iter()
.map(|item| {
let path = item.matched_string;
Entry::new(path.clone(), PreviewType::Directory)
Entry::new(path, PreviewType::Directory)
.with_name_match_ranges(item.match_indices)
.with_icon(self.icon)
})
@ -60,8 +60,7 @@ impl OnAir for Channel {
fn get_result(&self, index: u32) -> Option<Entry> {
self.matcher.get_result(index).map(|item| {
let path = item.matched_string;
Entry::new(path.clone(), PreviewType::Directory)
.with_icon(self.icon)
Entry::new(path, PreviewType::Directory).with_icon(self.icon)
})
}

View File

@ -57,7 +57,7 @@ impl OnAir for RemoteControl {
.into_iter()
.map(|item| {
let path = item.matched_string;
Entry::new(path.clone(), PreviewType::Basic)
Entry::new(path, PreviewType::Basic)
.with_name_match_ranges(item.match_indices)
.with_icon(TV_ICON)
})
@ -67,7 +67,7 @@ impl OnAir for RemoteControl {
fn get_result(&self, index: u32) -> Option<Entry> {
self.matcher.get_result(index).map(|item| {
let path = item.matched_string;
Entry::new(path.clone(), PreviewType::Basic).with_icon(TV_ICON)
Entry::new(path, PreviewType::Basic).with_icon(TV_ICON)
})
}

View File

@ -168,7 +168,7 @@ impl OnAir for Channel {
let line = item.matched_string;
let display_path =
item.inner.path.to_string_lossy().to_string();
Entry::new(display_path.clone(), PreviewType::Files)
Entry::new(display_path, PreviewType::Files)
.with_value(line)
.with_value_match_ranges(item.match_indices)
.with_icon(FileIcon::from(item.inner.path.as_path()))
@ -180,7 +180,7 @@ impl OnAir for Channel {
fn get_result(&self, index: u32) -> Option<Entry> {
self.matcher.get_result(index).map(|item| {
let display_path = item.inner.path.to_string_lossy().to_string();
Entry::new(display_path.clone(), PreviewType::Files)
Entry::new(display_path, PreviewType::Files)
.with_icon(FileIcon::from(item.inner.path.as_path()))
.with_line_number(item.inner.line_number)
})

View File

@ -134,7 +134,7 @@ impl PreviewCache {
/// If the key is already in the cache, the preview will be updated.
pub fn insert(&mut self, key: String, preview: Arc<Preview>) {
debug!("Inserting preview into cache: {}", key);
self.entries.insert(key.clone(), preview.clone());
self.entries.insert(key.clone(), preview);
if let Some(oldest_key) = self.ring_set.push(key) {
debug!("Cache full, removing oldest entry: {}", oldest_key);
self.entries.remove(&oldest_key);

View File

@ -40,7 +40,7 @@ impl DirectoryPreviewer {
let cache = self.cache.clone();
tokio::spawn(async move {
let preview = Arc::new(build_tree_preview(&entry_c));
cache.lock().insert(entry_c.name.clone(), preview.clone());
cache.lock().insert(entry_c.name.clone(), preview);
});
preview
}

View File

@ -86,7 +86,7 @@ impl FilePreviewer {
// do we have a preview in cache for that entry?
if let Some(preview) = self.cache.lock().get(&entry.name) {
return preview.clone();
return preview;
}
debug!("No preview in cache for {:?}", entry.name);
@ -99,7 +99,7 @@ impl FilePreviewer {
let entry_c = entry.clone();
let syntax_set = self.syntax_set.clone();
let syntax_theme = self.syntax_theme.clone();
let path = path_buf.clone();
let path = path_buf;
let concurrent_tasks = self.concurrent_preview_tasks.clone();
let last_previewed = self.last_previewed.clone();
tokio::spawn(async move {
@ -161,7 +161,7 @@ pub fn try_preview(
if get_file_size(&path).map_or(false, |s| s > MAX_FILE_SIZE) {
debug!("File too large: {:?}", entry.name);
let preview = meta::file_too_large(&entry.name);
cache.lock().insert(entry.name.clone(), preview.clone());
cache.lock().insert(entry.name.clone(), preview);
}
if matches!(FileType::from(&path), FileType::Text) {
@ -184,13 +184,13 @@ pub fn try_preview(
Err(e) => {
warn!("Error opening file: {:?}", e);
let p = meta::not_supported(&entry.name);
cache.lock().insert(entry.name.clone(), p.clone());
cache.lock().insert(entry.name.clone(), p);
}
}
} else {
debug!("File isn't text-based: {:?}", entry.name);
let preview = meta::not_supported(&entry.name);
cache.lock().insert(entry.name.clone(), preview.clone());
cache.lock().insert(entry.name.clone(), preview);
}
concurrent_tasks.fetch_sub(1, Ordering::Relaxed);
});

View File

@ -140,7 +140,7 @@ impl App {
)?;
debug!("{:?}", keymap);
let television =
Arc::new(Mutex::new(Television::new(channel, config.clone())));
Arc::new(Mutex::new(Television::new(channel, config)));
Ok(Self {
keymap,

View File

@ -46,7 +46,7 @@ pub struct Config {
lazy_static! {
pub static ref PROJECT_NAME: String = String::from("television");
pub static ref PROJECT_NAME_UPPER: String = PROJECT_NAME.to_uppercase().to_string();
pub static ref PROJECT_NAME_UPPER: String = PROJECT_NAME.to_uppercase();
pub static ref DATA_FOLDER: Option<PathBuf> =
// if `TELEVISION_DATA` is set, use that as the data directory
env::var_os(format!("{}_DATA", PROJECT_NAME_UPPER.clone())).map(PathBuf::from).or_else(|| {

View File

@ -18,7 +18,7 @@ pub struct PreviewersConfig {
impl From<PreviewersConfig> for PreviewerConfig {
fn from(val: PreviewersConfig) -> Self {
PreviewerConfig::default()
.file(previewers::FilePreviewerConfig::new(val.file.theme.clone()))
.file(previewers::FilePreviewerConfig::new(val.file.theme))
}
}

View File

@ -152,7 +152,6 @@ async fn poll_event(timeout: Duration) -> bool {
impl EventLoop {
pub fn new(tick_rate: f64, init: bool) -> Self {
let (tx, rx) = mpsc::unbounded_channel();
let tx_c = tx.clone();
let tick_interval = Duration::from_secs_f64(1.0 / tick_rate);
let (abort, mut abort_recv) = mpsc::unbounded_channel();
@ -168,32 +167,32 @@ impl EventLoop {
tokio::select! {
// if we receive a message on the abort channel, stop the event loop
_ = abort_recv.recv() => {
tx_c.send(Event::Closed).unwrap_or_else(|_| warn!("Unable to send Closed event"));
tx_c.send(Event::Tick).unwrap_or_else(|_| warn!("Unable to send Tick event"));
tx.send(Event::Closed).unwrap_or_else(|_| warn!("Unable to send Closed event"));
tx.send(Event::Tick).unwrap_or_else(|_| warn!("Unable to send Tick event"));
break;
},
// if `delay` completes, pass to the next event "frame"
() = delay => {
tx_c.send(Event::Tick).unwrap_or_else(|_| warn!("Unable to send Tick event"));
tx.send(Event::Tick).unwrap_or_else(|_| warn!("Unable to send Tick event"));
},
// if the receiver dropped the channel, stop the event loop
() = tx_c.closed() => break,
() = tx.closed() => break,
// if an event was received, process it
_ = event_available => {
let maybe_event = crossterm::event::read();
match maybe_event {
Ok(crossterm::event::Event::Key(key)) => {
let key = convert_raw_event_to_key(key);
tx_c.send(Event::Input(key)).unwrap_or_else(|_| warn!("Unable to send {:?} event", key));
tx.send(Event::Input(key)).unwrap_or_else(|_| warn!("Unable to send {:?} event", key));
},
Ok(crossterm::event::Event::FocusLost) => {
tx_c.send(Event::FocusLost).unwrap_or_else(|_| warn!("Unable to send FocusLost event"));
tx.send(Event::FocusLost).unwrap_or_else(|_| warn!("Unable to send FocusLost event"));
},
Ok(crossterm::event::Event::FocusGained) => {
tx_c.send(Event::FocusGained).unwrap_or_else(|_| warn!("Unable to send FocusGained event"));
tx.send(Event::FocusGained).unwrap_or_else(|_| warn!("Unable to send FocusGained event"));
},
Ok(crossterm::event::Event::Resize(x, y)) => {
tx_c.send(Event::Resize(x, y)).unwrap_or_else(|_| warn!("Unable to send Resize event"));
tx.send(Event::Resize(x, y)).unwrap_or_else(|_| warn!("Unable to send Resize event"));
},
_ => {}
}

View File

@ -222,7 +222,7 @@ impl Television {
&mut self,
tx: UnboundedSender<Action>,
) -> Result<()> {
self.action_tx = Some(tx.clone());
self.action_tx = Some(tx);
Ok(())
}