From 048de5223843145e99d482525036e8c158e54031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=BClker?= Date: Mon, 16 Jun 2025 19:32:33 +0200 Subject: [PATCH] Don't allocate in `Dom::trace` even when debug assertions are enabled (#37487) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Dom::trace` currently allocates a new string when debug assertions are enabled: https://github.com/servo/servo/blob/0f61361e27e6904c51431f96c9550fab9048d220/components/script_bindings/root.rs#L232-L241 This allocation is very heavy in profiles (~14% of runtime). While it doesn't affect production builds, these few characters are not providing enough value to justify the cost. This changes the method to instead only use `std::any::type_name`, without `format!`. With this change, all the string-format related methods vanish from the profile. Signed-off-by: Simon Wülker --- components/script_bindings/root.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/components/script_bindings/root.rs b/components/script_bindings/root.rs index 3d0378f0df1..f383447a997 100644 --- a/components/script_bindings/root.rs +++ b/components/script_bindings/root.rs @@ -229,15 +229,15 @@ impl Deref for Dom { } unsafe impl JSTraceable for Dom { - unsafe fn trace(&self, trc: *mut JSTracer) { - let trace_string; + unsafe fn trace(&self, tracer: *mut JSTracer) { let trace_info = if cfg!(debug_assertions) { - trace_string = format!("for {} on heap", ::std::any::type_name::()); - &trace_string[..] + std::any::type_name::() } else { - "for DOM object on heap" + "DOM object on heap" }; - trace_reflector(trc, trace_info, (*self.ptr.as_ptr()).reflector()); + unsafe { + trace_reflector(tracer, trace_info, (*self.ptr.as_ptr()).reflector()); + } } }