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
Fmt
  • Loading branch information
MarcusGrass committed Feb 26, 2024
commit b1e1cd477abf6c8aa346fd62cba44a19e69eac7c
19 changes: 14 additions & 5 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ use crate::parse::session::ParseSess;
use crate::print::Printer;
use crate::utils::{contains_skip, count_newlines};
use crate::visitor::FmtVisitor;
use crate::{buf_eprintln, modules, source_file, ErrorKind, FormatReport, Input, Session, buf_println, buf_print};
use crate::{
buf_eprintln, buf_print, buf_println, modules, source_file, ErrorKind, FormatReport, Input,
Session,
};

mod generated;
mod newline_style;
Expand All @@ -44,7 +47,7 @@ impl<'b, T: Write + 'b> Session<'b, T> {
// Echo back stdin
buf_print!(self.printer, "{buf}");
Ok(FormatReport::new())
},
}
_ => Ok(FormatReport::new()),
};
}
Expand Down Expand Up @@ -151,13 +154,19 @@ fn format_project<T: FormatHandler>(
for (path, module) in files {
if input_is_stdin && contains_skip(module.attrs()) {
// Echo back stdin
buf_print!(printer, "{}", context
buf_print!(
printer,
"{}",
context
.parse_session
.snippet_provider(module.span)
.entire_snippet());
.entire_snippet()
);
return Ok(FormatReport::new());
}
should_emit_verbose(input_is_stdin, config, || buf_println!(printer, "Formatting {}", path));
should_emit_verbose(input_is_stdin, config, || {
buf_println!(printer, "Formatting {}", path)
});
context.format_file(path, &module, is_macro_def)?;
}
timer = timer.done_formatting();
Expand Down
8 changes: 4 additions & 4 deletions src/parse/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ fn default_dcx(
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
false,
);
Box::new(EmitterWriter::new(
Box::new(printer.clone()),
fallback_bundle,
).sm(Some(source_map.clone())))
Box::new(
EmitterWriter::new(Box::new(printer.clone()), fallback_bundle)
.sm(Some(source_map.clone())),
)
};
DiagCtxt::with_emitter(Box::new(SilentOnIgnoredFilesEmitter {
has_non_ignorable_parser_errors: false,
Expand Down
34 changes: 19 additions & 15 deletions src/print.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Formatter;
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 _};
Expand Down Expand Up @@ -34,7 +34,6 @@ impl Write for Printer {
//println!("Flush");
Ok(())
}

}

impl WriteColor for Printer {
Expand All @@ -54,7 +53,6 @@ impl WriteColor for Printer {
self.inner.lock().unwrap().current_color.take();
Ok(())
}

}

struct PrinterInner {
Expand Down Expand Up @@ -223,18 +221,24 @@ pub enum PrintMessage {
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))
}
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
)),
}
}
}
Expand Down