Auto merge of #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>
This commit is contained in:
commit
9d272a1b05
3 changed files with 6 additions and 2 deletions
|
@ -4572,6 +4572,7 @@ name = "rustc_span"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"indexmap",
|
"indexmap",
|
||||||
|
"itoa",
|
||||||
"md-5",
|
"md-5",
|
||||||
"rustc_arena",
|
"rustc_arena",
|
||||||
"rustc_data_structures",
|
"rustc_data_structures",
|
||||||
|
|
|
@ -6,6 +6,7 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# tidy-alphabetical-start
|
# tidy-alphabetical-start
|
||||||
indexmap = { version = "2.0.0" }
|
indexmap = { version = "2.0.0" }
|
||||||
|
itoa = "1.0"
|
||||||
md5 = { package = "md-5", version = "0.10.0" }
|
md5 = { package = "md-5", version = "0.10.0" }
|
||||||
rustc_arena = { path = "../rustc_arena" }
|
rustc_arena = { path = "../rustc_arena" }
|
||||||
rustc_data_structures = { path = "../rustc_data_structures" }
|
rustc_data_structures = { path = "../rustc_data_structures" }
|
||||||
|
|
|
@ -2327,13 +2327,15 @@ pub mod sym {
|
||||||
///
|
///
|
||||||
/// The first few non-negative integers each have a static symbol and therefore
|
/// The first few non-negative integers each have a static symbol and therefore
|
||||||
/// are fast.
|
/// 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 let Result::Ok(idx) = n.try_into() {
|
||||||
if idx < 10 {
|
if idx < 10 {
|
||||||
return Symbol::new(super::SYMBOL_DIGITS_BASE + idx as u32);
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue