Skip to content

Commit 451d48a

Browse files
committed
perf: reuse GraphicalReportHandler
1 parent 5a43307 commit 451d48a

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

crates/rspack_error/src/diagnostic.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use std::{borrow::Cow, fmt, ops::Deref, sync::Arc};
1+
use std::{
2+
borrow::Cow,
3+
fmt,
4+
ops::Deref,
5+
sync::{Arc, LazyLock},
6+
};
27

38
use cow_utils::CowUtils;
49
use miette::{GraphicalTheme, IntoDiagnostic, MietteDiagnostic};
@@ -165,23 +170,29 @@ impl Diagnostic {
165170
}
166171
}
167172

173+
static COLORED_GRAPHICAL_REPORT_HANDLER: LazyLock<GraphicalReportHandler> = LazyLock::new(|| {
174+
GraphicalReportHandler::new()
175+
.with_theme(GraphicalTheme::unicode())
176+
.with_context_lines(2)
177+
.with_width(usize::MAX)
178+
});
179+
180+
static NO_COLOR_GRAPHICAL_REPORT_HANDLER: LazyLock<GraphicalReportHandler> = LazyLock::new(|| {
181+
GraphicalReportHandler::new()
182+
.with_theme(GraphicalTheme::unicode_nocolor())
183+
.with_context_lines(2)
184+
.with_width(usize::MAX)
185+
.without_syntax_highlighting()
186+
});
187+
168188
impl Diagnostic {
169189
pub fn render_report(&self, colored: bool) -> crate::Result<String> {
170190
let mut buf = String::new();
171-
let theme = if colored {
172-
GraphicalTheme::unicode()
173-
} else {
174-
GraphicalTheme::unicode_nocolor()
175-
};
176-
let h = GraphicalReportHandler::new()
177-
.with_theme(theme)
178-
.with_context_lines(2)
179-
.with_width(usize::MAX);
180191

181-
let h = if !colored {
182-
h.without_syntax_highlighting()
192+
let h = if colored {
193+
&COLORED_GRAPHICAL_REPORT_HANDLER
183194
} else {
184-
h
195+
&NO_COLOR_GRAPHICAL_REPORT_HANDLER
185196
};
186197

187198
h.render_report(&mut buf, self.as_ref()).into_diagnostic()?;

0 commit comments

Comments
 (0)