From 1d4c3146f5e35ce60db73849da8806d73c6ecee2 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Thu, 11 Jul 2013 00:56:26 -0700 Subject: [PATCH] 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. --- src/libsyntax/diagnostic.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/libsyntax/diagnostic.rs b/src/libsyntax/diagnostic.rs index fc0dc4c8f52..2971ad5cc29 100644 --- a/src/libsyntax/diagnostic.rs +++ b/src/libsyntax/diagnostic.rs @@ -13,6 +13,7 @@ use codemap; use std::io; use std::uint; +use std::local_data; use extra::term; pub type Emitter = @fn(cmsp: Option<(@codemap::CodeMap, span)>, @@ -187,13 +188,29 @@ fn diagnosticcolor(lvl: level) -> term::color::Color { } fn print_maybe_styled(msg: &str, color: term::attr::Attr) { + #[cfg(not(stage0))] + static tls_terminal: local_data::Key<@Option> = &local_data::Key; + #[cfg(stage0)] + fn tls_terminal(_: @Option) {} + let stderr = io::stderr(); if stderr.get_type() == io::Screen { - let t = term::Terminal::new(stderr); + let t = match local_data::get(tls_terminal, |v| v.map_consume(|&k|k)) { + None => { + let t = term::Terminal::new(stderr); + let tls = @match t { + Ok(t) => Some(t), + Err(_) => None + }; + local_data::set(tls_terminal, tls); + &*tls + } + Some(tls) => &*tls + }; match t { - Ok(term) => { + &Some(ref term) => { term.attr(color); stderr.write_str(msg); term.reset();