From 33ef4b963bb9f24119e638f91d08241ba71f0ca2 Mon Sep 17 00:00:00 2001 From: Urgau Date: Wed, 6 Mar 2024 19:39:36 +0100 Subject: [PATCH] Optimize Symbol::integer by utilizing itoa in-place formatting --- Cargo.lock | 1 + compiler/rustc_span/Cargo.toml | 1 + compiler/rustc_span/src/symbol.rs | 6 ++++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5feb21a65b774..c12b23e87fd4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4570,6 +4570,7 @@ name = "rustc_span" version = "0.0.0" dependencies = [ "indexmap", + "itoa", "md-5", "rustc_arena", "rustc_data_structures", diff --git a/compiler/rustc_span/Cargo.toml b/compiler/rustc_span/Cargo.toml index 99de91a068ad3..98ed985738adf 100644 --- a/compiler/rustc_span/Cargo.toml +++ b/compiler/rustc_span/Cargo.toml @@ -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" } diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 3784a08b1b720..96ad1e81e798b 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -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 + Copy + ToString>(n: N) -> Symbol { + pub fn integer + 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) } }