Skip to content

Commit 1d4c314

Browse files
committed
Don't re-parse terminfo (twice!) on every compiler diagnostic
Stuff the term::Terminal into TLS to avoid re-parsing for every single message we want to color. Fixes #6827.
1 parent 69da380 commit 1d4c314

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/libsyntax/diagnostic.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use codemap;
1313

1414
use std::io;
1515
use std::uint;
16+
use std::local_data;
1617
use extra::term;
1718

1819
pub type Emitter = @fn(cmsp: Option<(@codemap::CodeMap, span)>,
@@ -187,13 +188,29 @@ fn diagnosticcolor(lvl: level) -> term::color::Color {
187188
}
188189

189190
fn print_maybe_styled(msg: &str, color: term::attr::Attr) {
191+
#[cfg(not(stage0))]
192+
static tls_terminal: local_data::Key<@Option<term::Terminal>> = &local_data::Key;
193+
#[cfg(stage0)]
194+
fn tls_terminal(_: @Option<term::Terminal>) {}
195+
190196
let stderr = io::stderr();
191197

192198
if stderr.get_type() == io::Screen {
193-
let t = term::Terminal::new(stderr);
199+
let t = match local_data::get(tls_terminal, |v| v.map_consume(|&k|k)) {
200+
None => {
201+
let t = term::Terminal::new(stderr);
202+
let tls = @match t {
203+
Ok(t) => Some(t),
204+
Err(_) => None
205+
};
206+
local_data::set(tls_terminal, tls);
207+
&*tls
208+
}
209+
Some(tls) => &*tls
210+
};
194211

195212
match t {
196-
Ok(term) => {
213+
&Some(ref term) => {
197214
term.attr(color);
198215
stderr.write_str(msg);
199216
term.reset();

0 commit comments

Comments
 (0)