Compute colors in parallel

This commit is contained in:
Tavian Barnes 2022-10-31 11:52:23 -04:00 committed by David Peter
parent b2c8888a50
commit 17d849df6c
5 changed files with 28 additions and 3 deletions

View File

@ -19,6 +19,7 @@
- No leading `./` prefix for non-interactive results, see above.
- fd can now avoid `stat` syscalls even when colorizing paths, as long as the color scheme doesn't
require metadata, see #1148 (@tavianator)
- fd now colorizes paths in parallel, significantly improving performance, see #1148 (@tavianator)
## Other

View File

@ -120,3 +120,10 @@ pub struct Config {
/// Whether or not to strip the './' prefix for search results
pub strip_cwd_prefix: bool,
}
impl Config {
/// Check whether results are being printed.
pub fn is_printing(&self) -> bool {
self.command.is_none()
}
}

View File

@ -2,7 +2,8 @@ use std::ffi::OsString;
use std::fs::{FileType, Metadata};
use std::path::{Path, PathBuf};
use lscolors::Colorable;
use lscolors::{Colorable, LsColors, Style};
use once_cell::unsync::OnceCell;
use crate::config::Config;
@ -16,6 +17,7 @@ enum DirEntryInner {
pub struct DirEntry {
inner: DirEntryInner,
metadata: OnceCell<Option<Metadata>>,
style: OnceCell<Option<Style>>,
}
impl DirEntry {
@ -24,6 +26,7 @@ impl DirEntry {
Self {
inner: DirEntryInner::Normal(e),
metadata: OnceCell::new(),
style: OnceCell::new(),
}
}
@ -31,6 +34,7 @@ impl DirEntry {
Self {
inner: DirEntryInner::BrokenSymlink(path),
metadata: OnceCell::new(),
style: OnceCell::new(),
}
}
@ -88,6 +92,12 @@ impl DirEntry {
DirEntryInner::BrokenSymlink(_) => None,
}
}
pub fn style(&self, ls_colors: &LsColors) -> Option<&Style> {
self.style
.get_or_init(|| ls_colors.style_for(self).cloned())
.as_ref()
}
}
impl PartialEq for DirEntry {

View File

@ -90,8 +90,8 @@ fn print_entry_colorized<W: Write>(
write!(stdout, "{}", style.paint(parent_str))?;
}
let style = ls_colors
.style_for(entry)
let style = entry
.style(ls_colors)
.map(Style::to_ansi_term_style)
.unwrap_or_default();
write!(stdout, "{}", style.paint(&path_str[offset..]))?;

View File

@ -535,6 +535,13 @@ fn spawn_senders(
}
}
if config.is_printing() {
if let Some(ls_colors) = &config.ls_colors {
// Try to compute colors in parallel
entry.style(ls_colors);
}
}
let send_result = tx_thread.send(WorkerResult::Entry(entry));
if send_result.is_err() {