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

base_n: speedup push_str #86214

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 8 additions & 7 deletions compiler/rustc_data_structures/src/base_n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@ const BASE_64: &[u8; MAX_BASE as usize] =

#[inline]
pub fn push_str(mut n: u128, base: usize, output: &mut String) {
debug_assert!(base >= 2 && base <= MAX_BASE);
assert!(base >= 2 && base <= MAX_BASE);
let mut s = [0u8; 128];
let mut index = 0;
let mut first_index = 0;

let base = base as u128;

loop {
s[index] = BASE_64[(n % base) as usize];
index += 1;
for idx in (0..128).rev() {
// SAFETY: given `base <= MAX_BASE`, so `n % base < MAX_BASE`
s[idx] = unsafe { *BASE_64.get_unchecked((n % base) as usize) };
Copy link
Member

Choose a reason for hiding this comment

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

This probably doesn't need to be unsafe. LLVM will eliminate the bounds check because the assertion above already allows it to proof the condition given in the comment.

n /= base;

if n == 0 {
first_index = idx;
break;
}
}
s[0..index].reverse();

output.push_str(str::from_utf8(&s[0..index]).unwrap());
// SAFETY: all chars in given range is nonnull ascii
output.push_str(unsafe { str::from_utf8_unchecked(&s[first_index..]) });
}

#[inline]
Expand Down