about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-03-31 23:50:46 +0000
committerbors <bors@rust-lang.org>2019-03-31 23:50:46 +0000
commiteab3eb38df8dca99110b6149b3a15deeb4ef0413 (patch)
treefb19dec596c81c4854d95386a5868857bbda9081 /src/libsyntax
parente3428db7c2b1eeb35096e0bf37672397977ff030 (diff)
parent606f3158bf7dd2708b1efdde2e6de34f8eaebd7b (diff)
downloadrust-eab3eb38df8dca99110b6149b3a15deeb4ef0413.tar.gz
rust-eab3eb38df8dca99110b6149b3a15deeb4ef0413.zip
Auto merge of #59507 - nnethercote:indent-with-SPACES, r=petrochenkov
Optimize indentation in the pretty printer.

Currently the pretty-printer calls `write!` for every space of
indentation. On some workloads the indentation level can exceed 100, and
a faster implementation reduces instruction counts by up to 7% on a few
workloads.
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/print/pp.rs22
1 files changed, 19 insertions, 3 deletions
diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs
index d8a8cbb655b..45eb6995a76 100644
--- a/src/libsyntax/print/pp.rs
+++ b/src/libsyntax/print/pp.rs
@@ -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()
@@ -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)
     }