Skip to content

Commit 23fdb55

Browse files
committed
Minimize the cost of write_fmt without arguments
1 parent 13290e8 commit 23fdb55

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

library/core/src/fmt/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ pub trait Write {
190190
/// ```
191191
#[stable(feature = "rust1", since = "1.0.0")]
192192
fn write_fmt(mut self: &mut Self, args: Arguments<'_>) -> Result {
193-
write(&mut self, args)
193+
match args.as_str() {
194+
Some(s) => self.write_str(s),
195+
None => write(&mut self, args),
196+
}
194197
}
195198
}
196199

library/std/src/io/mod.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -1506,15 +1506,19 @@ pub trait Write {
15061506
}
15071507
}
15081508

1509-
let mut output = Adaptor { inner: self, error: Ok(()) };
1510-
match fmt::write(&mut output, fmt) {
1511-
Ok(()) => Ok(()),
1512-
Err(..) => {
1513-
// check if the error came from the underlying `Write` or not
1514-
if output.error.is_err() {
1515-
output.error
1516-
} else {
1517-
Err(Error::new(ErrorKind::Other, "formatter error"))
1509+
if let Some(s) = fmt.as_str() {
1510+
self.write_all(s.as_bytes())
1511+
} else {
1512+
let mut output = Adaptor { inner: self, error: Ok(()) };
1513+
match fmt::write(&mut output, fmt) {
1514+
Ok(()) => Ok(()),
1515+
Err(..) => {
1516+
// check if the error came from the underlying `Write` or not
1517+
if output.error.is_err() {
1518+
output.error
1519+
} else {
1520+
Err(Error::new(ErrorKind::Other, "formatter error"))
1521+
}
15181522
}
15191523
}
15201524
}

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@
265265
#![feature(exhaustive_patterns)]
266266
#![feature(extend_one)]
267267
#![feature(external_doc)]
268+
#![feature(fmt_as_str)]
268269
#![feature(fn_traits)]
269270
#![feature(format_args_nl)]
270271
#![feature(future_readiness_fns)]

0 commit comments

Comments
 (0)