Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallel formatting to increase speed #6095

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Comments, reorder, rename
  • Loading branch information
MarcusGrass committed Feb 26, 2024
commit 81fa030baf209a8b02c31027c06d88cdee09f0ec
4 changes: 2 additions & 2 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ fn format_string(input: String, options: GetOptsOptions) -> Result<i32> {
let printer = Printer::new(config.color());
let mut session = Session::new(config, Some(out), &printer);
format_and_emit_report(&mut session, Input::Text(input));
printer.dump()?;
printer.write_to_outputs()?;

let exit_code = if session.has_operational_errors() || session.has_parsing_errors() {
1
Expand Down Expand Up @@ -488,7 +488,7 @@ fn join_thread_reporting_back(
if !output.is_empty() {
stdout().write_all(&output).unwrap();
}
threaded_file_output.printer.dump()?;
threaded_file_output.printer.write_to_outputs()?;
Ok(threaded_file_output.exit_code)
}

Expand Down
133 changes: 59 additions & 74 deletions src/print.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,17 @@
use crate::Color;
use rustc_errors::{Color as RustColor, ColorSpec, WriteColor};
use std::fmt::Formatter;
use std::io::{stderr, stdout, Write};
use std::sync::{Arc, Mutex};
use termcolor::{ColorChoice, StandardStream, WriteColor as _};

#[derive(Clone)]
pub struct Printer {
// Needs `Mutex` to be UnwindSafe, although, this should be
// safe to `Unwind` without it.
// Needs `Arc` to pass the boundary over to `rustc_error`.
inner: Arc<Mutex<PrinterInner>>,
}

impl Write for Printer {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let mut inner = self.inner.lock().unwrap();
let col = inner.current_color.clone();
inner
.messages
.push(PrintMessage::RustcErrTerm(RustcErrTermMessage::new(
buf.to_vec(),
col,
)));
Ok(buf.len())
}

#[inline]
fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> {
self.write(buf)?;
Ok(())
}

#[inline]
fn flush(&mut self) -> std::io::Result<()> {
//println!("Flush");
Ok(())
}
}

impl WriteColor for Printer {
#[inline]
fn supports_color(&self) -> bool {
self.inner.lock().unwrap().supports_color
}

#[inline]
fn set_color(&mut self, spec: &ColorSpec) -> std::io::Result<()> {
self.inner.lock().unwrap().current_color = Some(spec.clone());
Ok(())
}

#[inline]
fn reset(&mut self) -> std::io::Result<()> {
self.inner.lock().unwrap().current_color.take();
Ok(())
}
}

struct PrinterInner {
color_setting: Color,
current_color: Option<ColorSpec>,
Expand Down Expand Up @@ -83,14 +40,19 @@ impl Printer {
self.inner.lock().unwrap().messages.push(msg);
}

pub fn dump(&self) -> Result<(), std::io::Error> {
/// Writes stored messages to respective outputs in order.
pub fn write_to_outputs(&self) -> Result<(), std::io::Error> {
let inner = self.inner.lock().unwrap();
// Pretty common case, early exit
if inner.messages.is_empty() {
return Ok(());
}

// Stdout term, from diffs
let mut use_term_stdout =
term::stdout().filter(|t| inner.color_setting.use_colored_tty() && t.supports_color());

// `rustc_error` diagnostics, compilation errors.
let use_rustc_error_color = inner.color_setting.use_colored_tty()
&& term::stderr()
.map(|t| t.supports_color())
Expand Down Expand Up @@ -140,6 +102,52 @@ impl Printer {
}
}

/// Trait evoked by `rustc_error`s `EmitterWriter` (`HumanEmitter` on main) to print
/// compilation errors and diffs.
impl Write for Printer {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let mut inner = self.inner.lock().unwrap();
let col = inner.current_color.clone();
inner
.messages
.push(PrintMessage::RustcErrTerm(RustcErrTermMessage::new(
buf.to_vec(),
col,
)));
Ok(buf.len())
}

#[inline]
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}

#[inline]
fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> {
self.write(buf)?;
Ok(())
}
}

impl WriteColor for Printer {
#[inline]
fn supports_color(&self) -> bool {
self.inner.lock().unwrap().supports_color
}

#[inline]
fn set_color(&mut self, spec: &ColorSpec) -> std::io::Result<()> {
self.inner.lock().unwrap().current_color = Some(spec.clone());
Ok(())
}

#[inline]
fn reset(&mut self) -> std::io::Result<()> {
self.inner.lock().unwrap().current_color.take();
Ok(())
}
}

// Rustc vendors termcolor, but not everything needed to use it,
// as far as I can tell
fn rustc_colorspec_compat(rustc: &ColorSpec) -> termcolor::ColorSpec {
Expand All @@ -149,10 +157,12 @@ fn rustc_colorspec_compat(rustc: &ColorSpec) -> termcolor::ColorSpec {
let bg = rustc.bg().and_then(rustc_color_compat);
cs.set_bg(bg);
cs.set_bold(rustc.bold());
cs.set_strikethrough(rustc.strikethrough());
cs.set_underline(rustc.underline());
cs.set_intense(rustc.intense());
cs.set_underline(rustc.underline());
cs.set_dimmed(rustc.dimmed());
cs.set_italic(rustc.italic());
cs.set_reset(rustc.reset());
cs.set_strikethrough(rustc.strikethrough());
cs
}

Expand Down Expand Up @@ -218,31 +228,6 @@ pub enum PrintMessage {
RustcErrTerm(RustcErrTermMessage),
}

impl std::fmt::Debug for PrintMessage {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
PrintMessage::Stdout(buf) => f.write_fmt(format_args!(
"PrintMessage::Stdout({:?})",
core::str::from_utf8(buf)
)),
PrintMessage::StdErr(buf) => f.write_fmt(format_args!(
"PrintMessage::Stderr({:?})",
core::str::from_utf8(buf)
)),
PrintMessage::Term(msg) => f.write_fmt(format_args!(
"PrintMessage::Term({:?}, {:?})",
core::str::from_utf8(&msg.message),
msg.color
)),
PrintMessage::RustcErrTerm(msg) => f.write_fmt(format_args!(
"PrintMessage::RustcErrTerm({:?}, {:?})",
core::str::from_utf8(&msg.message),
msg.color
)),
}
}
}

pub struct TermMessage {
message: Vec<u8>,
color: Option<term::color::Color>,
Expand Down
2 changes: 1 addition & 1 deletion src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn verify_config_test_names() {
fn write_message(msg: &str) {
let print = Printer::new(Color::Auto);
buf_term_println!(print, None, "{msg}");
print.dump().unwrap();
print.write_to_outputs().unwrap();
}

// Integration tests. The files in `tests/source` are formatted and compared
Expand Down