Skip to content

Commit dc45afb

Browse files
committed
perf: use Arguments::as_str to bypass the formatting mechanism if possible
This involves branching, but as shown in [1], the compiler seems to have no problem resolving the branch at compile time if optimization is enabled. [1]: rust-lang/rust#74056 (comment)
1 parent aa79a7c commit dc45afb

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

src/arm_semihosting/src/macros.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ macro_rules! syscall1 {
3535
#[macro_export]
3636
macro_rules! hprint {
3737
($($tt:tt)*) => {
38-
$crate::export::hstdout_fmt(format_args!($($tt)*))
38+
match ::core::format_args!($($tt)*) {
39+
args => if let ::core::option::Option::Some(s) = args.as_str() {
40+
$crate::export::hstdout_str(s)
41+
} else {
42+
$crate::export::hstdout_fmt(args)
43+
},
44+
}
3945
};
4046
}
4147

@@ -45,7 +51,7 @@ macro_rules! hprint {
4551
#[macro_export]
4652
macro_rules! hprintln {
4753
($($tt:tt)*) => {
48-
match $crate::export::hstdout_fmt(format_args!($($tt)*)) {
54+
match $crate::hprint!($($tt)*) {
4955
Ok(()) => $crate::export::hstdout_str("\n"),
5056
Err(()) => Err(()),
5157
}
@@ -58,7 +64,13 @@ macro_rules! hprintln {
5864
#[macro_export]
5965
macro_rules! heprint {
6066
($($tt:tt)*) => {
61-
$crate::export::hstderr_fmt(format_args!($($tt)*))
67+
match ::core::format_args!($($tt)*) {
68+
args => if let ::core::option::Option::Some(s) = args.as_str() {
69+
$crate::export::hstderr_str(s)
70+
} else {
71+
$crate::export::hstderr_fmt(args)
72+
},
73+
}
6274
};
6375
}
6476

@@ -68,7 +80,7 @@ macro_rules! heprint {
6880
#[macro_export]
6981
macro_rules! heprintln {
7082
($($tt:tt)*) => {
71-
match $crate::export::hstderr_fmt(format_args!($($tt)*)) {
83+
match $crate::heprint!($($tt)*) {
7284
Ok(()) => $crate::export::hstderr_str("\n"),
7385
Err(()) => Err(()),
7486
}

src/r3_support_rp2040/src/stdout.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,21 @@ pub fn write_fmt(args: fmt::Arguments<'_>) {
106106
#[macro_export]
107107
macro_rules! sprint {
108108
($($tt:tt)*) => {
109-
$crate::stdout::write_fmt(format_args!($($tt)*))
109+
match ::core::format_args!($($tt)*) {
110+
args => if let ::core::option::Option::Some(s) = args.as_str() {
111+
$crate::stdout::write_str(s)
112+
} else {
113+
$crate::stdout::write_fmt(args)
114+
},
115+
}
110116
};
111117
}
112118

113119
/// Macro for printing to the serial standard output, with a newline.
114120
#[macro_export]
115121
macro_rules! sprintln {
116122
($($tt:tt)*) => {{
117-
$crate::stdout::write_fmt(format_args!($($tt)*));
123+
$crate::sprint!($($tt)*);
118124
$crate::stdout::write_str("\n");
119125
}};
120126
}

src/r3_support_rza1/src/stdout.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,21 @@ pub fn write_fmt(args: fmt::Arguments<'_>) {
102102
#[macro_export]
103103
macro_rules! sprint {
104104
($($tt:tt)*) => {
105-
$crate::stdout::write_fmt(format_args!($($tt)*))
105+
match ::core::format_args!($($tt)*) {
106+
args => if let ::core::option::Option::Some(s) = args.as_str() {
107+
$crate::stdout::write_str(s)
108+
} else {
109+
$crate::stdout::write_fmt(args)
110+
},
111+
}
106112
};
107113
}
108114

109115
/// Macro for printing to the serial standard output, with a newline.
110116
#[macro_export]
111117
macro_rules! sprintln {
112118
($($tt:tt)*) => {{
113-
$crate::stdout::write_fmt(format_args!($($tt)*));
119+
$crate::sprint!($($tt)*);
114120
$crate::stdout::write_str("\n");
115121
}};
116122
}

0 commit comments

Comments
 (0)