Skip to content

Commit dd5d80a

Browse files
committed
Auto merge of rust-lang#83302 - camsteffen:write-piece-unchecked, r=dtolnay
Get piece unchecked in `write` We already use specialized `zip`, but it seems like we can do a little better by not checking `pieces` length at all. `Arguments` constructors are now unsafe. So the `format_args!` expansion now includes an `unsafe` block. <details> <summary>Local Bench Diff</summary> ```text name before ns/iter after ns/iter diff ns/iter diff % speedup fmt::write_str_macro1 22,967 19,718 -3,249 -14.15% x 1.16 fmt::write_str_macro2 35,527 32,654 -2,873 -8.09% x 1.09 fmt::write_str_macro_debug 571,953 575,973 4,020 0.70% x 0.99 fmt::write_str_ref 9,579 9,459 -120 -1.25% x 1.01 fmt::write_str_value 9,573 9,572 -1 -0.01% x 1.00 fmt::write_u128_max 176 173 -3 -1.70% x 1.02 fmt::write_u128_min 138 134 -4 -2.90% x 1.03 fmt::write_u64_max 139 136 -3 -2.16% x 1.02 fmt::write_u64_min 129 135 6 4.65% x 0.96 fmt::write_vec_macro1 24,401 22,273 -2,128 -8.72% x 1.10 fmt::write_vec_macro2 37,096 35,602 -1,494 -4.03% x 1.04 fmt::write_vec_macro_debug 588,291 589,575 1,284 0.22% x 1.00 fmt::write_vec_ref 9,568 9,732 164 1.71% x 0.98 fmt::write_vec_value 9,516 9,625 109 1.15% x 0.99 ``` </details>
2 parents d5f2ce5 + 10a5983 commit dd5d80a

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

core/src/fmt/mod.rs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use crate::cell::{Cell, Ref, RefCell, RefMut, UnsafeCell};
66
use crate::char::EscapeDebugExtArgs;
7-
use crate::iter;
87
use crate::marker::PhantomData;
98
use crate::mem;
109
use crate::num::fmt as numfmt;
@@ -334,11 +333,29 @@ enum FlagV1 {
334333
impl<'a> Arguments<'a> {
335334
/// When using the format_args!() macro, this function is used to generate the
336335
/// Arguments structure.
336+
#[cfg(not(bootstrap))]
337+
#[doc(hidden)]
338+
#[inline]
339+
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
340+
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
341+
pub const unsafe fn new_v1(
342+
pieces: &'a [&'static str],
343+
args: &'a [ArgumentV1<'a>],
344+
) -> Arguments<'a> {
345+
if pieces.len() < args.len() || pieces.len() > args.len() + 1 {
346+
panic!("invalid args");
347+
}
348+
Arguments { pieces, fmt: None, args }
349+
}
350+
#[cfg(bootstrap)]
337351
#[doc(hidden)]
338352
#[inline]
339353
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
340354
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
341355
pub const fn new_v1(pieces: &'a [&'static str], args: &'a [ArgumentV1<'a>]) -> Arguments<'a> {
356+
if pieces.len() < args.len() || pieces.len() > args.len() + 1 {
357+
panic!("invalid args");
358+
}
342359
Arguments { pieces, fmt: None, args }
343360
}
344361

@@ -348,6 +365,19 @@ impl<'a> Arguments<'a> {
348365
/// `CountIsParam` or `CountIsNextParam` has to point to an argument
349366
/// created with `argumentusize`. However, failing to do so doesn't cause
350367
/// unsafety, but will ignore invalid .
368+
#[cfg(not(bootstrap))]
369+
#[doc(hidden)]
370+
#[inline]
371+
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
372+
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
373+
pub const unsafe fn new_v1_formatted(
374+
pieces: &'a [&'static str],
375+
args: &'a [ArgumentV1<'a>],
376+
fmt: &'a [rt::v1::Argument],
377+
) -> Arguments<'a> {
378+
Arguments { pieces, fmt: Some(fmt), args }
379+
}
380+
#[cfg(bootstrap)]
351381
#[doc(hidden)]
352382
#[inline]
353383
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
@@ -1110,7 +1140,10 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
11101140
match args.fmt {
11111141
None => {
11121142
// We can use default formatting parameters for all arguments.
1113-
for (arg, piece) in iter::zip(args.args, args.pieces) {
1143+
for (i, arg) in args.args.iter().enumerate() {
1144+
// SAFETY: args.args and args.pieces come from the same Arguments,
1145+
// which guarantees the indexes are always within bounds.
1146+
let piece = unsafe { args.pieces.get_unchecked(i) };
11141147
if !piece.is_empty() {
11151148
formatter.buf.write_str(*piece)?;
11161149
}
@@ -1121,7 +1154,10 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
11211154
Some(fmt) => {
11221155
// Every spec has a corresponding argument that is preceded by
11231156
// a string piece.
1124-
for (arg, piece) in iter::zip(fmt, args.pieces) {
1157+
for (i, arg) in fmt.iter().enumerate() {
1158+
// SAFETY: fmt and args.pieces come from the same Arguments,
1159+
// which guarantees the indexes are always within bounds.
1160+
let piece = unsafe { args.pieces.get_unchecked(i) };
11251161
if !piece.is_empty() {
11261162
formatter.buf.write_str(*piece)?;
11271163
}

core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
//
112112
// Language features:
113113
#![feature(abi_unadjusted)]
114+
#![feature(allow_internal_unsafe)]
114115
#![feature(allow_internal_unstable)]
115116
#![feature(asm)]
116117
#![feature(associated_type_bounds)]

core/src/macros/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,7 @@ pub(crate) mod builtin {
828828
/// assert_eq!(s, format!("hello {}", "world"));
829829
/// ```
830830
#[stable(feature = "rust1", since = "1.0.0")]
831+
#[allow_internal_unsafe]
831832
#[allow_internal_unstable(fmt_internals)]
832833
#[rustc_builtin_macro]
833834
#[macro_export]

core/src/panicking.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ pub fn panic(expr: &'static str) -> ! {
4747
// truncation and padding (even though none is used here). Using
4848
// Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
4949
// output binary, saving up to a few kilobytes.
50-
panic_fmt(fmt::Arguments::new_v1(&[expr], &[]));
50+
panic_fmt(
51+
#[cfg(bootstrap)]
52+
fmt::Arguments::new_v1(&[expr], &[]),
53+
#[cfg(not(bootstrap))]
54+
// SAFETY: Arguments::new_v1 is safe with exactly one str and zero args
55+
unsafe {
56+
fmt::Arguments::new_v1(&[expr], &[])
57+
},
58+
);
5159
}
5260

5361
#[inline]

0 commit comments

Comments
 (0)