This repository was archived by the owner on May 28, 2025. It is now read-only.
Commit cf92f86
committed
Auto merge of rust-lang#115129 - m-ou-se:fmt-args-new-repr, r=<try>
New fmt::Arguments representation.
This changes the internal representation of `fmt::Arguments` to reduce it in size and make it possible to convert a `&'static str` to a `fmt::Arguments` without additional indirection.
Part of rust-lang#99012
fmt::Arguments stores three slices:
1. The slice of string literals between the placeholders.
2. The details of the placeholders (format options, which argument to display there, etc.).
3. The arguments.
2 is omitted when it's just all arguments once in order without additional options.
Before this change, fmt::Arguments stores each of these as a &[] (or Option of that), resulting in a total size of six words/pointers.
However, we don't need to store the length of each of these slices separately, as we may (unsafely) assume that the indexes of the fmt::Arguments generated by format_args!() are never out of bounds, and the number of placeholders and number of strings between them must be nearly equal.
This PR changes the struct to store three pointers without length, and a single 'number of parts' counter that is the sum of the number of placeholders and string pieces.
Additionally, this PR adds a special case for 1 part (that is, one string piece and no placeholders) to store the string pointer directly instead of through a slice of length 1.
This makes it possible for `fmt::Arguments::new_str(&'static str)` to exist, which unlike before (`new_const(&[&'static str])`) doesn't need to borrow anything other than the string itself.File tree
10 files changed
+200
-145
lines changed- compiler
- rustc_ast_lowering/src
- rustc_span/src
- library/core/src
- fmt
- num
- tests
- incremental
- hashes
- hygiene/auxiliary
- pretty
- ui
- borrowck
- fmt
10 files changed
+200
-145
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
380 | 380 | | |
381 | 381 | | |
382 | 382 | | |
383 | | - | |
384 | 383 | | |
385 | 384 | | |
386 | 385 | | |
| |||
407 | 406 | | |
408 | 407 | | |
409 | 408 | | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
410 | 427 | | |
411 | 428 | | |
412 | 429 | | |
| |||
421 | 438 | | |
422 | 439 | | |
423 | 440 | | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | 441 | | |
439 | 442 | | |
440 | 443 | | |
| |||
555 | 558 | | |
556 | 559 | | |
557 | 560 | | |
| 561 | + | |
| 562 | + | |
558 | 563 | | |
559 | 564 | | |
560 | 565 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1030 | 1030 | | |
1031 | 1031 | | |
1032 | 1032 | | |
| 1033 | + | |
1033 | 1034 | | |
1034 | 1035 | | |
1035 | 1036 | | |
| |||
0 commit comments