From 7ac4154643a952fed1d942adcb78e27b17ad41dd Mon Sep 17 00:00:00 2001 From: Markus Westerlind Date: Mon, 17 Feb 2020 08:09:34 +0100 Subject: [PATCH 1/2] perf: Buffer stderr when writing json errors/warnings Since `stderr` is unbuffered, writing out json messages actually take up about ~10%/0.1s of the runtime of the `inflate` benchmark. cc #64413 --- src/librustc_errors/json.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_errors/json.rs b/src/librustc_errors/json.rs index ffdff6acec5d..f0cfd815a2c3 100644 --- a/src/librustc_errors/json.rs +++ b/src/librustc_errors/json.rs @@ -48,7 +48,7 @@ impl JsonEmitter { macro_backtrace: bool, ) -> JsonEmitter { JsonEmitter { - dst: Box::new(io::stderr()), + dst: Box::new(io::BufWriter::new(io::stderr())), registry, sm: source_map, pretty, From ee064befa06ad54193786c9169672f9349d0bfaf Mon Sep 17 00:00:00 2001 From: Markus Westerlind Date: Mon, 17 Feb 2020 16:46:51 +0100 Subject: [PATCH 2/2] Ensure diagnostics are printed in the correct order Even when buffered. Ideally we would flush only when the emitter is done, but that requires larger changes. This gives most of the benefit of buffering in any case. --- src/librustc_errors/json.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/librustc_errors/json.rs b/src/librustc_errors/json.rs index f0cfd815a2c3..97b9ca924bb7 100644 --- a/src/librustc_errors/json.rs +++ b/src/librustc_errors/json.rs @@ -104,7 +104,8 @@ impl Emitter for JsonEmitter { writeln!(&mut self.dst, "{}", as_pretty_json(&data)) } else { writeln!(&mut self.dst, "{}", as_json(&data)) - }; + } + .and_then(|_| self.dst.flush()); if let Err(e) = result { panic!("failed to print diagnostics: {:?}", e); } @@ -116,7 +117,8 @@ impl Emitter for JsonEmitter { writeln!(&mut self.dst, "{}", as_pretty_json(&data)) } else { writeln!(&mut self.dst, "{}", as_json(&data)) - }; + } + .and_then(|_| self.dst.flush()); if let Err(e) = result { panic!("failed to print notification: {:?}", e); }