Skip to content

Commit

Permalink
Extend Printer::buf on demand.
Browse files Browse the repository at this point in the history
So that 55 entries (at 48 bytes each) don't need to be eagerly
initialized on creation.

This speeds up numerous rust-perf benchmark runs, by up to 3%.
  • Loading branch information
nnethercote committed Apr 30, 2018
1 parent f76f6fb commit 989815d
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/libsyntax/print/pp.rs
Expand Up @@ -247,12 +247,14 @@ pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> {
debug!("mk_printer {}", linewidth);
Printer {
out,
buf_len: n,
buf_max_len: n,
margin: linewidth as isize,
space: linewidth as isize,
left: 0,
right: 0,
buf: vec![BufEntry { token: Token::Eof, size: 0 }; n],
// Initialize a single entry; advance_right() will extend it on demand
// up to `buf_max_len` elements.
buf: vec![BufEntry::default()],
left_total: 0,
right_total: 0,
scan_stack: VecDeque::new(),
Expand All @@ -263,7 +265,7 @@ pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> {

pub struct Printer<'a> {
out: Box<io::Write+'a>,
buf_len: usize,
buf_max_len: usize,
/// Width of lines we're constrained to
margin: isize,
/// Number of spaces left on line
Expand Down Expand Up @@ -297,6 +299,12 @@ struct BufEntry {
size: isize,
}

impl Default for BufEntry {
fn default() -> Self {
BufEntry { token: Token::Eof, size: 0 }
}
}

impl<'a> Printer<'a> {
pub fn last_token(&mut self) -> Token {
self.buf[self.right].token.clone()
Expand All @@ -322,7 +330,9 @@ impl<'a> Printer<'a> {
self.right_total = 1;
self.left = 0;
self.right = 0;
} else { self.advance_right(); }
} else {
self.advance_right();
}
debug!("pp Begin({})/buffer Vec<{},{}>",
b.offset, self.left, self.right);
self.buf[self.right] = BufEntry { token: token, size: -self.right_total };
Expand All @@ -349,7 +359,9 @@ impl<'a> Printer<'a> {
self.right_total = 1;
self.left = 0;
self.right = 0;
} else { self.advance_right(); }
} else {
self.advance_right();
}
debug!("pp Break({})/buffer Vec<{},{}>",
b.offset, self.left, self.right);
self.check_stack(0);
Expand Down Expand Up @@ -408,7 +420,11 @@ impl<'a> Printer<'a> {
}
pub fn advance_right(&mut self) {
self.right += 1;
self.right %= self.buf_len;
self.right %= self.buf_max_len;
// Extend the buf if necessary.
if self.right == self.buf.len() {
self.buf.push(BufEntry::default());
}
assert_ne!(self.right, self.left);
}
pub fn advance_left(&mut self) -> io::Result<()> {
Expand Down Expand Up @@ -438,7 +454,7 @@ impl<'a> Printer<'a> {
}

self.left += 1;
self.left %= self.buf_len;
self.left %= self.buf_max_len;

left_size = self.buf[self.left].size;
}
Expand Down

0 comments on commit 989815d

Please sign in to comment.