Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize counting digits in line numbers during error reporting #82248

Merged
merged 1 commit into from
Feb 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment explaining why we calculate the number of digits this way? (To make sure someone doesn't revert this change without realising it's intentional, later.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an explanatory comment.

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) {
Expand Down