Commit 503dce3
committed
Auto merge of rust-lang#148789 - m-ou-se:new-fmt-args-alt, r=wafflelapkin,jdonszelmann
New format_args!() and fmt::Arguments implementation
Part of rust-lang#99012
This is a new implementation of `format_args!()` and `fmt::Arguments`. With this implementation, `fmt::Arguments` is only two pointers in size. (Instead of six, before.) This makes it the same size as a `&str` and makes it fit in a register pair.
---
This `fmt::Arguments` can store a `&'static str` _without any indirection_ or additional storage. This means that simple cases like `print_fmt(format_args!("hello"))` are now just as efficient for the caller as `print_str("hello")`, as shown by this example:
> code:
> ```rust
> fn main() {
> println!("Hello, world!");
> }
> ```
>
> before:
> ```asm
> main:
> sub rsp, 56
> lea rax, [rip + .Lanon_hello_world]
> mov qword ptr [rsp + 8], rax
> mov qword ptr [rsp + 16], 1
> mov qword ptr [rsp + 24], 8
> xorps xmm0, xmm0
> movups xmmword ptr [rsp + 32], xmm0
> lea rdi, [rsp + 8]
> call qword ptr [rip + std::io::stdio::_print]
> add rsp, 56
> ret
> ```
>
> after:
> ```asm
> main:
> lea rsi, [rip + .Lanon_hello_world]
> mov edi, 29
> jmp qword ptr [rip + std::io::stdio::_print]
> ```
(`panic!("Hello, world!");` shows a similar change.)
---
This implementation stores all static information as just a single (byte) string, without any indirection:
> code:
> ```rust
> format_args!("Hello, {name:-^20}!")
> ```
>
> lowering before:
> ```rust
> fmt::Arguments::new_v1_formatted(
> &["Hello, ", "!\n"],
> &args,
> &[
> Placeholder {
> position: 0usize,
> flags: 3355443245u32,
> precision: format_count::Implied,
> width: format_count::Is(20u16),
> },
> ],
> )
> ```
>
> lowering after:
> ```rust
> fmt::Arguments::new(
> b"\x07Hello, \xc3-\x00\x00\xc8\x14\x00\x02!\n\x00",
> &args,
> )
> ```
This saves a ton of pointers and simplifies the expansion significantly, but does mean that individual pieces (e.g. `"Hello, "` and `"!\n"`) cannot be reused. (Those pieces are often smaller than a pointer to them, though, in which case reusing them is useless.)
---
The details of the new representation are documented in [library/core/src/fmt/mod.rs](https://github.com/m-ou-se/rust/blob/new-fmt-args-alt/library/core/src/fmt/mod.rs#L609-L712).File tree
58 files changed
+886
-989
lines changed- compiler
- rustc_ast_lowering/src
- rustc_hir/src
- rustc_span/src
- library/core/src
- fmt
- src/tools/clippy
- clippy_utils/src
- tests/ui/author
- tests
- codegen-units/item-collection
- coverage
- attr
- incremental
- hashes
- hygiene/auxiliary
- mir-opt
- sroa
- pretty
- run-make/symbol-mangling-hashed
- ui
- consts
- unpretty
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
58 files changed
+886
-989
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
924 | 924 | | |
925 | 925 | | |
926 | 926 | | |
927 | | - | |
| 927 | + | |
928 | 928 | | |
929 | 929 | | |
930 | 930 | | |
| |||
1832 | 1832 | | |
1833 | 1833 | | |
1834 | 1834 | | |
1835 | | - | |
| 1835 | + | |
1836 | 1836 | | |
1837 | 1837 | | |
1838 | 1838 | | |
| |||
1887 | 1887 | | |
1888 | 1888 | | |
1889 | 1889 | | |
1890 | | - | |
| 1890 | + | |
1891 | 1891 | | |
1892 | 1892 | | |
1893 | 1893 | | |
| |||
2103 | 2103 | | |
2104 | 2104 | | |
2105 | 2105 | | |
2106 | | - | |
| 2106 | + | |
2107 | 2107 | | |
2108 | 2108 | | |
2109 | | - | |
| 2109 | + | |
2110 | 2110 | | |
2111 | 2111 | | |
2112 | 2112 | | |
2113 | 2113 | | |
2114 | | - | |
2115 | | - | |
2116 | | - | |
2117 | | - | |
2118 | | - | |
2119 | | - | |
2120 | | - | |
2121 | | - | |
2122 | | - | |
2123 | | - | |
2124 | | - | |
2125 | | - | |
2126 | | - | |
| 2114 | + | |
2127 | 2115 | | |
2128 | 2116 | | |
2129 | | - | |
| 2117 | + | |
2130 | 2118 | | |
2131 | 2119 | | |
2132 | 2120 | | |
| |||
2262 | 2250 | | |
2263 | 2251 | | |
2264 | 2252 | | |
2265 | | - | |
| 2253 | + | |
| 2254 | + | |
| 2255 | + | |
| 2256 | + | |
| 2257 | + | |
2266 | 2258 | | |
2267 | | - | |
2268 | 2259 | | |
2269 | 2260 | | |
2270 | 2261 | | |
| |||
2302 | 2293 | | |
2303 | 2294 | | |
2304 | 2295 | | |
2305 | | - | |
2306 | | - | |
2307 | | - | |
2308 | | - | |
2309 | | - | |
2310 | | - | |
2311 | | - | |
2312 | | - | |
2313 | | - | |
2314 | 2296 | | |
2315 | 2297 | | |
2316 | 2298 | | |
| |||
0 commit comments