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
Rip out auto term-printing
  • Loading branch information
MarcusGrass committed Feb 26, 2024
commit 378e10658257ccec0826cab6d34272fb656cc748
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ regex = "1.7"
serde = { version = "1.0.160", features = ["derive"] }
serde_json = "1.0"
term = "0.7"
termcolor = "1.4.1"
thiserror = "1.0.40"
toml = "0.7.4"
tracing = "0.1.37"
Expand Down
18 changes: 7 additions & 11 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::str::FromStr;
use std::thread::JoinHandle;

use getopts::{Matches, Options};
use rustfmt_nightly::buf_eprintln;
use rustfmt_nightly::{buf_eprintln, buf_println};
use rustfmt_nightly::print::Printer;

use crate::rustfmt::{
Expand Down Expand Up @@ -350,7 +350,7 @@ fn format(
buf_eprintln!(printer, "Error: file `{}` does not exist", file.to_str().unwrap());
session.add_operational_error();
} else if file.is_dir() {
eprintln!("Error: `{}` is a directory", file.to_str().unwrap());
buf_eprintln!(printer, "Error: `{}` is a directory", file.to_str().unwrap());
session.add_operational_error();
} else {
// Check the file directory if the config-path could not be read or not provided
Expand All @@ -371,11 +371,9 @@ fn format(
};
if local_config.verbose() == Verbosity::Verbose {
if let Some(path) = config_path {
println!(
"Using rustfmt config file {} for {}",
buf_println!(printer, "Using rustfmt config file {} for {}",
path.display(),
file.display()
);
file.display());
}
}

Expand Down Expand Up @@ -451,16 +449,14 @@ fn format_and_emit_report<T: Write>(session: &mut Session<'_, T>, input: Input)
match session.format(input) {
Ok(report) => {
if report.has_warnings() {
eprintln!(
"{}",
buf_eprintln!(session.printer, "{}",
FormatReportFormatterBuilder::new(&report)
.enable_colors(should_print_with_colors(session))
.build()
);
.build());
}
}
Err(msg) => {
eprintln!("Error writing files: {msg}");
buf_eprintln!(session.printer, "Error writing files: {msg}");
session.add_operational_error();
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::parse::parser::{DirectoryOwnership, Parser, ParserError};
use crate::parse::session::ParseSess;
use crate::utils::{contains_skip, count_newlines};
use crate::visitor::FmtVisitor;
use crate::{modules, source_file, ErrorKind, FormatReport, Input, Session};
use crate::{modules, source_file, ErrorKind, FormatReport, Input, Session, buf_eprintln};
use crate::print::Printer;

mod generated;
Expand Down Expand Up @@ -111,7 +111,7 @@ fn format_project<T: FormatHandler>(
let main_file = input.file_name();
let input_is_stdin = main_file == FileName::Stdin;

let parse_session = ParseSess::new(config)?;
let parse_session = ParseSess::new(config, printer)?;
if config.skip_children() && parse_session.ignore_file(&main_file) {
return Ok(FormatReport::new());
}
Expand All @@ -125,7 +125,7 @@ fn format_project<T: FormatHandler>(
Err(e) => {
let forbid_verbose = input_is_stdin || e != ParserError::ParsePanicError;
should_emit_verbose(forbid_verbose, config, || {
eprintln!("The Rust parser panicked");
buf_eprintln!(printer, "The Rust parser panicked");
});
report.add_parsing_error();
return Ok(report);
Expand Down
1 change: 0 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use crate::lists::{itemize_list, write_list, ListFormatting};
use crate::overflow;
use crate::parse::macros::lazy_static::parse_lazy_static;
use crate::parse::macros::{parse_expr, parse_macro_args, ParsedMacroArgs};
use crate::print::Printer;
use crate::rewrite::{Rewrite, RewriteContext};
use crate::shape::{Indent, Shape};
use crate::source_map::SpanUtils;
Expand Down
8 changes: 6 additions & 2 deletions src/parse/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::source_map::LineRangeUtils;
use crate::utils::starts_with_newline;
use crate::visitor::SnippetProvider;
use crate::{Config, ErrorKind, FileName};
use crate::print::Printer;

/// ParseSess holds structs necessary for constructing a parser.
pub(crate) struct ParseSess {
Expand Down Expand Up @@ -124,6 +125,7 @@ fn default_dcx(
can_reset: Lrc<AtomicBool>,
show_parse_errors: bool,
color: Color,
printer: &Printer,
) -> DiagCtxt {
let supports_color = term::stderr().map_or(false, |term| term.supports_color());
let emit_color = if supports_color {
Expand All @@ -139,7 +141,8 @@ fn default_dcx(
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
false,
);
Box::new(EmitterWriter::stderr(emit_color, fallback_bundle).sm(Some(source_map.clone())))
Box::new(EmitterWriter::new(Box::new(printer.clone()), fallback_bundle))
//Box::new(EmitterWriter::stderr(emit_color, fallback_bundle).sm(Some(source_map.clone())))
};
DiagCtxt::with_emitter(Box::new(SilentOnIgnoredFilesEmitter {
has_non_ignorable_parser_errors: false,
Expand All @@ -151,7 +154,7 @@ fn default_dcx(
}

impl ParseSess {
pub(crate) fn new(config: &Config) -> Result<ParseSess, ErrorKind> {
pub(crate) fn new(config: &Config, printer: &Printer) -> Result<ParseSess, ErrorKind> {
let ignore_path_set = match IgnorePathSet::from_ignore_list(&config.ignore()) {
Ok(ignore_path_set) => Lrc::new(ignore_path_set),
Err(e) => return Err(ErrorKind::InvalidGlobPattern(e)),
Expand All @@ -165,6 +168,7 @@ impl ParseSess {
Lrc::clone(&can_reset_errors),
config.show_parse_errors(),
config.color(),
printer,
);
let parse_sess = RawParseSess::with_dcx(dcx, source_map);

Expand Down
61 changes: 51 additions & 10 deletions src/print.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,66 @@
use std::sync::Mutex;
use std::io::Write;
use std::sync::{Arc, Mutex};
use rustc_errors::{ColorSpec, WriteColor, Color as RustColor};
use crate::Color;

#[derive(Clone)]
pub struct Printer {
inner: Mutex<PrinterInner>,
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;
inner.messages.push(PrintMessage::Term(TermMessage::new(buf.to_vec(), col)));
Ok(buf.len())
}

#[inline]
fn flush(&mut self) -> std::io::Result<()> {
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 = spec.fg().copied();
Ok(())
}

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

struct PrinterInner {
color: Color,
messages: Vec<PrintMessage>
color_setting: Color,
current_color: Option<RustColor>,
messages: Vec<PrintMessage>,
supports_color: bool,
}

impl Printer {

pub fn new(term_output_color: Color) -> Self {
Self {
inner: Mutex::new(PrinterInner {
color: term_output_color,
inner: Arc::new(Mutex::new(PrinterInner {
color_setting: term_output_color,
current_color: None,
messages: vec![],
}),
supports_color: true, // Todo: Actually check
})),
}
}

#[inline]
pub fn push_msg(&self, msg: PrintMessage) {
self.inner.lock().unwrap().messages.push(msg);
Expand Down Expand Up @@ -49,7 +90,7 @@ macro_rules! buf_term_println {
($pb: expr, $col:expr, $($arg:tt)*) => {{
let mut msg_buf = Vec::new();
let _ = writeln!(&mut msg_buf, $($arg)*);
$pb.push_msg($crate::print::PrintMessage::Term(TermMessage::new(msg_buf, $col)));
$pb.push_msg($crate::print::PrintMessage::Term($crate::print::TermMessage::new(msg_buf, $col)));
}};
}

Expand All @@ -61,11 +102,11 @@ pub enum PrintMessage {

pub struct TermMessage {
message: Vec<u8>,
color: Option<Color>,
color: Option<RustColor>,
}

impl TermMessage {
pub fn new(message: Vec<u8>, color: Option<Color>) -> Self {
pub fn new(message: Vec<u8>, color: Option<RustColor>) -> Self {
Self { message, color }
}
}
15 changes: 8 additions & 7 deletions src/rustfmt_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use std::collections::VecDeque;
use std::fmt;
use std::io;
use std::io::Write;
use crate::buf_term_println;

use crate::config::{Color, Config, Verbosity};
use crate::print::Printer;

#[derive(Debug, PartialEq)]
pub(crate) enum DiffLine {
Expand Down Expand Up @@ -245,7 +247,7 @@ pub(crate) fn make_diff(expected: &str, actual: &str, context_size: usize) -> Ve
results
}

pub(crate) fn print_diff<F>(diff: Vec<Mismatch>, get_section_title: F, config: &Config)
pub(crate) fn print_diff<F>(diff: Vec<Mismatch>, get_section_title: F, config: &Config, printer: &Printer)
where
F: Fn(u32) -> String,
{
Expand All @@ -265,14 +267,13 @@ where
for line in mismatch.lines {
match line {
DiffLine::Context(ref str) => {
writer.writeln(&format!(" {str}{line_terminator}"), None)
buf_term_println!(printer, None, " {str}{line_terminator}");
}
DiffLine::Expected(ref str) => writer.writeln(
&format!("+{str}{line_terminator}"),
Some(term::color::GREEN),
),
DiffLine::Expected(ref str) => {
buf_term_println!(printer, Some(rustc_errors::Color::Green), "+{str}{line_terminator}");
},
DiffLine::Resulting(ref str) => {
writer.writeln(&format!("-{str}{line_terminator}"), Some(term::color::RED))
buf_term_println!(printer, Some(rustc_errors::Color::Red), "-{str}{line_terminator}");
}
}
}
Expand Down