Skip to content

Commit 9c0057d

Browse files
committed
Remove core::panicking::panic's dependence on str's Display::fmt impl
Display::fmt for str calls into Formatter::pad, which is modest in size and also pulls in string-related functions for its truncation and padding abilities. For size-critical programs (e.g. embedded), this call site may be the only reason Formatter::pad is linked into the output.
1 parent 0eb0ba3 commit 9c0057d

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/libcore/panicking.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ use fmt;
3535
#[cold] #[inline(never)] // this is the slow path, always
3636
#[lang="panic"]
3737
pub fn panic(expr_file_line: &(&'static str, &'static str, u32)) -> ! {
38+
// Use Arguments::new_v1 instead of format_args!("{}", expr) to potentially
39+
// reduce size overhead. The format_args! macro uses str's Display trait to
40+
// write expr, which calls Formatter::pad, which must accommodate string
41+
// truncation and padding (even though none is used here). Using
42+
// Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
43+
// output binary, saving up to a few kilobytes.
3844
let (expr, file, line) = *expr_file_line;
39-
panic_fmt(format_args!("{}", expr), &(file, line))
45+
panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), &(file, line))
4046
}
4147

4248
#[cold] #[inline(never)]

0 commit comments

Comments
 (0)