about summary refs log tree commit diff
path: root/compiler/rustc_errors/src
diff options
context:
space:
mode:
authorNathan Nguyen <nathan.tm.nguyen@gmail.com>2021-02-17 23:44:37 -0600
committerNathan Nguyen <nathan.tm.nguyen@gmail.com>2021-02-18 08:20:07 -0600
commit8a5c5681da3695e1c2e3f23bee43a7ebfdce6161 (patch)
tree61fd5eff1a328ad11cb0fde32fff34fe6984f5ec /compiler/rustc_errors/src
parent93f6a4b9d85b7eef71e17d95c113f5834238b574 (diff)
downloadrust-8a5c5681da3695e1c2e3f23bee43a7ebfdce6161.tar.gz
rust-8a5c5681da3695e1c2e3f23bee43a7ebfdce6161.zip
nhwn: optimize counting digits in line numbers
Diffstat (limited to 'compiler/rustc_errors/src')
-rw-r--r--compiler/rustc_errors/src/emitter.rs13
1 files changed, 12 insertions, 1 deletions
diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs
index ea62e215230..42c3d5e48fe 100644
--- a/compiler/rustc_errors/src/emitter.rs
+++ b/compiler/rustc_errors/src/emitter.rs
@@ -1713,7 +1713,18 @@ impl EmitterWriter {
         let max_line_num_len = if self.ui_testing {
             ANONYMIZED_LINE_NUM.len()
         } else {
-            self.get_max_line_num(span, children).to_string().len()
+            // Instead of using .to_string().len(), we iteratively count the
+            // number of digits to avoid allocation. This strategy has sizable
+            // performance gains over the old string strategy.
+            let mut n = self.get_max_line_num(span, children);
+            let mut num_digits = 0;
+            loop {
+                num_digits += 1;
+                n /= 10;
+                if n == 0 {
+                    break num_digits;
+                }
+            }
         };
 
         match self.emit_message_default(span, message, code, level, max_line_num_len, false) {