Skip to content

Commit 8a5c568

Browse files
committed
nhwn: optimize counting digits in line numbers
1 parent 93f6a4b commit 8a5c568

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

compiler/rustc_errors/src/emitter.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,18 @@ impl EmitterWriter {
17131713
let max_line_num_len = if self.ui_testing {
17141714
ANONYMIZED_LINE_NUM.len()
17151715
} else {
1716-
self.get_max_line_num(span, children).to_string().len()
1716+
// Instead of using .to_string().len(), we iteratively count the
1717+
// number of digits to avoid allocation. This strategy has sizable
1718+
// performance gains over the old string strategy.
1719+
let mut n = self.get_max_line_num(span, children);
1720+
let mut num_digits = 0;
1721+
loop {
1722+
num_digits += 1;
1723+
n /= 10;
1724+
if n == 0 {
1725+
break num_digits;
1726+
}
1727+
}
17171728
};
17181729

17191730
match self.emit_message_default(span, message, code, level, max_line_num_len, false) {

0 commit comments

Comments
 (0)