Description
Like molasses in the Antarctic.
As a consequence, so is any method which depends on its Arguments, like {fmt, io}::Write::write_fmt
. The microbenchmarks in this issue about write!
's speed demonstrate that merely running the same arguments through format_args!
and then write_fmt
, even if it's just a plain string literal without any formatting required, produces a massive slowdown next to just feeding the same through fmt::Write::write_str
or io::Write::write_all
.
Unfortunately, write!
, format!
, println!
, and other such macros are a common feature of fluent Rust code. Rust promises a lot of zero-cost abstractions, and on a scale from "even better than you could handwrite the asm" to "technically, booting an entire virtual machine is zero cost if you define the expression as booting a virtual machine..." this is currently "not very". Validating and formatting strings correctly can be surprisingly complex, which is going to increase with features like implicit named arguments in format_args!, so we can expect increasing speed here may be challenging. However, this should be possible, even if it might require extensive redesign.
Multiple Problems, Multiple Solutions
format_args!
's internal machinery in the Rust compiler can likely be improved.- Consumers of Arguments, such as
fmt::{format, write}
and{fmt, io}::Write::write_fmt
, can be reviewed for runtime performance. - Macros downstream of
format_args!
often are invoked to do something simple that does not require extensive formatting and can use the pattern-matching feature ofmacro_rules!
to special-case simple patterns to side-stepformat_args!
when it's not needed. This will increase the complexity of those macros and risks breakage if done incautiously, but could be a big gain in itself.
Unfortunately some of these cases may run up against complex situations with types, trait bounds, and method resolutions, because e.g. both io::Write
and fmt::Write
both exist and write!
needs to "serve" both. Fortunately, this is exactly the sort of thing that can benefit from the recent advances in const generics, since it's a lot of compile-time evaluation that could benefit from interacting with types (as opposed to being purely syntactic like macros), and in the future generic associated types and specialization may be able to minimize breakage from type issues as those features come online, so it's a good time to begin reviewing this code.
Related issues and PRs
- Unused
format!()
s are not optimised away #75742 (and Usestr::to_owned
whenformat!
has no formatting arguments #75894) - Improve codegen for single argument format_args! #75301 (and Minimize the cost of write_fmt without arguments #75358)
- Special-case
format!("{}", string_like)
for increased performance #52804 - write!(wr,"foo") is 10% to 72% slower than wr.write("foo".as_bytes()) #10761