Skip to content

Commit

Permalink
Optimize Symbol::integer by utilizing itoa in-place formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Mar 6, 2024
1 parent 1547c07 commit 33ef4b9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4570,6 +4570,7 @@ name = "rustc_span"
version = "0.0.0"
dependencies = [
"indexmap",
"itoa",
"md-5",
"rustc_arena",
"rustc_data_structures",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
# tidy-alphabetical-start
indexmap = { version = "2.0.0" }
itoa = "1.0"
md5 = { package = "md-5", version = "0.10.0" }
rustc_arena = { path = "../rustc_arena" }
rustc_data_structures = { path = "../rustc_data_structures" }
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2325,13 +2325,15 @@ pub mod sym {
///
/// The first few non-negative integers each have a static symbol and therefore
/// are fast.
pub fn integer<N: TryInto<usize> + Copy + ToString>(n: N) -> Symbol {
pub fn integer<N: TryInto<usize> + Copy + itoa::Integer>(n: N) -> Symbol {
if let Result::Ok(idx) = n.try_into() {
if idx < 10 {
return Symbol::new(super::SYMBOL_DIGITS_BASE + idx as u32);
}
}
Symbol::intern(&n.to_string())
let mut buffer = itoa::Buffer::new();
let printed = buffer.format(n);
Symbol::intern(printed)
}
}

Expand Down

0 comments on commit 33ef4b9

Please sign in to comment.