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

Optimize indentation in the pretty printer. #59507

Merged
merged 1 commit into from
Apr 1, 2019
Merged
Changes from all commits
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
22 changes: 19 additions & 3 deletions src/libsyntax/print/pp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ impl Default for BufEntry {
}
}

const SPACES: [u8; 128] = [b' '; 128];

impl<'a> Printer<'a> {
pub fn last_token(&mut self) -> Token {
self.buf[self.right].token.clone()
Expand Down Expand Up @@ -580,10 +582,24 @@ impl<'a> Printer<'a> {
debug!("print String({})", s);
// assert!(len <= space);
self.space -= len;
while self.pending_indentation > 0 {
write!(self.out, " ")?;
self.pending_indentation -= 1;

// Write the pending indent. A more concise way of doing this would be:
//
// write!(self.out, "{: >n$}", "", n = self.pending_indentation as usize)?;
//
// But that is significantly slower than using `SPACES`. This code is
// sufficiently hot, and indents can get sufficiently large, that the
// difference is significant on some workloads.
let spaces_len = SPACES.len() as isize;
while self.pending_indentation >= spaces_len {
self.out.write_all(&SPACES)?;
self.pending_indentation -= spaces_len;
}
if self.pending_indentation > 0 {
self.out.write_all(&SPACES[0..self.pending_indentation as usize])?;
self.pending_indentation = 0;
}

write!(self.out, "{}", s)
}

Expand Down