Don't allocate in Dom::trace even when debug assertions are enabled (#37487)

`Dom::trace` currently allocates a new string when debug assertions are
enabled:

0f61361e27/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 <simon.wuelker@arcor.de>
This commit is contained in:
Simon Wülker 2025-06-16 19:32:33 +02:00 committed by GitHub
parent f60e9cdff5
commit 048de52238
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -229,15 +229,15 @@ impl<T: DomObject> Deref for Dom<T> {
}
unsafe impl<T: DomObject> JSTraceable for Dom<T> {
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::<T>());
&trace_string[..]
std::any::type_name::<T>()
} 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());
}
}
}