Skip to content

Commit 37c8f25

Browse files
committed
Use non-generic inner function for pointer formatting
1 parent 454cc5f commit 37c8f25

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

library/core/src/fmt/mod.rs

+24-18
Original file line numberDiff line numberDiff line change
@@ -2186,28 +2186,34 @@ impl Display for char {
21862186
#[stable(feature = "rust1", since = "1.0.0")]
21872187
impl<T: ?Sized> Pointer for *const T {
21882188
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2189-
let old_width = f.width;
2190-
let old_flags = f.flags;
2191-
2192-
// The alternate flag is already treated by LowerHex as being special-
2193-
// it denotes whether to prefix with 0x. We use it to work out whether
2194-
// or not to zero extend, and then unconditionally set it to get the
2195-
// prefix.
2196-
if f.alternate() {
2197-
f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32);
2198-
2199-
if f.width.is_none() {
2200-
f.width = Some((usize::BITS / 4) as usize + 2);
2189+
/// Since the formatting will be identical for all pointer types, use a non-monomorphized
2190+
/// implementation for the actual formatting to reduce the amount of codegen work needed
2191+
fn inner(ptr: *const (), f: &mut Formatter<'_>) -> Result {
2192+
let old_width = f.width;
2193+
let old_flags = f.flags;
2194+
2195+
// The alternate flag is already treated by LowerHex as being special-
2196+
// it denotes whether to prefix with 0x. We use it to work out whether
2197+
// or not to zero extend, and then unconditionally set it to get the
2198+
// prefix.
2199+
if f.alternate() {
2200+
f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32);
2201+
2202+
if f.width.is_none() {
2203+
f.width = Some((usize::BITS / 4) as usize + 2);
2204+
}
22012205
}
2202-
}
2203-
f.flags |= 1 << (FlagV1::Alternate as u32);
2206+
f.flags |= 1 << (FlagV1::Alternate as u32);
2207+
2208+
let ret = LowerHex::fmt(&(ptr as usize), f);
22042209

2205-
let ret = LowerHex::fmt(&(*self as *const () as usize), f);
2210+
f.width = old_width;
2211+
f.flags = old_flags;
22062212

2207-
f.width = old_width;
2208-
f.flags = old_flags;
2213+
ret
2214+
}
22092215

2210-
ret
2216+
inner(*self as *const (), f)
22112217
}
22122218
}
22132219

0 commit comments

Comments
 (0)