Skip to content

Commit

Permalink
perf: use Arguments::as_str to bypass the formatting mechanism if p…
Browse files Browse the repository at this point in the history
…ossible

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)
  • Loading branch information
yvt committed Mar 22, 2022
1 parent aa79a7c commit dc45afb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
20 changes: 16 additions & 4 deletions src/arm_semihosting/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ macro_rules! syscall1 {
#[macro_export]
macro_rules! hprint {
($($tt:tt)*) => {
$crate::export::hstdout_fmt(format_args!($($tt)*))
match ::core::format_args!($($tt)*) {
args => if let ::core::option::Option::Some(s) = args.as_str() {
$crate::export::hstdout_str(s)
} else {
$crate::export::hstdout_fmt(args)
},
}
};
}

Expand All @@ -45,7 +51,7 @@ macro_rules! hprint {
#[macro_export]
macro_rules! hprintln {
($($tt:tt)*) => {
match $crate::export::hstdout_fmt(format_args!($($tt)*)) {
match $crate::hprint!($($tt)*) {
Ok(()) => $crate::export::hstdout_str("\n"),
Err(()) => Err(()),
}
Expand All @@ -58,7 +64,13 @@ macro_rules! hprintln {
#[macro_export]
macro_rules! heprint {
($($tt:tt)*) => {
$crate::export::hstderr_fmt(format_args!($($tt)*))
match ::core::format_args!($($tt)*) {
args => if let ::core::option::Option::Some(s) = args.as_str() {
$crate::export::hstderr_str(s)
} else {
$crate::export::hstderr_fmt(args)
},
}
};
}

Expand All @@ -68,7 +80,7 @@ macro_rules! heprint {
#[macro_export]
macro_rules! heprintln {
($($tt:tt)*) => {
match $crate::export::hstderr_fmt(format_args!($($tt)*)) {
match $crate::heprint!($($tt)*) {
Ok(()) => $crate::export::hstderr_str("\n"),
Err(()) => Err(()),
}
Expand Down
10 changes: 8 additions & 2 deletions src/r3_support_rp2040/src/stdout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,21 @@ pub fn write_fmt(args: fmt::Arguments<'_>) {
#[macro_export]
macro_rules! sprint {
($($tt:tt)*) => {
$crate::stdout::write_fmt(format_args!($($tt)*))
match ::core::format_args!($($tt)*) {
args => if let ::core::option::Option::Some(s) = args.as_str() {
$crate::stdout::write_str(s)
} else {
$crate::stdout::write_fmt(args)
},
}
};
}

/// Macro for printing to the serial standard output, with a newline.
#[macro_export]
macro_rules! sprintln {
($($tt:tt)*) => {{
$crate::stdout::write_fmt(format_args!($($tt)*));
$crate::sprint!($($tt)*);
$crate::stdout::write_str("\n");
}};
}
10 changes: 8 additions & 2 deletions src/r3_support_rza1/src/stdout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,21 @@ pub fn write_fmt(args: fmt::Arguments<'_>) {
#[macro_export]
macro_rules! sprint {
($($tt:tt)*) => {
$crate::stdout::write_fmt(format_args!($($tt)*))
match ::core::format_args!($($tt)*) {
args => if let ::core::option::Option::Some(s) = args.as_str() {
$crate::stdout::write_str(s)
} else {
$crate::stdout::write_fmt(args)
},
}
};
}

/// Macro for printing to the serial standard output, with a newline.
#[macro_export]
macro_rules! sprintln {
($($tt:tt)*) => {{
$crate::stdout::write_fmt(format_args!($($tt)*));
$crate::sprint!($($tt)*);
$crate::stdout::write_str("\n");
}};
}

0 comments on commit dc45afb

Please sign in to comment.