Skip to content

Commit 46ec2df

Browse files
committed
optimize format_args placeholders without options
This attempts to achieve the same thing as #104525, but without adding a dedicated method to `Display`. Draft for experimentation
1 parent 7d8ebe3 commit 46ec2df

3 files changed

Lines changed: 76 additions & 59 deletions

File tree

compiler/rustc_ast_lowering/src/format.rs

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ fn flatten_format_args(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
218218

219219
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
220220
enum ArgumentType {
221-
Format(FormatTrait),
221+
Format { how: FormatTrait, simple: bool },
222222
Usize,
223223
}
224224

@@ -241,18 +241,32 @@ fn make_argument<'hir>(
241241
sp,
242242
hir::LangItem::FormatArgument,
243243
match ty {
244-
Format(Display) => sym::new_display,
245-
Format(Debug) => match ctx.tcx.sess.opts.unstable_opts.fmt_debug {
246-
FmtDebug::Full | FmtDebug::Shallow => sym::new_debug,
244+
Format { how: Display, simple: false } => sym::new_display,
245+
Format { how: Display, simple: true } => sym::new_display_simple,
246+
Format { how: Debug, simple } => match ctx.tcx.sess.opts.unstable_opts.fmt_debug {
247+
FmtDebug::Full | FmtDebug::Shallow => {
248+
if simple {
249+
sym::new_debug_simple
250+
} else {
251+
sym::new_debug
252+
}
253+
}
247254
FmtDebug::None => sym::new_debug_noop,
248255
},
249-
Format(LowerExp) => sym::new_lower_exp,
250-
Format(UpperExp) => sym::new_upper_exp,
251-
Format(Octal) => sym::new_octal,
252-
Format(Pointer) => sym::new_pointer,
253-
Format(Binary) => sym::new_binary,
254-
Format(LowerHex) => sym::new_lower_hex,
255-
Format(UpperHex) => sym::new_upper_hex,
256+
Format { how: LowerExp, simple: false } => sym::new_lower_exp,
257+
Format { how: LowerExp, simple: true } => sym::new_lower_exp_simple,
258+
Format { how: UpperExp, simple: false } => sym::new_upper_exp,
259+
Format { how: UpperExp, simple: true } => sym::new_upper_exp_simple,
260+
Format { how: Octal, simple: false } => sym::new_octal,
261+
Format { how: Octal, simple: true } => sym::new_octal_simple,
262+
Format { how: Pointer, simple: false } => sym::new_pointer,
263+
Format { how: Pointer, simple: true } => sym::new_pointer_simple,
264+
Format { how: Binary, simple: false } => sym::new_binary,
265+
Format { how: Binary, simple: true } => sym::new_binary_simple,
266+
Format { how: LowerHex, simple: false } => sym::new_lower_hex,
267+
Format { how: LowerHex, simple: true } => sym::new_lower_hex_simple,
268+
Format { how: UpperHex, simple: false } => sym::new_upper_hex,
269+
Format { how: UpperHex, simple: true } => sym::new_upper_hex_simple,
256270
Usize => sym::from_usize,
257271
},
258272
));
@@ -360,16 +374,6 @@ fn expand_format_args<'hir>(
360374
let i = bytecode.len();
361375
bytecode.push(0xC0);
362376

363-
let position = argmap
364-
.insert_full(
365-
(
366-
p.argument.index.unwrap_or(usize::MAX),
367-
ArgumentType::Format(p.format_trait),
368-
),
369-
p.span,
370-
)
371-
.0 as u64;
372-
373377
// This needs to match the constants in library/core/src/fmt/mod.rs.
374378
let o = &p.format_options;
375379
let align = match o.alignment {
@@ -389,6 +393,18 @@ fn expand_format_args<'hir>(
389393
| (o.width.is_some() as u32) << 27
390394
| (o.precision.is_some() as u32) << 28
391395
| align << 29;
396+
397+
let is_simple = flags == default_flags;
398+
let position = argmap
399+
.insert_full(
400+
(
401+
p.argument.index.unwrap_or(usize::MAX),
402+
ArgumentType::Format { how: p.format_trait, simple: is_simple },
403+
),
404+
p.span,
405+
)
406+
.0 as u64;
407+
392408
if flags != default_flags {
393409
bytecode[i] |= 1;
394410
bytecode.extend_from_slice(&flags.to_le_bytes());

compiler/rustc_span/src/symbol.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,18 +1557,27 @@ symbols! {
15571557
never_type_fallback,
15581558
new,
15591559
new_binary,
1560+
new_binary_simple,
15601561
new_const,
15611562
new_debug,
15621563
new_debug_noop,
1564+
new_debug_simple,
15631565
new_display,
1566+
new_display_simple,
15641567
new_lower_exp,
1568+
new_lower_exp_simple,
15651569
new_lower_hex,
1570+
new_lower_hex_simple,
15661571
new_octal,
1572+
new_octal_simple,
15671573
new_pointer,
1574+
new_pointer_simple,
15681575
new_range,
15691576
new_unchecked,
15701577
new_upper_exp,
1578+
new_upper_exp_simple,
15711579
new_upper_hex,
1580+
new_upper_hex_simple,
15721581
new_v1,
15731582
new_v1_formatted,
15741583
next,

library/core/src/fmt/rt.rs

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -73,58 +73,50 @@ macro_rules! argument_new {
7373
},
7474
#[cfg(any(sanitize = "cfi", sanitize = "kcfi"))]
7575
formatter: |ptr: NonNull<()>, fmt: &mut Formatter<'_>| {
76-
let func = $f;
76+
let f: fn(&$t, &mut Formatter<'_>) -> Result = $f;
7777
// SAFETY: This is the same type as the `value` field.
7878
let r = unsafe { ptr.cast::<$t>().as_ref() };
79-
(func)(r, fmt)
79+
f(r, fmt)
8080
},
8181
_lifetime: PhantomData,
8282
},
8383
}
8484
};
8585
}
8686

