Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: only scan the first 1024 bytes to detect if it's binary, apply \r fixes only to content within the visible range, avoid unnecessary allocations during natural sorting #1551

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ parking_lot = "0.12.3"
ratatui = { version = "0.27.0", features = [ "unstable-rendered-line-info" ] }
regex = "1.10.6"
scopeguard = "1.2.0"
serde = { version = "1.0.208", features = [ "derive" ] }
serde_json = "1.0.125"
serde = { version = "1.0.209", features = [ "derive" ] }
serde_json = "1.0.127"
shell-words = "1.1.0"
tokio = { version = "1.39.3", features = [ "full" ] }
tokio-stream = "0.1.15"
Expand Down
24 changes: 12 additions & 12 deletions yazi-fs/src/sorter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,25 @@ impl FilesSorter {
}

fn sort_naturally(&self, items: &mut Vec<File>) {
let mut indices = Vec::with_capacity(items.len());
let mut entities = Vec::with_capacity(items.len());
for (i, file) in items.iter().enumerate() {
indices.push(i);
entities.push(file.url.as_os_str().as_encoded_bytes());
}

let mut indices: Vec<usize> = (0..items.len()).collect();
indices.sort_unstable_by(|&a, &b| {
let promote = self.promote(&items[a], &items[b]);
let (a, b) = (&items[a], &items[b]);

let promote = self.promote(a, b);
if promote != Ordering::Equal {
return promote;
}

let ordering = if !self.translit {
natsort(entities[a], entities[b], !self.sensitive)
let ordering = if self.translit {
natsort(
a.url.as_os_str().as_encoded_bytes().transliterate().as_bytes(),
b.url.as_os_str().as_encoded_bytes().transliterate().as_bytes(),
!self.sensitive,
)
} else {
natsort(
entities[a].transliterate().as_bytes(),
entities[b].transliterate().as_bytes(),
a.url.as_os_str().as_encoded_bytes(),
b.url.as_os_str().as_encoded_bytes(),
!self.sensitive,
)
};
Expand Down
129 changes: 71 additions & 58 deletions yazi-plugin/src/external/highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,8 @@ impl Highlighter {
(&r.0, &r.1)
}

async fn find_syntax(path: &Path) -> Result<&'static SyntaxReference> {
let (_, syntaxes) = Self::init().await;
let name = path.file_name().map(|n| n.to_string_lossy()).unwrap_or_default();
if let Some(s) = syntaxes.find_syntax_by_extension(&name) {
return Ok(s);
}

let ext = path.extension().map(|e| e.to_string_lossy()).unwrap_or_default();
if let Some(s) = syntaxes.find_syntax_by_extension(&ext) {
return Ok(s);
}

let mut line = String::new();
let mut reader = BufReader::new(File::open(&path).await?);
reader.read_line(&mut line).await?;
syntaxes.find_syntax_by_first_line(&line).ok_or_else(|| anyhow!("No syntax found"))
}
#[inline]
pub fn abort() { INCR.fetch_add(1, Ordering::Relaxed); }

pub async fn highlight(&self, skip: usize, limit: usize) -> Result<Text<'static>, PeekError> {
let mut reader = BufReader::new(File::open(&self.path).await?);
Expand All @@ -67,10 +52,13 @@ impl Highlighter {

let mut i = 0;
let mut buf = vec![];
let mut inspected = 0u16;
while reader.read_until(b'\n', &mut buf).await.is_ok() {
i += 1;
if buf.is_empty() || i > skip + limit {
break;
} else if Self::is_binary(&buf, &mut inspected) {
return Err("Binary file".into());
}

if !plain && (buf.len() > 5000 || buf.contains(&0x1b)) {
Expand All @@ -84,15 +72,8 @@ impl Highlighter {
buf.push(b'\n');
}

for b in &mut buf {
match *b {
b'\0' => return Err("Binary file".into()),
b'\r' => *b = b'\n', // '\r' occurs in the middle of a line
_ => {}
}
}

if i > skip {
buf.iter_mut().for_each(Self::carriage_return_to_line_feed);
after.push(String::from_utf8_lossy(&buf).into_owned());
} else if !plain {
before.push(String::from_utf8_lossy(&buf).into_owned());
Expand Down Expand Up @@ -144,13 +125,75 @@ impl Highlighter {
.await?
}

#[inline]
pub fn abort() { INCR.fetch_add(1, Ordering::Relaxed); }
async fn find_syntax(path: &Path) -> Result<&'static SyntaxReference> {
let (_, syntaxes) = Self::init().await;
let name = path.file_name().map(|n| n.to_string_lossy()).unwrap_or_default();
if let Some(s) = syntaxes.find_syntax_by_extension(&name) {
return Ok(s);
}

let ext = path.extension().map(|e| e.to_string_lossy()).unwrap_or_default();
if let Some(s) = syntaxes.find_syntax_by_extension(&ext) {
return Ok(s);
}

let mut line = String::new();
let mut reader = BufReader::new(File::open(&path).await?);
reader.read_line(&mut line).await?;
syntaxes.find_syntax_by_first_line(&line).ok_or_else(|| anyhow!("No syntax found"))
}

#[inline(always)]
fn is_binary(buf: &[u8], inspected: &mut u16) -> bool {
if let Some(n) = 1024u16.checked_sub(*inspected) {
*inspected += n.min(buf.len() as u16);
buf.iter().take(n as usize).any(|&b| b == 0)
} else {
false
}
}

#[inline(always)]
fn carriage_return_to_line_feed(c: &mut u8) {
if *c == b'\r' {
*c = b'\n';
}
}
}

impl Highlighter {
pub fn to_line_widget(regions: Vec<(highlighting::Style, &str)>, indent: &str) -> Line<'static> {
let spans: Vec<_> = regions
.into_iter()
.map(|(style, s)| {
let mut modifier = ratatui::style::Modifier::empty();
if style.font_style.contains(highlighting::FontStyle::BOLD) {
modifier |= ratatui::style::Modifier::BOLD;
}
if style.font_style.contains(highlighting::FontStyle::ITALIC) {
modifier |= ratatui::style::Modifier::ITALIC;
}
if style.font_style.contains(highlighting::FontStyle::UNDERLINE) {
modifier |= ratatui::style::Modifier::UNDERLINED;
}

Span {
content: s.replace('\t', indent).into(),
style: ratatui::style::Style {
fg: Self::to_ansi_color(style.foreground),
// bg: Self::to_ansi_color(style.background),
add_modifier: modifier,
..Default::default()
},
}
})
.collect();

Line::from(spans)
}

// Copy from https://github.com/sharkdp/bat/blob/master/src/terminal.rs
pub fn to_ansi_color(color: highlighting::Color) -> Option<ratatui::style::Color> {
fn to_ansi_color(color: highlighting::Color) -> Option<ratatui::style::Color> {
if color.a == 0 {
// Themes can specify one of the user-configurable terminal colors by
// encoding them as #RRGGBBAA with AA set to 00 (transparent) and RR set
Expand Down Expand Up @@ -188,34 +231,4 @@ impl Highlighter {
Some(ratatui::style::Color::Rgb(color.r, color.g, color.b))
}
}

pub fn to_line_widget(regions: Vec<(highlighting::Style, &str)>, indent: &str) -> Line<'static> {
let spans: Vec<_> = regions
.into_iter()
.map(|(style, s)| {
let mut modifier = ratatui::style::Modifier::empty();
if style.font_style.contains(highlighting::FontStyle::BOLD) {
modifier |= ratatui::style::Modifier::BOLD;
}
if style.font_style.contains(highlighting::FontStyle::ITALIC) {
modifier |= ratatui::style::Modifier::ITALIC;
}
if style.font_style.contains(highlighting::FontStyle::UNDERLINE) {
modifier |= ratatui::style::Modifier::UNDERLINED;
}

Span {
content: s.replace('\t', indent).into(),
style: ratatui::style::Style {
fg: Self::to_ansi_color(style.foreground),
// bg: Self::to_ansi_color(style.background),
add_modifier: modifier,
..Default::default()
},
}
})
.collect();

Line::from(spans)
}
}