}
}
+const SPACES: [u8; 128] = [b' '; 128];
+
impl<'a> Printer<'a> {
pub fn last_token(&mut self) -> Token {
self.buf[self.right].token.clone()
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)
}