mirror of
https://github.com/aphrody-labs/oxc.git
synced 2026-10-15 06:39:17 +00:00
perf(diagnostics): optimize graphical number rendering (#25715)
Use direct integer formatting for plain graphical snippet coordinates and line numbers. Criterion shows 4-14% faster graphical rendering across the covered cases. Created with AI assistance. <sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>
This commit is contained in:
1 parent
71528342ee
commit
f62ed0e2b9
4 files changed
+71
-3
No files matched your search
Generated
+1
@@ -2002,6 +2002,7 @@ version = "0.144.0"
|
||||
dependencies = [
|
||||
"bytecount",
|
||||
"cow-utils",
|
||||
"itoa",
|
||||
"memchr",
|
||||
"owo-colors",
|
||||
"oxc_span",
|
||||
|
||||
@@ -21,6 +21,7 @@ oxc_span = { workspace = true }
|
||||
|
||||
bytecount = { workspace = true }
|
||||
cow-utils = { workspace = true }
|
||||
itoa = { workspace = true }
|
||||
memchr = { workspace = true }
|
||||
owo-colors = { workspace = true }
|
||||
percent-encoding = { workspace = true }
|
||||
|
||||
@@ -26,7 +26,10 @@ impl GraphicalReportHandler {
|
||||
) -> fmt::Result {
|
||||
f.write_char(' ')?;
|
||||
if self.theme.styles.linum.is_plain() {
|
||||
write!(f, "{linum:width$}")?;
|
||||
let mut buffer = itoa::Buffer::new();
|
||||
let linum = buffer.format(linum);
|
||||
write_repeated_char(f, ' ', width.saturating_sub(linum.len()))?;
|
||||
f.write_str(linum)?;
|
||||
} else {
|
||||
write!(f, "{:width$}", linum.style(self.theme.styles.linum), width = width)?;
|
||||
}
|
||||
@@ -186,3 +189,19 @@ impl GraphicalReportHandler {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::GraphicalTheme;
|
||||
|
||||
#[test]
|
||||
fn direct_plain_line_numbers_match_standard_formatting() {
|
||||
let handler = GraphicalReportHandler::new_themed(GraphicalTheme::none());
|
||||
for (width, linum) in [(3, 1), (1, usize::MAX)] {
|
||||
let mut actual = String::new();
|
||||
handler.write_linum(&mut actual, width, linum).unwrap();
|
||||
assert_eq!(actual, format!(" {linum:width$} | "));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -151,7 +151,12 @@ impl GraphicalReportHandler {
|
||||
Some(source_name) => {
|
||||
let style = self.theme.styles.link;
|
||||
if style.is_plain() {
|
||||
writeln!(f, "[{source_name}:{}:{}]", primary_line + 1, primary_column + 1)?;
|
||||
Self::write_location(
|
||||
f,
|
||||
Some(source_name),
|
||||
primary_line + 1,
|
||||
primary_column + 1,
|
||||
)?;
|
||||
} else {
|
||||
let source_name = source_name.style(style);
|
||||
writeln!(f, "[{}:{}:{}]", source_name, primary_line + 1, primary_column + 1)?;
|
||||
@@ -162,7 +167,7 @@ impl GraphicalReportHandler {
|
||||
write_repeated_char(f, self.theme.characters.hbar, 3)?;
|
||||
f.write_char('\n')?;
|
||||
} else {
|
||||
writeln!(f, "[{}:{}]", primary_line + 1, primary_column + 1)?;
|
||||
Self::write_location(f, None, primary_line + 1, primary_column + 1)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -207,6 +212,28 @@ impl GraphicalReportHandler {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_location(
|
||||
f: &mut impl fmt::Write,
|
||||
source_name: Option<&str>,
|
||||
line: usize,
|
||||
column: usize,
|
||||
) -> fmt::Result {
|
||||
let mut line_buffer = itoa::Buffer::new();
|
||||
let mut column_buffer = itoa::Buffer::new();
|
||||
let line = line_buffer.format(line);
|
||||
let column = column_buffer.format(column);
|
||||
|
||||
f.write_char('[')?;
|
||||
if let Some(source_name) = source_name {
|
||||
f.write_str(source_name)?;
|
||||
f.write_char(':')?;
|
||||
}
|
||||
f.write_str(line)?;
|
||||
f.write_char(':')?;
|
||||
f.write_str(column)?;
|
||||
f.write_str("]\n")
|
||||
}
|
||||
|
||||
/// Renders a line to the output formatter, replacing tabs with spaces.
|
||||
pub(super) fn render_line_text(f: &mut impl fmt::Write, text: &str) -> fmt::Result {
|
||||
if !text.contains('\t') {
|
||||
@@ -227,3 +254,23 @@ impl GraphicalReportHandler {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::GraphicalReportHandler;
|
||||
|
||||
#[test]
|
||||
fn direct_locations_match_standard_formatting() {
|
||||
for (source_name, line, column) in
|
||||
[(None, 1, 1), (Some("src/火.ts"), usize::MAX, usize::MAX)]
|
||||
{
|
||||
let expected = source_name.map_or_else(
|
||||
|| format!("[{line}:{column}]\n"),
|
||||
|source_name| format!("[{source_name}:{line}:{column}]\n"),
|
||||
);
|
||||
let mut actual = String::new();
|
||||
GraphicalReportHandler::write_location(&mut actual, source_name, line, column).unwrap();
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user