Skip to content

Commit 9d272a1

Browse files
committedMar 9, 2024·
Auto merge of rust-lang#122102 - Urgau:optimize-symbol-integer, r=cjgillot
Optimize `Symbol::integer` by utilizing in-place formatting This PR optimize `Symbol::integer` by utilizing `itoa` in-place formatting instead of going through a dynamically allocated `String` and the format machinery. <details> For some context: I was profiling `rustc --check-cfg` with callgrind and due to the way we currently setup all the targets and we end-up calling `Symbol::integer` multiple times for all the targets. Using `itoa` reduced the number of instructions. </details>
2 parents 48a15aa + 33ef4b9 commit 9d272a1

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed
 

‎Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4572,6 +4572,7 @@ name = "rustc_span"
45724572
version = "0.0.0"
45734573
dependencies = [
45744574
"indexmap",
4575+
"itoa",
45754576
"md-5",
45764577
"rustc_arena",
45774578
"rustc_data_structures",

‎compiler/rustc_span/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
[dependencies]
77
# tidy-alphabetical-start
88
indexmap = { version = "2.0.0" }
9+
itoa = "1.0"
910
md5 = { package = "md-5", version = "0.10.0" }
1011
rustc_arena = { path = "../rustc_arena" }
1112
rustc_data_structures = { path = "../rustc_data_structures" }

‎compiler/rustc_span/src/symbol.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2327,13 +2327,15 @@ pub mod sym {
23272327
///
23282328
/// The first few non-negative integers each have a static symbol and therefore
23292329
/// are fast.
2330-
pub fn integer<N: TryInto<usize> + Copy + ToString>(n: N) -> Symbol {
2330+
pub fn integer<N: TryInto<usize> + Copy + itoa::Integer>(n: N) -> Symbol {
23312331
if let Result::Ok(idx) = n.try_into() {
23322332
if idx < 10 {
23332333
return Symbol::new(super::SYMBOL_DIGITS_BASE + idx as u32);
23342334
}
23352335
}
2336-
Symbol::intern(&n.to_string())
2336+
let mut buffer = itoa::Buffer::new();
2337+
let printed = buffer.format(n);
2338+
Symbol::intern(printed)
23372339
}
23382340
}
23392341

0 commit comments

Comments
 (0)
Please sign in to comment.