87+
macro_rules! argument_constructor {
88+
($name:ident => $trait:path) => {
89+
#[inline]
90+
pub const fn $name<T: $trait>(x: &T) -> Argument<'_> {
91+
argument_new!(T, x, <T as $trait>::fmt)
92+
}
93+
94+
#[inline]
95+
pub const fn ${concat($name, _simple)}<T: $trait>(x: &T) -> Argument<'_> {
96+
argument_new!(T, x, |x, f| {
97+
let mut f = f.with_options(FormattingOptions::new());
98+
<T as $trait>::fmt(x, &mut f)
99+
})
100+
}
101+
};
102+
}
103+
87104
impl Argument<'_> {
88-
#[inline]
89-
pub const fn new_display<T: Display>(x: &T) -> Argument<'_> {
90-
argument_new!(T, x, <T as Display>::fmt)
91-
}
92-
#[inline]
93-
pub const fn new_debug<T: Debug>(x: &T) -> Argument<'_> {
94-
argument_new!(T, x, <T as Debug>::fmt)
95-
}
105+
argument_constructor!(new_display => Display);
106+
argument_constructor!(new_debug => Debug);
107+
argument_constructor!(new_octal => Octal);
108+
argument_constructor!(new_lower_hex => LowerHex);
109+
argument_constructor!(new_upper_hex => UpperHex);
110+
argument_constructor!(new_pointer => Pointer);
111+
argument_constructor!(new_binary => Binary);
112+
argument_constructor!(new_lower_exp => LowerExp);
113+
argument_constructor!(new_upper_exp => UpperExp);
114+
96115
#[inline]
97116
pub const fn new_debug_noop<T: Debug>(x: &T) -> Argument<'_> {
98117
argument_new!(T, x, |_: &T, _| Ok(()))
99118
}
100-
#[inline]
101-
pub const fn new_octal<T: Octal>(x: &T) -> Argument<'_> {
102-
argument_new!(T, x, <T as Octal>::fmt)
103-
}
104-
#[inline]
105-
pub const fn new_lower_hex<T: LowerHex>(x: &T) -> Argument<'_> {
106-
argument_new!(T, x, <T as LowerHex>::fmt)
107-
}
108-
#[inline]
109-
pub const fn new_upper_hex<T: UpperHex>(x: &T) -> Argument<'_> {
110-
argument_new!(T, x, <T as UpperHex>::fmt)
111-
}
112-
#[inline]
113-
pub const fn new_pointer<T: Pointer>(x: &T) -> Argument<'_> {
114-
argument_new!(T, x, <T as Pointer>::fmt)
115-
}
116-
#[inline]
117-
pub const fn new_binary<T: Binary>(x: &T) -> Argument<'_> {
118-
argument_new!(T, x, <T as Binary>::fmt)
119-
}
120-
#[inline]
121-
pub const fn new_lower_exp<T: LowerExp>(x: &T) -> Argument<'_> {
122-
argument_new!(T, x, <T as LowerExp>::fmt)
123-
}
124-
#[inline]
125-
pub const fn new_upper_exp<T: UpperExp>(x: &T) -> Argument<'_> {
126-
argument_new!(T, x, <T as UpperExp>::fmt)
127-
}
119+
128120
#[inline]
129121
#[track_caller]
130122
pub const fn from_usize(x: &usize) -> Argument<'_> {

0 commit comments

Comments
 (0)