Skip to content

Commit

Permalink
Auto merge of #50339 - nnethercote:lazy-Printer-buf, r=michaelwoerister
Browse files Browse the repository at this point in the history
Extend Printer::buf on demand.

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%.
```
crates.io-check
        avg: -2.4%      min: -3.7%      max: -1.1%
encoding-check
        avg: -2.1%      min: -2.9%      max: -1.2%
crates.io-opt
        avg: -1.3%      min: -2.7%      max: -0.1%
crates.io
        avg: -1.4%      min: -2.7%      max: -0.3%
encoding-opt
        avg: -1.1%      min: -2.5%      max: 0.1%
encoding
        avg: -1.3%      min: -2.4%      max: -0.3%
hyper-check
        avg: -1.7%      min: -2.3%      max: -0.9%
regex-check
        avg: -1.5%      min: -1.9%      max: -0.7%
piston-image-check
        avg: -0.9%      min: -1.8%      max: -0.5%
hyper
        avg: -1.0%      min: -1.7%      max: -0.3%
hyper-opt
        avg: -0.9%      min: -1.7%      max: -0.1%
syn-check
        avg: -1.0%      min: -1.5%      max: -0.6%
clap-rs
        avg: -0.3%      min: -1.5%      max: 0.2%
regex-opt
        avg: -0.6%      min: -1.5%      max: -0.0%
regression-31157-check
        avg: -1.1%      min: -1.4%      max: -0.7%
regex
        avg: -0.7%      min: -1.3%      max: -0.1%
clap-rs-check
        avg: -0.5%      min: -1.2%      max: 0.1%
syn-opt
        avg: -0.5%      min: -1.1%      max: -0.1%
syn
        avg: -0.5%      min: -1.1%      max: -0.2%
serde-opt
        avg: -0.3%      min: -1.1%      max: 0.1%
piston-image-opt
        avg: -0.4%      min: -1.1%      max: -0.0%
piston-image
        avg: -0.4%      min: -1.0%      max: -0.0%
```
  • Loading branch information
bors committed May 2, 2018
2 parents 5f3994f + 989815d commit 6a87289
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/libsyntax/print/pp.rs
Original file line number Diff line number Diff line change
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 6a87289

Please sign in to comment.