New format_args!() and fmt::Arguments implementation - #148789

Merged
bors merged 18 commits into
rust-lang:mainfrom
m-ou-se:new-fmt-args-alt
Nov 13, 2025
Merged

New format_args!() and fmt::Arguments implementation#148789
bors merged 18 commits into
rust-lang:mainfrom
m-ou-se:new-fmt-args-alt

Conversation

@m-ou-se

@m-ou-sem-ou-se commented Nov 10, 2025

Copy link
Copy Markdown
Member

Part of #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 strwithout 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:

fnmain(){println!("Hello, world!");}

before:

main:subrsp,56learax,[rip+ .Lanon_hello_world]mov qword ptr [rsp+8],raxmov qword ptr [rsp+16],1mov qword ptr [rsp+24],8xorpsxmm0,xmm0movups xmmword ptr [rsp+32],xmm0leardi,[rsp+8]call qword ptr [rip+std::io::stdio::_print]addrsp,56ret

after:

main:learsi,[rip+ .Lanon_hello_world]movedi,29jmp 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:

format_args!("Hello, {name:-^20}!")

lowering before:

fmt::Arguments::new_v1_formatted(&["Hello, ","!\n"],&args,&[Placeholder{position:0usize,flags:3355443245u32,precision: format_count::Implied,width: format_count::Is(20u16),},],)

lowering after:

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.


Diagram of the data structure after this change:

A diagram showing the fmt::Arguments internal structure after the change. Most notably, it is only two pointers in size, and all the string data is all part of a single string, removing a level of indirection.
Original data structureA diagram showing the fmt::Arguments internal structure before the change. Most notably, it consists of three slices (so six pointers in size), and one of the slices contains string slices (so another two pointers in size for each string part, and more indirection).

@m-ou-sem-ou-se self-assigned this Nov 10, 2025
@m-ou-sem-ou-se added the A-fmt Area: `core::fmt` label Nov 10, 2025
@rustbotrustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 10, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-borsBot added a commit that referenced this pull request Nov 10, 2025
 Experiment: New fmt::Arguments implementation (another one)
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 10, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-borsBot commented Nov 10, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 6e6ba94 (6e6ba949d24fbfbd9cd48ca4c98adf59fbd04482, parent: a7b3715826827677ca8769eb88dc8052f43e734b)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (6e6ba94): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.7%[0.1%, 5.8%]26
Regressions ❌
(secondary)
0.6%[0.1%, 1.3%]44
Improvements ✅
(primary)
-0.7%[-4.3%, -0.1%]109
Improvements ✅
(secondary)
-1.7%[-38.2%, -0.0%]93
All ❌✅ (primary)-0.5%[-4.3%, 5.8%]135

Max RSS (memory usage)

Results (primary -1.5%, secondary -0.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
2.2%[2.2%, 2.2%]1
Regressions ❌
(secondary)
3.7%[1.0%, 6.7%]12
Improvements ✅
(primary)
-1.6%[-6.0%, -0.5%]31
Improvements ✅
(secondary)
-2.6%[-7.9%, -0.7%]25
All ❌✅ (primary)-1.5%[-6.0%, 2.2%]32

Cycles

Results (primary -0.5%, secondary -4.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
4.8%[3.4%, 6.2%]2
Regressions ❌
(secondary)
8.8%[2.6%, 18.8%]6
Improvements ✅
(primary)
-3.1%[-5.0%, -2.1%]4
Improvements ✅
(secondary)
-10.3%[-39.4%, -2.1%]13
All ❌✅ (primary)-0.5%[-5.0%, 6.2%]6

Binary size

Results (primary -0.7%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.5%[0.0%, 1.4%]4
Regressions ❌
(secondary)
3.2%[0.0%, 7.5%]12
Improvements ✅
(primary)
-0.8%[-3.3%, -0.0%]129
Improvements ✅
(secondary)
-1.7%[-23.6%, -0.0%]123
All ❌✅ (primary)-0.7%[-3.3%, 1.4%]133

Bootstrap: 476.631s -> 471.922s (-0.99%)
Artifact size: 391.32 MiB -> 388.56 MiB (-0.70%)

@rustbotrustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 10, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

Ooh that's pretty good :D

@m-ou-se

m-ou-se commented Nov 10, 2025

Copy link
Copy Markdown
MemberAuthor

Pretty much everything looks like a great improvement. Not only number of instructions executed, but also memory usage and binary size. 🎉

Only two significant negative results:

1. "image-0.25.6 opt incr-patched:println" with almost +6% instructions:u.

Looking at the detailed results, it looks like that's all LLVM. Probably because llvm got more optimization opportunities. That's not necessarily a bad thing.

2. The fmt-write-str runtime benchmark with over +12% instructions:u.

This could be concerning, but I can't seem to fully replicate it locally.

If I recompile and run this benchmark 100 times in both nightly and with this PR, I do get this interesting result though:

Violin plot

With the nightly compiler, the results vary, with many measurements clustered close to 25ms but also many around 40ms. With this PR, the results are very consistent, all clustered around 27ms. (Update: It's around 26ms now, after a minor optimization.)

So, the median result is worse, but the average is better.

My guess is that the indirection (a slice of string slices) can make things unpredictable, as the strings aren't always in the optimal place for caching. The lack of indirection in the new version then makes it much more predictable. This is just a guess though.

@rustbotrustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Nov 11, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-borsBot added a commit that referenced this pull request Nov 11, 2025
 Experiment: New fmt::Arguments implementation (another one)
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 11, 2025
@m-ou-sem-ou-se changed the title Experiment: New fmt::Arguments implementation (another one)New format_args!() and fmt::Arguments implementationNov 11, 2025
@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (503dce3): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.7%[0.1%, 5.7%]17
Regressions ❌
(secondary)
0.6%[0.1%, 1.1%]40
Improvements ✅
(primary)
-0.7%[-4.4%, -0.1%]120
Improvements ✅
(secondary)
-1.6%[-38.5%, -0.0%]106
All ❌✅ (primary)-0.5%[-4.4%, 5.7%]137

Max RSS (memory usage)

Results (primary -1.5%, secondary -1.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
3.8%[1.4%, 7.0%]3
Regressions ❌
(secondary)
3.8%[1.2%, 5.8%]8
Improvements ✅
(primary)
-2.2%[-6.0%, -0.6%]23
Improvements ✅
(secondary)
-3.1%[-7.3%, -0.6%]29
All ❌✅ (primary)-1.5%[-6.0%, 7.0%]26

Cycles

Results (primary -2.3%, secondary 0.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
5.2%[3.4%, 6.9%]2
Regressions ❌
(secondary)
7.6%[1.6%, 18.5%]19
Improvements ✅
(primary)
-4.4%[-7.9%, -1.5%]7
Improvements ✅
(secondary)
-11.0%[-40.0%, -1.8%]12
All ❌✅ (primary)-2.3%[-7.9%, 6.9%]9

Binary size

Results (primary -0.8%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.5%[0.0%, 1.4%]4
Regressions ❌
(secondary)
3.0%[0.0%, 6.9%]12
Improvements ✅
(primary)
-0.8%[-3.3%, -0.0%]129
Improvements ✅
(secondary)
-1.7%[-23.6%, -0.0%]123
All ❌✅ (primary)-0.8%[-3.3%, 1.4%]133

Bootstrap: 476.356s -> 473.309s (-0.64%)
Artifact size: 391.04 MiB -> 388.34 MiB (-0.69%)

@m-ou-se
m-ou-se deleted the new-fmt-args-alt branch November 13, 2025 09:57
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 13, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
rust-timer added a commit that referenced this pull request Nov 14, 2025
Rollup merge of #148906 - m-ou-se:fmt-args-from-str, r=dtolnay
Expose fmt::Arguments::from_str as unstable.
Now that #148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: #148905
SebastianSpeitel

This comment was marked as off-topic.

@m-ou-se

This comment was marked as off-topic.

tautschnig added a commit to tautschnig/kani that referenced this pull request Nov 17, 2025
Relevant upstream PR:
- rust-lang/rust#148789 (New format_args!() and
fmt::Arguments implementation)
Resolves: model-checking#4474
github-merge-queueBot pushed a commit to model-checking/kani that referenced this pull request Nov 17, 2025
Relevant upstream PR:
- rust-lang/rust#148789 (New format_args!() and
fmt::Arguments implementation)
Resolves: #4474
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
@panstromek

Copy link
Copy Markdown
Contributor

Perf triage:

Improvements outweigh regressions.

@rustbot label: +perf-regression-triaged

@rustbotrustbot added the perf-regression-triaged The performance regression has been triaged. label Nov 19, 2025
github-actionsBot pushed a commit to model-checking/verify-rust-std that referenced this pull request Nov 30, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
oxalica added a commit to oxalica/palc that referenced this pull request Mar 19, 2026
It is expected to have a big change since Rust 1.93 includes a significant optimization for `format_args`.
See: <rust-lang/rust#148789>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-fmtArea: `core::fmt`A-run-makeArea: port run-make Makefiles to rmake.rsmerged-by-borsThis PR was explicitly merged by bors.perf-regressionPerformance regression.perf-regression-triagedThe performance regression has been triaged.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-clippyRelevant to the Clippy team.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

14 participants

@m-ou-se@rust-timer@rust-log-analyzer@nyurik@bors@rustbot@Zalathar@jdonszelmann@panstromek@jhpratt@SUPERCILEX@SebastianSpeitel@yhx-12243@WaffleLapkin
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

New format_args!() and fmt::Arguments implementation - #148789

Merged
bors merged 18 commits into
rust-lang:mainfrom
m-ou-se:new-fmt-args-alt
Nov 13, 2025
Merged

New format_args!() and fmt::Arguments implementation#148789
bors merged 18 commits into
rust-lang:mainfrom
m-ou-se:new-fmt-args-alt

Conversation

@m-ou-se

@m-ou-sem-ou-se commented Nov 10, 2025

Copy link
Copy Markdown
Member

Part of #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 strwithout 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:

fnmain(){println!("Hello, world!");}

before:

main:subrsp,56learax,[rip+ .Lanon_hello_world]mov qword ptr [rsp+8],raxmov qword ptr [rsp+16],1mov qword ptr [rsp+24],8xorpsxmm0,xmm0movups xmmword ptr [rsp+32],xmm0leardi,[rsp+8]call qword ptr [rip+std::io::stdio::_print]addrsp,56ret

after:

main:learsi,[rip+ .Lanon_hello_world]movedi,29jmp 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:

format_args!("Hello, {name:-^20}!")

lowering before:

fmt::Arguments::new_v1_formatted(&["Hello, ","!\n"],&args,&[Placeholder{position:0usize,flags:3355443245u32,precision: format_count::Implied,width: format_count::Is(20u16),},],)

lowering after:

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.


Diagram of the data structure after this change:

A diagram showing the fmt::Arguments internal structure after the change. Most notably, it is only two pointers in size, and all the string data is all part of a single string, removing a level of indirection.
Original data structureA diagram showing the fmt::Arguments internal structure before the change. Most notably, it consists of three slices (so six pointers in size), and one of the slices contains string slices (so another two pointers in size for each string part, and more indirection).

@m-ou-sem-ou-se self-assigned this Nov 10, 2025
@m-ou-sem-ou-se added the A-fmt Area: `core::fmt` label Nov 10, 2025
@rustbotrustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 10, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-borsBot added a commit that referenced this pull request Nov 10, 2025
 Experiment: New fmt::Arguments implementation (another one)
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 10, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-borsBot commented Nov 10, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 6e6ba94 (6e6ba949d24fbfbd9cd48ca4c98adf59fbd04482, parent: a7b3715826827677ca8769eb88dc8052f43e734b)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (6e6ba94): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.7%[0.1%, 5.8%]26
Regressions ❌
(secondary)
0.6%[0.1%, 1.3%]44
Improvements ✅
(primary)
-0.7%[-4.3%, -0.1%]109
Improvements ✅
(secondary)
-1.7%[-38.2%, -0.0%]93
All ❌✅ (primary)-0.5%[-4.3%, 5.8%]135

Max RSS (memory usage)

Results (primary -1.5%, secondary -0.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
2.2%[2.2%, 2.2%]1
Regressions ❌
(secondary)
3.7%[1.0%, 6.7%]12
Improvements ✅
(primary)
-1.6%[-6.0%, -0.5%]31
Improvements ✅
(secondary)
-2.6%[-7.9%, -0.7%]25
All ❌✅ (primary)-1.5%[-6.0%, 2.2%]32

Cycles

Results (primary -0.5%, secondary -4.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
4.8%[3.4%, 6.2%]2
Regressions ❌
(secondary)
8.8%[2.6%, 18.8%]6
Improvements ✅
(primary)
-3.1%[-5.0%, -2.1%]4
Improvements ✅
(secondary)
-10.3%[-39.4%, -2.1%]13
All ❌✅ (primary)-0.5%[-5.0%, 6.2%]6

Binary size

Results (primary -0.7%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.5%[0.0%, 1.4%]4
Regressions ❌
(secondary)
3.2%[0.0%, 7.5%]12
Improvements ✅
(primary)
-0.8%[-3.3%, -0.0%]129
Improvements ✅
(secondary)
-1.7%[-23.6%, -0.0%]123
All ❌✅ (primary)-0.7%[-3.3%, 1.4%]133

Bootstrap: 476.631s -> 471.922s (-0.99%)
Artifact size: 391.32 MiB -> 388.56 MiB (-0.70%)

@rustbotrustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 10, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

Ooh that's pretty good :D

@m-ou-se

m-ou-se commented Nov 10, 2025

Copy link
Copy Markdown
MemberAuthor

Pretty much everything looks like a great improvement. Not only number of instructions executed, but also memory usage and binary size. 🎉

Only two significant negative results:

1. "image-0.25.6 opt incr-patched:println" with almost +6% instructions:u.

Looking at the detailed results, it looks like that's all LLVM. Probably because llvm got more optimization opportunities. That's not necessarily a bad thing.

2. The fmt-write-str runtime benchmark with over +12% instructions:u.

This could be concerning, but I can't seem to fully replicate it locally.

If I recompile and run this benchmark 100 times in both nightly and with this PR, I do get this interesting result though:

Violin plot

With the nightly compiler, the results vary, with many measurements clustered close to 25ms but also many around 40ms. With this PR, the results are very consistent, all clustered around 27ms. (Update: It's around 26ms now, after a minor optimization.)

So, the median result is worse, but the average is better.

My guess is that the indirection (a slice of string slices) can make things unpredictable, as the strings aren't always in the optimal place for caching. The lack of indirection in the new version then makes it much more predictable. This is just a guess though.

@rustbotrustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Nov 11, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-borsBot added a commit that referenced this pull request Nov 11, 2025
 Experiment: New fmt::Arguments implementation (another one)
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 11, 2025
@m-ou-sem-ou-se changed the title Experiment: New fmt::Arguments implementation (another one)New format_args!() and fmt::Arguments implementationNov 11, 2025
@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (503dce3): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.7%[0.1%, 5.7%]17
Regressions ❌
(secondary)
0.6%[0.1%, 1.1%]40
Improvements ✅
(primary)
-0.7%[-4.4%, -0.1%]120
Improvements ✅
(secondary)
-1.6%[-38.5%, -0.0%]106
All ❌✅ (primary)-0.5%[-4.4%, 5.7%]137

Max RSS (memory usage)

Results (primary -1.5%, secondary -1.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
3.8%[1.4%, 7.0%]3
Regressions ❌
(secondary)
3.8%[1.2%, 5.8%]8
Improvements ✅
(primary)
-2.2%[-6.0%, -0.6%]23
Improvements ✅
(secondary)
-3.1%[-7.3%, -0.6%]29
All ❌✅ (primary)-1.5%[-6.0%, 7.0%]26

Cycles

Results (primary -2.3%, secondary 0.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
5.2%[3.4%, 6.9%]2
Regressions ❌
(secondary)
7.6%[1.6%, 18.5%]19
Improvements ✅
(primary)
-4.4%[-7.9%, -1.5%]7
Improvements ✅
(secondary)
-11.0%[-40.0%, -1.8%]12
All ❌✅ (primary)-2.3%[-7.9%, 6.9%]9

Binary size

Results (primary -0.8%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.5%[0.0%, 1.4%]4
Regressions ❌
(secondary)
3.0%[0.0%, 6.9%]12
Improvements ✅
(primary)
-0.8%[-3.3%, -0.0%]129
Improvements ✅
(secondary)
-1.7%[-23.6%, -0.0%]123
All ❌✅ (primary)-0.8%[-3.3%, 1.4%]133

Bootstrap: 476.356s -> 473.309s (-0.64%)
Artifact size: 391.04 MiB -> 388.34 MiB (-0.69%)

@m-ou-se
m-ou-se deleted the new-fmt-args-alt branch November 13, 2025 09:57
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 13, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
rust-timer added a commit that referenced this pull request Nov 14, 2025
Rollup merge of #148906 - m-ou-se:fmt-args-from-str, r=dtolnay
Expose fmt::Arguments::from_str as unstable.
Now that #148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: #148905
SebastianSpeitel

This comment was marked as off-topic.

@m-ou-se

This comment was marked as off-topic.

tautschnig added a commit to tautschnig/kani that referenced this pull request Nov 17, 2025
Relevant upstream PR:
- rust-lang/rust#148789 (New format_args!() and
fmt::Arguments implementation)
Resolves: model-checking#4474
github-merge-queueBot pushed a commit to model-checking/kani that referenced this pull request Nov 17, 2025
Relevant upstream PR:
- rust-lang/rust#148789 (New format_args!() and
fmt::Arguments implementation)
Resolves: #4474
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
@panstromek

Copy link
Copy Markdown
Contributor

Perf triage:

Improvements outweigh regressions.

@rustbot label: +perf-regression-triaged

@rustbotrustbot added the perf-regression-triaged The performance regression has been triaged. label Nov 19, 2025
github-actionsBot pushed a commit to model-checking/verify-rust-std that referenced this pull request Nov 30, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
oxalica added a commit to oxalica/palc that referenced this pull request Mar 19, 2026
It is expected to have a big change since Rust 1.93 includes a significant optimization for `format_args`.
See: <rust-lang/rust#148789>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-fmtArea: `core::fmt`A-run-makeArea: port run-make Makefiles to rmake.rsmerged-by-borsThis PR was explicitly merged by bors.perf-regressionPerformance regression.perf-regression-triagedThe performance regression has been triaged.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-clippyRelevant to the Clippy team.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

14 participants

@m-ou-se@rust-timer@rust-log-analyzer@nyurik@bors@rustbot@Zalathar@jdonszelmann@panstromek@jhpratt@SUPERCILEX@SebastianSpeitel@yhx-12243@WaffleLapkin
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

New format_args!() and fmt::Arguments implementation - #148789

Merged
bors merged 18 commits into
rust-lang:mainfrom
m-ou-se:new-fmt-args-alt
Nov 13, 2025
Merged

New format_args!() and fmt::Arguments implementation#148789
bors merged 18 commits into
rust-lang:mainfrom
m-ou-se:new-fmt-args-alt

Conversation

@m-ou-se

@m-ou-sem-ou-se commented Nov 10, 2025

Copy link
Copy Markdown
Member

Part of #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 strwithout 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:

fnmain(){println!("Hello, world!");}

before:

main:subrsp,56learax,[rip+ .Lanon_hello_world]mov qword ptr [rsp+8],raxmov qword ptr [rsp+16],1mov qword ptr [rsp+24],8xorpsxmm0,xmm0movups xmmword ptr [rsp+32],xmm0leardi,[rsp+8]call qword ptr [rip+std::io::stdio::_print]addrsp,56ret

after:

main:learsi,[rip+ .Lanon_hello_world]movedi,29jmp 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:

format_args!("Hello, {name:-^20}!")

lowering before:

fmt::Arguments::new_v1_formatted(&["Hello, ","!\n"],&args,&[Placeholder{position:0usize,flags:3355443245u32,precision: format_count::Implied,width: format_count::Is(20u16),},],)

lowering after:

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.


Diagram of the data structure after this change:

A diagram showing the fmt::Arguments internal structure after the change. Most notably, it is only two pointers in size, and all the string data is all part of a single string, removing a level of indirection.
Original data structureA diagram showing the fmt::Arguments internal structure before the change. Most notably, it consists of three slices (so six pointers in size), and one of the slices contains string slices (so another two pointers in size for each string part, and more indirection).

@m-ou-sem-ou-se self-assigned this Nov 10, 2025
@m-ou-sem-ou-se added the A-fmt Area: `core::fmt` label Nov 10, 2025
@rustbotrustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 10, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-borsBot added a commit that referenced this pull request Nov 10, 2025
 Experiment: New fmt::Arguments implementation (another one)
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 10, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-borsBot commented Nov 10, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 6e6ba94 (6e6ba949d24fbfbd9cd48ca4c98adf59fbd04482, parent: a7b3715826827677ca8769eb88dc8052f43e734b)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (6e6ba94): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.7%[0.1%, 5.8%]26
Regressions ❌
(secondary)
0.6%[0.1%, 1.3%]44
Improvements ✅
(primary)
-0.7%[-4.3%, -0.1%]109
Improvements ✅
(secondary)
-1.7%[-38.2%, -0.0%]93
All ❌✅ (primary)-0.5%[-4.3%, 5.8%]135

Max RSS (memory usage)

Results (primary -1.5%, secondary -0.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
2.2%[2.2%, 2.2%]1
Regressions ❌
(secondary)
3.7%[1.0%, 6.7%]12
Improvements ✅
(primary)
-1.6%[-6.0%, -0.5%]31
Improvements ✅
(secondary)
-2.6%[-7.9%, -0.7%]25
All ❌✅ (primary)-1.5%[-6.0%, 2.2%]32

Cycles

Results (primary -0.5%, secondary -4.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
4.8%[3.4%, 6.2%]2
Regressions ❌
(secondary)
8.8%[2.6%, 18.8%]6
Improvements ✅
(primary)
-3.1%[-5.0%, -2.1%]4
Improvements ✅
(secondary)
-10.3%[-39.4%, -2.1%]13
All ❌✅ (primary)-0.5%[-5.0%, 6.2%]6

Binary size

Results (primary -0.7%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.5%[0.0%, 1.4%]4
Regressions ❌
(secondary)
3.2%[0.0%, 7.5%]12
Improvements ✅
(primary)
-0.8%[-3.3%, -0.0%]129
Improvements ✅
(secondary)
-1.7%[-23.6%, -0.0%]123
All ❌✅ (primary)-0.7%[-3.3%, 1.4%]133

Bootstrap: 476.631s -> 471.922s (-0.99%)
Artifact size: 391.32 MiB -> 388.56 MiB (-0.70%)

@rustbotrustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 10, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

Ooh that's pretty good :D

@m-ou-se

m-ou-se commented Nov 10, 2025

Copy link
Copy Markdown
MemberAuthor

Pretty much everything looks like a great improvement. Not only number of instructions executed, but also memory usage and binary size. 🎉

Only two significant negative results:

1. "image-0.25.6 opt incr-patched:println" with almost +6% instructions:u.

Looking at the detailed results, it looks like that's all LLVM. Probably because llvm got more optimization opportunities. That's not necessarily a bad thing.

2. The fmt-write-str runtime benchmark with over +12% instructions:u.

This could be concerning, but I can't seem to fully replicate it locally.

If I recompile and run this benchmark 100 times in both nightly and with this PR, I do get this interesting result though:

Violin plot

With the nightly compiler, the results vary, with many measurements clustered close to 25ms but also many around 40ms. With this PR, the results are very consistent, all clustered around 27ms. (Update: It's around 26ms now, after a minor optimization.)

So, the median result is worse, but the average is better.

My guess is that the indirection (a slice of string slices) can make things unpredictable, as the strings aren't always in the optimal place for caching. The lack of indirection in the new version then makes it much more predictable. This is just a guess though.

@rustbotrustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Nov 11, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-borsBot added a commit that referenced this pull request Nov 11, 2025
 Experiment: New fmt::Arguments implementation (another one)
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 11, 2025
@m-ou-sem-ou-se changed the title Experiment: New fmt::Arguments implementation (another one)New format_args!() and fmt::Arguments implementationNov 11, 2025
@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (503dce3): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.7%[0.1%, 5.7%]17
Regressions ❌
(secondary)
0.6%[0.1%, 1.1%]40
Improvements ✅
(primary)
-0.7%[-4.4%, -0.1%]120
Improvements ✅
(secondary)
-1.6%[-38.5%, -0.0%]106
All ❌✅ (primary)-0.5%[-4.4%, 5.7%]137

Max RSS (memory usage)

Results (primary -1.5%, secondary -1.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
3.8%[1.4%, 7.0%]3
Regressions ❌
(secondary)
3.8%[1.2%, 5.8%]8
Improvements ✅
(primary)
-2.2%[-6.0%, -0.6%]23
Improvements ✅
(secondary)
-3.1%[-7.3%, -0.6%]29
All ❌✅ (primary)-1.5%[-6.0%, 7.0%]26

Cycles

Results (primary -2.3%, secondary 0.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
5.2%[3.4%, 6.9%]2
Regressions ❌
(secondary)
7.6%[1.6%, 18.5%]19
Improvements ✅
(primary)
-4.4%[-7.9%, -1.5%]7
Improvements ✅
(secondary)
-11.0%[-40.0%, -1.8%]12
All ❌✅ (primary)-2.3%[-7.9%, 6.9%]9

Binary size

Results (primary -0.8%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.5%[0.0%, 1.4%]4
Regressions ❌
(secondary)
3.0%[0.0%, 6.9%]12
Improvements ✅
(primary)
-0.8%[-3.3%, -0.0%]129
Improvements ✅
(secondary)
-1.7%[-23.6%, -0.0%]123
All ❌✅ (primary)-0.8%[-3.3%, 1.4%]133

Bootstrap: 476.356s -> 473.309s (-0.64%)
Artifact size: 391.04 MiB -> 388.34 MiB (-0.69%)

@m-ou-se
m-ou-se deleted the new-fmt-args-alt branch November 13, 2025 09:57
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 13, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
rust-timer added a commit that referenced this pull request Nov 14, 2025
Rollup merge of #148906 - m-ou-se:fmt-args-from-str, r=dtolnay
Expose fmt::Arguments::from_str as unstable.
Now that #148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: #148905
SebastianSpeitel

This comment was marked as off-topic.

@m-ou-se

This comment was marked as off-topic.

tautschnig added a commit to tautschnig/kani that referenced this pull request Nov 17, 2025
Relevant upstream PR:
- rust-lang/rust#148789 (New format_args!() and
fmt::Arguments implementation)
Resolves: model-checking#4474
github-merge-queueBot pushed a commit to model-checking/kani that referenced this pull request Nov 17, 2025
Relevant upstream PR:
- rust-lang/rust#148789 (New format_args!() and
fmt::Arguments implementation)
Resolves: #4474
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
@panstromek

Copy link
Copy Markdown
Contributor

Perf triage:

Improvements outweigh regressions.

@rustbot label: +perf-regression-triaged

@rustbotrustbot added the perf-regression-triaged The performance regression has been triaged. label Nov 19, 2025
github-actionsBot pushed a commit to model-checking/verify-rust-std that referenced this pull request Nov 30, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
oxalica added a commit to oxalica/palc that referenced this pull request Mar 19, 2026
It is expected to have a big change since Rust 1.93 includes a significant optimization for `format_args`.
See: <rust-lang/rust#148789>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-fmtArea: `core::fmt`A-run-makeArea: port run-make Makefiles to rmake.rsmerged-by-borsThis PR was explicitly merged by bors.perf-regressionPerformance regression.perf-regression-triagedThe performance regression has been triaged.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-clippyRelevant to the Clippy team.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

14 participants

@m-ou-se@rust-timer@rust-log-analyzer@nyurik@bors@rustbot@Zalathar@jdonszelmann@panstromek@jhpratt@SUPERCILEX@SebastianSpeitel@yhx-12243@WaffleLapkin
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

New format_args!() and fmt::Arguments implementation - #148789

Merged
bors merged 18 commits into
rust-lang:mainfrom
m-ou-se:new-fmt-args-alt
Nov 13, 2025
Merged

New format_args!() and fmt::Arguments implementation#148789
bors merged 18 commits into
rust-lang:mainfrom
m-ou-se:new-fmt-args-alt

Conversation

@m-ou-se

@m-ou-sem-ou-se commented Nov 10, 2025

Copy link
Copy Markdown
Member

Part of #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 strwithout 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:

fnmain(){println!("Hello, world!");}

before:

main:subrsp,56learax,[rip+ .Lanon_hello_world]mov qword ptr [rsp+8],raxmov qword ptr [rsp+16],1mov qword ptr [rsp+24],8xorpsxmm0,xmm0movups xmmword ptr [rsp+32],xmm0leardi,[rsp+8]call qword ptr [rip+std::io::stdio::_print]addrsp,56ret

after:

main:learsi,[rip+ .Lanon_hello_world]movedi,29jmp 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:

format_args!("Hello, {name:-^20}!")

lowering before:

fmt::Arguments::new_v1_formatted(&["Hello, ","!\n"],&args,&[Placeholder{position:0usize,flags:3355443245u32,precision: format_count::Implied,width: format_count::Is(20u16),},],)

lowering after:

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.


Diagram of the data structure after this change:

A diagram showing the fmt::Arguments internal structure after the change. Most notably, it is only two pointers in size, and all the string data is all part of a single string, removing a level of indirection.
Original data structureA diagram showing the fmt::Arguments internal structure before the change. Most notably, it consists of three slices (so six pointers in size), and one of the slices contains string slices (so another two pointers in size for each string part, and more indirection).

@m-ou-sem-ou-se self-assigned this Nov 10, 2025
@m-ou-sem-ou-se added the A-fmt Area: `core::fmt` label Nov 10, 2025
@rustbotrustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 10, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-borsBot added a commit that referenced this pull request Nov 10, 2025
 Experiment: New fmt::Arguments implementation (another one)
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 10, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-borsBot commented Nov 10, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 6e6ba94 (6e6ba949d24fbfbd9cd48ca4c98adf59fbd04482, parent: a7b3715826827677ca8769eb88dc8052f43e734b)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (6e6ba94): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.7%[0.1%, 5.8%]26
Regressions ❌
(secondary)
0.6%[0.1%, 1.3%]44
Improvements ✅
(primary)
-0.7%[-4.3%, -0.1%]109
Improvements ✅
(secondary)
-1.7%[-38.2%, -0.0%]93
All ❌✅ (primary)-0.5%[-4.3%, 5.8%]135

Max RSS (memory usage)

Results (primary -1.5%, secondary -0.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
2.2%[2.2%, 2.2%]1
Regressions ❌
(secondary)
3.7%[1.0%, 6.7%]12
Improvements ✅
(primary)
-1.6%[-6.0%, -0.5%]31
Improvements ✅
(secondary)
-2.6%[-7.9%, -0.7%]25
All ❌✅ (primary)-1.5%[-6.0%, 2.2%]32

Cycles

Results (primary -0.5%, secondary -4.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
4.8%[3.4%, 6.2%]2
Regressions ❌
(secondary)
8.8%[2.6%, 18.8%]6
Improvements ✅
(primary)
-3.1%[-5.0%, -2.1%]4
Improvements ✅
(secondary)
-10.3%[-39.4%, -2.1%]13
All ❌✅ (primary)-0.5%[-5.0%, 6.2%]6

Binary size

Results (primary -0.7%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.5%[0.0%, 1.4%]4
Regressions ❌
(secondary)
3.2%[0.0%, 7.5%]12
Improvements ✅
(primary)
-0.8%[-3.3%, -0.0%]129
Improvements ✅
(secondary)
-1.7%[-23.6%, -0.0%]123
All ❌✅ (primary)-0.7%[-3.3%, 1.4%]133

Bootstrap: 476.631s -> 471.922s (-0.99%)
Artifact size: 391.32 MiB -> 388.56 MiB (-0.70%)

@rustbotrustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 10, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

Ooh that's pretty good :D

@m-ou-se

m-ou-se commented Nov 10, 2025

Copy link
Copy Markdown
MemberAuthor

Pretty much everything looks like a great improvement. Not only number of instructions executed, but also memory usage and binary size. 🎉

Only two significant negative results:

1. "image-0.25.6 opt incr-patched:println" with almost +6% instructions:u.

Looking at the detailed results, it looks like that's all LLVM. Probably because llvm got more optimization opportunities. That's not necessarily a bad thing.

2. The fmt-write-str runtime benchmark with over +12% instructions:u.

This could be concerning, but I can't seem to fully replicate it locally.

If I recompile and run this benchmark 100 times in both nightly and with this PR, I do get this interesting result though:

Violin plot

With the nightly compiler, the results vary, with many measurements clustered close to 25ms but also many around 40ms. With this PR, the results are very consistent, all clustered around 27ms. (Update: It's around 26ms now, after a minor optimization.)

So, the median result is worse, but the average is better.

My guess is that the indirection (a slice of string slices) can make things unpredictable, as the strings aren't always in the optimal place for caching. The lack of indirection in the new version then makes it much more predictable. This is just a guess though.

@rustbotrustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Nov 11, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-borsBot added a commit that referenced this pull request Nov 11, 2025
 Experiment: New fmt::Arguments implementation (another one)
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 11, 2025
@m-ou-sem-ou-se changed the title Experiment: New fmt::Arguments implementation (another one)New format_args!() and fmt::Arguments implementationNov 11, 2025
@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (503dce3): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.7%[0.1%, 5.7%]17
Regressions ❌
(secondary)
0.6%[0.1%, 1.1%]40
Improvements ✅
(primary)
-0.7%[-4.4%, -0.1%]120
Improvements ✅
(secondary)
-1.6%[-38.5%, -0.0%]106
All ❌✅ (primary)-0.5%[-4.4%, 5.7%]137

Max RSS (memory usage)

Results (primary -1.5%, secondary -1.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
3.8%[1.4%, 7.0%]3
Regressions ❌
(secondary)
3.8%[1.2%, 5.8%]8
Improvements ✅
(primary)
-2.2%[-6.0%, -0.6%]23
Improvements ✅
(secondary)
-3.1%[-7.3%, -0.6%]29
All ❌✅ (primary)-1.5%[-6.0%, 7.0%]26

Cycles

Results (primary -2.3%, secondary 0.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
5.2%[3.4%, 6.9%]2
Regressions ❌
(secondary)
7.6%[1.6%, 18.5%]19
Improvements ✅
(primary)
-4.4%[-7.9%, -1.5%]7
Improvements ✅
(secondary)
-11.0%[-40.0%, -1.8%]12
All ❌✅ (primary)-2.3%[-7.9%, 6.9%]9

Binary size

Results (primary -0.8%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.5%[0.0%, 1.4%]4
Regressions ❌
(secondary)
3.0%[0.0%, 6.9%]12
Improvements ✅
(primary)
-0.8%[-3.3%, -0.0%]129
Improvements ✅
(secondary)
-1.7%[-23.6%, -0.0%]123
All ❌✅ (primary)-0.8%[-3.3%, 1.4%]133

Bootstrap: 476.356s -> 473.309s (-0.64%)
Artifact size: 391.04 MiB -> 388.34 MiB (-0.69%)

@m-ou-se
m-ou-se deleted the new-fmt-args-alt branch November 13, 2025 09:57
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 13, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
rust-timer added a commit that referenced this pull request Nov 14, 2025
Rollup merge of #148906 - m-ou-se:fmt-args-from-str, r=dtolnay
Expose fmt::Arguments::from_str as unstable.
Now that #148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: #148905
SebastianSpeitel

This comment was marked as off-topic.

@m-ou-se

This comment was marked as off-topic.

tautschnig added a commit to tautschnig/kani that referenced this pull request Nov 17, 2025
Relevant upstream PR:
- rust-lang/rust#148789 (New format_args!() and
fmt::Arguments implementation)
Resolves: model-checking#4474
github-merge-queueBot pushed a commit to model-checking/kani that referenced this pull request Nov 17, 2025
Relevant upstream PR:
- rust-lang/rust#148789 (New format_args!() and
fmt::Arguments implementation)
Resolves: #4474
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
@panstromek

Copy link
Copy Markdown
Contributor

Perf triage:

Improvements outweigh regressions.

@rustbot label: +perf-regression-triaged

@rustbotrustbot added the perf-regression-triaged The performance regression has been triaged. label Nov 19, 2025
github-actionsBot pushed a commit to model-checking/verify-rust-std that referenced this pull request Nov 30, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
oxalica added a commit to oxalica/palc that referenced this pull request Mar 19, 2026
It is expected to have a big change since Rust 1.93 includes a significant optimization for `format_args`.
See: <rust-lang/rust#148789>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-fmtArea: `core::fmt`A-run-makeArea: port run-make Makefiles to rmake.rsmerged-by-borsThis PR was explicitly merged by bors.perf-regressionPerformance regression.perf-regression-triagedThe performance regression has been triaged.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-clippyRelevant to the Clippy team.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

14 participants

@m-ou-se@rust-timer@rust-log-analyzer@nyurik@bors@rustbot@Zalathar@jdonszelmann@panstromek@jhpratt@SUPERCILEX@SebastianSpeitel@yhx-12243@WaffleLapkin
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

New format_args!() and fmt::Arguments implementation - #148789

Merged
bors merged 18 commits into
rust-lang:mainfrom
m-ou-se:new-fmt-args-alt
Nov 13, 2025
Merged

New format_args!() and fmt::Arguments implementation#148789
bors merged 18 commits into
rust-lang:mainfrom
m-ou-se:new-fmt-args-alt

Conversation

@m-ou-se

@m-ou-sem-ou-se commented Nov 10, 2025

Copy link
Copy Markdown
Member

Part of #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 strwithout 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:

fnmain(){println!("Hello, world!");}

before:

main:subrsp,56learax,[rip+ .Lanon_hello_world]mov qword ptr [rsp+8],raxmov qword ptr [rsp+16],1mov qword ptr [rsp+24],8xorpsxmm0,xmm0movups xmmword ptr [rsp+32],xmm0leardi,[rsp+8]call qword ptr [rip+std::io::stdio::_print]addrsp,56ret

after:

main:learsi,[rip+ .Lanon_hello_world]movedi,29jmp 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:

format_args!("Hello, {name:-^20}!")

lowering before:

fmt::Arguments::new_v1_formatted(&["Hello, ","!\n"],&args,&[Placeholder{position:0usize,flags:3355443245u32,precision: format_count::Implied,width: format_count::Is(20u16),},],)

lowering after:

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.


Diagram of the data structure after this change:

A diagram showing the fmt::Arguments internal structure after the change. Most notably, it is only two pointers in size, and all the string data is all part of a single string, removing a level of indirection.
Original data structureA diagram showing the fmt::Arguments internal structure before the change. Most notably, it consists of three slices (so six pointers in size), and one of the slices contains string slices (so another two pointers in size for each string part, and more indirection).

@m-ou-sem-ou-se self-assigned this Nov 10, 2025
@m-ou-sem-ou-se added the A-fmt Area: `core::fmt` label Nov 10, 2025
@rustbotrustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 10, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-borsBot added a commit that referenced this pull request Nov 10, 2025
 Experiment: New fmt::Arguments implementation (another one)
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 10, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-borsBot commented Nov 10, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 6e6ba94 (6e6ba949d24fbfbd9cd48ca4c98adf59fbd04482, parent: a7b3715826827677ca8769eb88dc8052f43e734b)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (6e6ba94): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.7%[0.1%, 5.8%]26
Regressions ❌
(secondary)
0.6%[0.1%, 1.3%]44
Improvements ✅
(primary)
-0.7%[-4.3%, -0.1%]109
Improvements ✅
(secondary)
-1.7%[-38.2%, -0.0%]93
All ❌✅ (primary)-0.5%[-4.3%, 5.8%]135

Max RSS (memory usage)

Results (primary -1.5%, secondary -0.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
2.2%[2.2%, 2.2%]1
Regressions ❌
(secondary)
3.7%[1.0%, 6.7%]12
Improvements ✅
(primary)
-1.6%[-6.0%, -0.5%]31
Improvements ✅
(secondary)
-2.6%[-7.9%, -0.7%]25
All ❌✅ (primary)-1.5%[-6.0%, 2.2%]32

Cycles

Results (primary -0.5%, secondary -4.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
4.8%[3.4%, 6.2%]2
Regressions ❌
(secondary)
8.8%[2.6%, 18.8%]6
Improvements ✅
(primary)
-3.1%[-5.0%, -2.1%]4
Improvements ✅
(secondary)
-10.3%[-39.4%, -2.1%]13
All ❌✅ (primary)-0.5%[-5.0%, 6.2%]6

Binary size

Results (primary -0.7%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.5%[0.0%, 1.4%]4
Regressions ❌
(secondary)
3.2%[0.0%, 7.5%]12
Improvements ✅
(primary)
-0.8%[-3.3%, -0.0%]129
Improvements ✅
(secondary)
-1.7%[-23.6%, -0.0%]123
All ❌✅ (primary)-0.7%[-3.3%, 1.4%]133

Bootstrap: 476.631s -> 471.922s (-0.99%)
Artifact size: 391.32 MiB -> 388.56 MiB (-0.70%)

@rustbotrustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 10, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

Ooh that's pretty good :D

@m-ou-se

m-ou-se commented Nov 10, 2025

Copy link
Copy Markdown
MemberAuthor

Pretty much everything looks like a great improvement. Not only number of instructions executed, but also memory usage and binary size. 🎉

Only two significant negative results:

1. "image-0.25.6 opt incr-patched:println" with almost +6% instructions:u.

Looking at the detailed results, it looks like that's all LLVM. Probably because llvm got more optimization opportunities. That's not necessarily a bad thing.

2. The fmt-write-str runtime benchmark with over +12% instructions:u.

This could be concerning, but I can't seem to fully replicate it locally.

If I recompile and run this benchmark 100 times in both nightly and with this PR, I do get this interesting result though:

Violin plot

With the nightly compiler, the results vary, with many measurements clustered close to 25ms but also many around 40ms. With this PR, the results are very consistent, all clustered around 27ms. (Update: It's around 26ms now, after a minor optimization.)

So, the median result is worse, but the average is better.

My guess is that the indirection (a slice of string slices) can make things unpredictable, as the strings aren't always in the optimal place for caching. The lack of indirection in the new version then makes it much more predictable. This is just a guess though.

@rustbotrustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Nov 11, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-borsBot added a commit that referenced this pull request Nov 11, 2025
 Experiment: New fmt::Arguments implementation (another one)
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 11, 2025
@m-ou-sem-ou-se changed the title Experiment: New fmt::Arguments implementation (another one)New format_args!() and fmt::Arguments implementationNov 11, 2025
@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (503dce3): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.7%[0.1%, 5.7%]17
Regressions ❌
(secondary)
0.6%[0.1%, 1.1%]40
Improvements ✅
(primary)
-0.7%[-4.4%, -0.1%]120
Improvements ✅
(secondary)
-1.6%[-38.5%, -0.0%]106
All ❌✅ (primary)-0.5%[-4.4%, 5.7%]137

Max RSS (memory usage)

Results (primary -1.5%, secondary -1.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
3.8%[1.4%, 7.0%]3
Regressions ❌
(secondary)
3.8%[1.2%, 5.8%]8
Improvements ✅
(primary)
-2.2%[-6.0%, -0.6%]23
Improvements ✅
(secondary)
-3.1%[-7.3%, -0.6%]29
All ❌✅ (primary)-1.5%[-6.0%, 7.0%]26

Cycles

Results (primary -2.3%, secondary 0.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
5.2%[3.4%, 6.9%]2
Regressions ❌
(secondary)
7.6%[1.6%, 18.5%]19
Improvements ✅
(primary)
-4.4%[-7.9%, -1.5%]7
Improvements ✅
(secondary)
-11.0%[-40.0%, -1.8%]12
All ❌✅ (primary)-2.3%[-7.9%, 6.9%]9

Binary size

Results (primary -0.8%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.5%[0.0%, 1.4%]4
Regressions ❌
(secondary)
3.0%[0.0%, 6.9%]12
Improvements ✅
(primary)
-0.8%[-3.3%, -0.0%]129
Improvements ✅
(secondary)
-1.7%[-23.6%, -0.0%]123
All ❌✅ (primary)-0.8%[-3.3%, 1.4%]133

Bootstrap: 476.356s -> 473.309s (-0.64%)
Artifact size: 391.04 MiB -> 388.34 MiB (-0.69%)

@m-ou-se
m-ou-se deleted the new-fmt-args-alt branch November 13, 2025 09:57
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 13, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
rust-timer added a commit that referenced this pull request Nov 14, 2025
Rollup merge of #148906 - m-ou-se:fmt-args-from-str, r=dtolnay
Expose fmt::Arguments::from_str as unstable.
Now that #148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: #148905
SebastianSpeitel

This comment was marked as off-topic.

@m-ou-se

This comment was marked as off-topic.

tautschnig added a commit to tautschnig/kani that referenced this pull request Nov 17, 2025
Relevant upstream PR:
- rust-lang/rust#148789 (New format_args!() and
fmt::Arguments implementation)
Resolves: model-checking#4474
github-merge-queueBot pushed a commit to model-checking/kani that referenced this pull request Nov 17, 2025
Relevant upstream PR:
- rust-lang/rust#148789 (New format_args!() and
fmt::Arguments implementation)
Resolves: #4474
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
@panstromek

Copy link
Copy Markdown
Contributor

Perf triage:

Improvements outweigh regressions.

@rustbot label: +perf-regression-triaged

@rustbotrustbot added the perf-regression-triaged The performance regression has been triaged. label Nov 19, 2025
github-actionsBot pushed a commit to model-checking/verify-rust-std that referenced this pull request Nov 30, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
oxalica added a commit to oxalica/palc that referenced this pull request Mar 19, 2026
It is expected to have a big change since Rust 1.93 includes a significant optimization for `format_args`.
See: <rust-lang/rust#148789>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-fmtArea: `core::fmt`A-run-makeArea: port run-make Makefiles to rmake.rsmerged-by-borsThis PR was explicitly merged by bors.perf-regressionPerformance regression.perf-regression-triagedThe performance regression has been triaged.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-clippyRelevant to the Clippy team.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

14 participants

@m-ou-se@rust-timer@rust-log-analyzer@nyurik@bors@rustbot@Zalathar@jdonszelmann@panstromek@jhpratt@SUPERCILEX@SebastianSpeitel@yhx-12243@WaffleLapkin
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

New format_args!() and fmt::Arguments implementation - #148789

Merged
bors merged 18 commits into
rust-lang:mainfrom
m-ou-se:new-fmt-args-alt
Nov 13, 2025
Merged

New format_args!() and fmt::Arguments implementation#148789
bors merged 18 commits into
rust-lang:mainfrom
m-ou-se:new-fmt-args-alt

Conversation

@m-ou-se

@m-ou-sem-ou-se commented Nov 10, 2025

Copy link
Copy Markdown
Member

Part of #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 strwithout 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:

fnmain(){println!("Hello, world!");}

before:

main:subrsp,56learax,[rip+ .Lanon_hello_world]mov qword ptr [rsp+8],raxmov qword ptr [rsp+16],1mov qword ptr [rsp+24],8xorpsxmm0,xmm0movups xmmword ptr [rsp+32],xmm0leardi,[rsp+8]call qword ptr [rip+std::io::stdio::_print]addrsp,56ret

after:

main:learsi,[rip+ .Lanon_hello_world]movedi,29jmp 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:

format_args!("Hello, {name:-^20}!")

lowering before:

fmt::Arguments::new_v1_formatted(&["Hello, ","!\n"],&args,&[Placeholder{position:0usize,flags:3355443245u32,precision: format_count::Implied,width: format_count::Is(20u16),},],)

lowering after:

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.


Diagram of the data structure after this change:

A diagram showing the fmt::Arguments internal structure after the change. Most notably, it is only two pointers in size, and all the string data is all part of a single string, removing a level of indirection.
Original data structureA diagram showing the fmt::Arguments internal structure before the change. Most notably, it consists of three slices (so six pointers in size), and one of the slices contains string slices (so another two pointers in size for each string part, and more indirection).

@m-ou-sem-ou-se self-assigned this Nov 10, 2025
@m-ou-sem-ou-se added the A-fmt Area: `core::fmt` label Nov 10, 2025
@rustbotrustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 10, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-borsBot added a commit that referenced this pull request Nov 10, 2025
 Experiment: New fmt::Arguments implementation (another one)
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 10, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-borsBot commented Nov 10, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 6e6ba94 (6e6ba949d24fbfbd9cd48ca4c98adf59fbd04482, parent: a7b3715826827677ca8769eb88dc8052f43e734b)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (6e6ba94): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.7%[0.1%, 5.8%]26
Regressions ❌
(secondary)
0.6%[0.1%, 1.3%]44
Improvements ✅
(primary)
-0.7%[-4.3%, -0.1%]109
Improvements ✅
(secondary)
-1.7%[-38.2%, -0.0%]93
All ❌✅ (primary)-0.5%[-4.3%, 5.8%]135

Max RSS (memory usage)

Results (primary -1.5%, secondary -0.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
2.2%[2.2%, 2.2%]1
Regressions ❌
(secondary)
3.7%[1.0%, 6.7%]12
Improvements ✅
(primary)
-1.6%[-6.0%, -0.5%]31
Improvements ✅
(secondary)
-2.6%[-7.9%, -0.7%]25
All ❌✅ (primary)-1.5%[-6.0%, 2.2%]32

Cycles

Results (primary -0.5%, secondary -4.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
4.8%[3.4%, 6.2%]2
Regressions ❌
(secondary)
8.8%[2.6%, 18.8%]6
Improvements ✅
(primary)
-3.1%[-5.0%, -2.1%]4
Improvements ✅
(secondary)
-10.3%[-39.4%, -2.1%]13
All ❌✅ (primary)-0.5%[-5.0%, 6.2%]6

Binary size

Results (primary -0.7%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.5%[0.0%, 1.4%]4
Regressions ❌
(secondary)
3.2%[0.0%, 7.5%]12
Improvements ✅
(primary)
-0.8%[-3.3%, -0.0%]129
Improvements ✅
(secondary)
-1.7%[-23.6%, -0.0%]123
All ❌✅ (primary)-0.7%[-3.3%, 1.4%]133

Bootstrap: 476.631s -> 471.922s (-0.99%)
Artifact size: 391.32 MiB -> 388.56 MiB (-0.70%)

@rustbotrustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 10, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

Ooh that's pretty good :D

@m-ou-se

m-ou-se commented Nov 10, 2025

Copy link
Copy Markdown
MemberAuthor

Pretty much everything looks like a great improvement. Not only number of instructions executed, but also memory usage and binary size. 🎉

Only two significant negative results:

1. "image-0.25.6 opt incr-patched:println" with almost +6% instructions:u.

Looking at the detailed results, it looks like that's all LLVM. Probably because llvm got more optimization opportunities. That's not necessarily a bad thing.

2. The fmt-write-str runtime benchmark with over +12% instructions:u.

This could be concerning, but I can't seem to fully replicate it locally.

If I recompile and run this benchmark 100 times in both nightly and with this PR, I do get this interesting result though:

Violin plot

With the nightly compiler, the results vary, with many measurements clustered close to 25ms but also many around 40ms. With this PR, the results are very consistent, all clustered around 27ms. (Update: It's around 26ms now, after a minor optimization.)

So, the median result is worse, but the average is better.

My guess is that the indirection (a slice of string slices) can make things unpredictable, as the strings aren't always in the optimal place for caching. The lack of indirection in the new version then makes it much more predictable. This is just a guess though.

@rustbotrustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Nov 11, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-borsBot added a commit that referenced this pull request Nov 11, 2025
 Experiment: New fmt::Arguments implementation (another one)
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 11, 2025
@m-ou-sem-ou-se changed the title Experiment: New fmt::Arguments implementation (another one)New format_args!() and fmt::Arguments implementationNov 11, 2025
@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (503dce3): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.7%[0.1%, 5.7%]17
Regressions ❌
(secondary)
0.6%[0.1%, 1.1%]40
Improvements ✅
(primary)
-0.7%[-4.4%, -0.1%]120
Improvements ✅
(secondary)
-1.6%[-38.5%, -0.0%]106
All ❌✅ (primary)-0.5%[-4.4%, 5.7%]137

Max RSS (memory usage)

Results (primary -1.5%, secondary -1.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
3.8%[1.4%, 7.0%]3
Regressions ❌
(secondary)
3.8%[1.2%, 5.8%]8
Improvements ✅
(primary)
-2.2%[-6.0%, -0.6%]23
Improvements ✅
(secondary)
-3.1%[-7.3%, -0.6%]29
All ❌✅ (primary)-1.5%[-6.0%, 7.0%]26

Cycles

Results (primary -2.3%, secondary 0.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
5.2%[3.4%, 6.9%]2
Regressions ❌
(secondary)
7.6%[1.6%, 18.5%]19
Improvements ✅
(primary)
-4.4%[-7.9%, -1.5%]7
Improvements ✅
(secondary)
-11.0%[-40.0%, -1.8%]12
All ❌✅ (primary)-2.3%[-7.9%, 6.9%]9

Binary size

Results (primary -0.8%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.5%[0.0%, 1.4%]4
Regressions ❌
(secondary)
3.0%[0.0%, 6.9%]12
Improvements ✅
(primary)
-0.8%[-3.3%, -0.0%]129
Improvements ✅
(secondary)
-1.7%[-23.6%, -0.0%]123
All ❌✅ (primary)-0.8%[-3.3%, 1.4%]133

Bootstrap: 476.356s -> 473.309s (-0.64%)
Artifact size: 391.04 MiB -> 388.34 MiB (-0.69%)

@m-ou-se
m-ou-se deleted the new-fmt-args-alt branch November 13, 2025 09:57
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 13, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
rust-timer added a commit that referenced this pull request Nov 14, 2025
Rollup merge of #148906 - m-ou-se:fmt-args-from-str, r=dtolnay
Expose fmt::Arguments::from_str as unstable.
Now that #148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: #148905
SebastianSpeitel

This comment was marked as off-topic.

@m-ou-se

This comment was marked as off-topic.

tautschnig added a commit to tautschnig/kani that referenced this pull request Nov 17, 2025
Relevant upstream PR:
- rust-lang/rust#148789 (New format_args!() and
fmt::Arguments implementation)
Resolves: model-checking#4474
github-merge-queueBot pushed a commit to model-checking/kani that referenced this pull request Nov 17, 2025
Relevant upstream PR:
- rust-lang/rust#148789 (New format_args!() and
fmt::Arguments implementation)
Resolves: #4474
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
@panstromek

Copy link
Copy Markdown
Contributor

Perf triage:

Improvements outweigh regressions.

@rustbot label: +perf-regression-triaged

@rustbotrustbot added the perf-regression-triaged The performance regression has been triaged. label Nov 19, 2025
github-actionsBot pushed a commit to model-checking/verify-rust-std that referenced this pull request Nov 30, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
oxalica added a commit to oxalica/palc that referenced this pull request Mar 19, 2026
It is expected to have a big change since Rust 1.93 includes a significant optimization for `format_args`.
See: <rust-lang/rust#148789>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-fmtArea: `core::fmt`A-run-makeArea: port run-make Makefiles to rmake.rsmerged-by-borsThis PR was explicitly merged by bors.perf-regressionPerformance regression.perf-regression-triagedThe performance regression has been triaged.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-clippyRelevant to the Clippy team.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

14 participants

@m-ou-se@rust-timer@rust-log-analyzer@nyurik@bors@rustbot@Zalathar@jdonszelmann@panstromek@jhpratt@SUPERCILEX@SebastianSpeitel@yhx-12243@WaffleLapkin
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

New format_args!() and fmt::Arguments implementation - #148789

Merged
bors merged 18 commits into
rust-lang:mainfrom
m-ou-se:new-fmt-args-alt
Nov 13, 2025
Merged

New format_args!() and fmt::Arguments implementation#148789
bors merged 18 commits into
rust-lang:mainfrom
m-ou-se:new-fmt-args-alt

Conversation

@m-ou-se

@m-ou-sem-ou-se commented Nov 10, 2025

Copy link
Copy Markdown
Member

Part of #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 strwithout 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:

fnmain(){println!("Hello, world!");}

before:

main:subrsp,56learax,[rip+ .Lanon_hello_world]mov qword ptr [rsp+8],raxmov qword ptr [rsp+16],1mov qword ptr [rsp+24],8xorpsxmm0,xmm0movups xmmword ptr [rsp+32],xmm0leardi,[rsp+8]call qword ptr [rip+std::io::stdio::_print]addrsp,56ret

after:

main:learsi,[rip+ .Lanon_hello_world]movedi,29jmp 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:

format_args!("Hello, {name:-^20}!")

lowering before:

fmt::Arguments::new_v1_formatted(&["Hello, ","!\n"],&args,&[Placeholder{position:0usize,flags:3355443245u32,precision: format_count::Implied,width: format_count::Is(20u16),},],)

lowering after:

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.


Diagram of the data structure after this change:

A diagram showing the fmt::Arguments internal structure after the change. Most notably, it is only two pointers in size, and all the string data is all part of a single string, removing a level of indirection.
Original data structureA diagram showing the fmt::Arguments internal structure before the change. Most notably, it consists of three slices (so six pointers in size), and one of the slices contains string slices (so another two pointers in size for each string part, and more indirection).

@m-ou-sem-ou-se self-assigned this Nov 10, 2025
@m-ou-sem-ou-se added the A-fmt Area: `core::fmt` label Nov 10, 2025
@rustbotrustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 10, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-borsBot added a commit that referenced this pull request Nov 10, 2025
 Experiment: New fmt::Arguments implementation (another one)
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 10, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-borsBot commented Nov 10, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 6e6ba94 (6e6ba949d24fbfbd9cd48ca4c98adf59fbd04482, parent: a7b3715826827677ca8769eb88dc8052f43e734b)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (6e6ba94): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.7%[0.1%, 5.8%]26
Regressions ❌
(secondary)
0.6%[0.1%, 1.3%]44
Improvements ✅
(primary)
-0.7%[-4.3%, -0.1%]109
Improvements ✅
(secondary)
-1.7%[-38.2%, -0.0%]93
All ❌✅ (primary)-0.5%[-4.3%, 5.8%]135

Max RSS (memory usage)

Results (primary -1.5%, secondary -0.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
2.2%[2.2%, 2.2%]1
Regressions ❌
(secondary)
3.7%[1.0%, 6.7%]12
Improvements ✅
(primary)
-1.6%[-6.0%, -0.5%]31
Improvements ✅
(secondary)
-2.6%[-7.9%, -0.7%]25
All ❌✅ (primary)-1.5%[-6.0%, 2.2%]32

Cycles

Results (primary -0.5%, secondary -4.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
4.8%[3.4%, 6.2%]2
Regressions ❌
(secondary)
8.8%[2.6%, 18.8%]6
Improvements ✅
(primary)
-3.1%[-5.0%, -2.1%]4
Improvements ✅
(secondary)
-10.3%[-39.4%, -2.1%]13
All ❌✅ (primary)-0.5%[-5.0%, 6.2%]6

Binary size

Results (primary -0.7%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.5%[0.0%, 1.4%]4
Regressions ❌
(secondary)
3.2%[0.0%, 7.5%]12
Improvements ✅
(primary)
-0.8%[-3.3%, -0.0%]129
Improvements ✅
(secondary)
-1.7%[-23.6%, -0.0%]123
All ❌✅ (primary)-0.7%[-3.3%, 1.4%]133

Bootstrap: 476.631s -> 471.922s (-0.99%)
Artifact size: 391.32 MiB -> 388.56 MiB (-0.70%)

@rustbotrustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 10, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

Ooh that's pretty good :D

@m-ou-se

m-ou-se commented Nov 10, 2025

Copy link
Copy Markdown
MemberAuthor

Pretty much everything looks like a great improvement. Not only number of instructions executed, but also memory usage and binary size. 🎉

Only two significant negative results:

1. "image-0.25.6 opt incr-patched:println" with almost +6% instructions:u.

Looking at the detailed results, it looks like that's all LLVM. Probably because llvm got more optimization opportunities. That's not necessarily a bad thing.

2. The fmt-write-str runtime benchmark with over +12% instructions:u.

This could be concerning, but I can't seem to fully replicate it locally.

If I recompile and run this benchmark 100 times in both nightly and with this PR, I do get this interesting result though:

Violin plot

With the nightly compiler, the results vary, with many measurements clustered close to 25ms but also many around 40ms. With this PR, the results are very consistent, all clustered around 27ms. (Update: It's around 26ms now, after a minor optimization.)

So, the median result is worse, but the average is better.

My guess is that the indirection (a slice of string slices) can make things unpredictable, as the strings aren't always in the optimal place for caching. The lack of indirection in the new version then makes it much more predictable. This is just a guess though.

@rustbotrustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Nov 11, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-borsBot added a commit that referenced this pull request Nov 11, 2025
 Experiment: New fmt::Arguments implementation (another one)
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 11, 2025
@m-ou-sem-ou-se changed the title Experiment: New fmt::Arguments implementation (another one)New format_args!() and fmt::Arguments implementationNov 11, 2025
@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (503dce3): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.7%[0.1%, 5.7%]17
Regressions ❌
(secondary)
0.6%[0.1%, 1.1%]40
Improvements ✅
(primary)
-0.7%[-4.4%, -0.1%]120
Improvements ✅
(secondary)
-1.6%[-38.5%, -0.0%]106
All ❌✅ (primary)-0.5%[-4.4%, 5.7%]137

Max RSS (memory usage)

Results (primary -1.5%, secondary -1.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
3.8%[1.4%, 7.0%]3
Regressions ❌
(secondary)
3.8%[1.2%, 5.8%]8
Improvements ✅
(primary)
-2.2%[-6.0%, -0.6%]23
Improvements ✅
(secondary)
-3.1%[-7.3%, -0.6%]29
All ❌✅ (primary)-1.5%[-6.0%, 7.0%]26

Cycles

Results (primary -2.3%, secondary 0.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
5.2%[3.4%, 6.9%]2
Regressions ❌
(secondary)
7.6%[1.6%, 18.5%]19
Improvements ✅
(primary)
-4.4%[-7.9%, -1.5%]7
Improvements ✅
(secondary)
-11.0%[-40.0%, -1.8%]12
All ❌✅ (primary)-2.3%[-7.9%, 6.9%]9

Binary size

Results (primary -0.8%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.5%[0.0%, 1.4%]4
Regressions ❌
(secondary)
3.0%[0.0%, 6.9%]12
Improvements ✅
(primary)
-0.8%[-3.3%, -0.0%]129
Improvements ✅
(secondary)
-1.7%[-23.6%, -0.0%]123
All ❌✅ (primary)-0.8%[-3.3%, 1.4%]133

Bootstrap: 476.356s -> 473.309s (-0.64%)
Artifact size: 391.04 MiB -> 388.34 MiB (-0.69%)

@m-ou-se
m-ou-se deleted the new-fmt-args-alt branch November 13, 2025 09:57
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 13, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
rust-timer added a commit that referenced this pull request Nov 14, 2025
Rollup merge of #148906 - m-ou-se:fmt-args-from-str, r=dtolnay
Expose fmt::Arguments::from_str as unstable.
Now that #148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: #148905
SebastianSpeitel

This comment was marked as off-topic.

@m-ou-se

This comment was marked as off-topic.

tautschnig added a commit to tautschnig/kani that referenced this pull request Nov 17, 2025
Relevant upstream PR:
- rust-lang/rust#148789 (New format_args!() and
fmt::Arguments implementation)
Resolves: model-checking#4474
github-merge-queueBot pushed a commit to model-checking/kani that referenced this pull request Nov 17, 2025
Relevant upstream PR:
- rust-lang/rust#148789 (New format_args!() and
fmt::Arguments implementation)
Resolves: #4474
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
@panstromek

Copy link
Copy Markdown
Contributor

Perf triage:

Improvements outweigh regressions.

@rustbot label: +perf-regression-triaged

@rustbotrustbot added the perf-regression-triaged The performance regression has been triaged. label Nov 19, 2025
github-actionsBot pushed a commit to model-checking/verify-rust-std that referenced this pull request Nov 30, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
oxalica added a commit to oxalica/palc that referenced this pull request Mar 19, 2026
It is expected to have a big change since Rust 1.93 includes a significant optimization for `format_args`.
See: <rust-lang/rust#148789>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-fmtArea: `core::fmt`A-run-makeArea: port run-make Makefiles to rmake.rsmerged-by-borsThis PR was explicitly merged by bors.perf-regressionPerformance regression.perf-regression-triagedThe performance regression has been triaged.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-clippyRelevant to the Clippy team.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

14 participants

@m-ou-se@rust-timer@rust-log-analyzer@nyurik@bors@rustbot@Zalathar@jdonszelmann@panstromek@jhpratt@SUPERCILEX@SebastianSpeitel@yhx-12243@WaffleLapkin
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

New format_args!() and fmt::Arguments implementation - #148789

Merged
bors merged 18 commits into
rust-lang:mainfrom
m-ou-se:new-fmt-args-alt
Nov 13, 2025
Merged

New format_args!() and fmt::Arguments implementation#148789
bors merged 18 commits into
rust-lang:mainfrom
m-ou-se:new-fmt-args-alt

Conversation

@m-ou-se

@m-ou-sem-ou-se commented Nov 10, 2025

Copy link
Copy Markdown
Member

Part of #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 strwithout 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:

fnmain(){println!("Hello, world!");}

before:

main:subrsp,56learax,[rip+ .Lanon_hello_world]mov qword ptr [rsp+8],raxmov qword ptr [rsp+16],1mov qword ptr [rsp+24],8xorpsxmm0,xmm0movups xmmword ptr [rsp+32],xmm0leardi,[rsp+8]call qword ptr [rip+std::io::stdio::_print]addrsp,56ret

after:

main:learsi,[rip+ .Lanon_hello_world]movedi,29jmp 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:

format_args!("Hello, {name:-^20}!")

lowering before:

fmt::Arguments::new_v1_formatted(&["Hello, ","!\n"],&args,&[Placeholder{position:0usize,flags:3355443245u32,precision: format_count::Implied,width: format_count::Is(20u16),},],)

lowering after:

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.


Diagram of the data structure after this change:

A diagram showing the fmt::Arguments internal structure after the change. Most notably, it is only two pointers in size, and all the string data is all part of a single string, removing a level of indirection.
Original data structureA diagram showing the fmt::Arguments internal structure before the change. Most notably, it consists of three slices (so six pointers in size), and one of the slices contains string slices (so another two pointers in size for each string part, and more indirection).

@m-ou-sem-ou-se self-assigned this Nov 10, 2025
@m-ou-sem-ou-se added the A-fmt Area: `core::fmt` label Nov 10, 2025
@rustbotrustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 10, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-borsBot added a commit that referenced this pull request Nov 10, 2025
 Experiment: New fmt::Arguments implementation (another one)
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 10, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-borsBot commented Nov 10, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 6e6ba94 (6e6ba949d24fbfbd9cd48ca4c98adf59fbd04482, parent: a7b3715826827677ca8769eb88dc8052f43e734b)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (6e6ba94): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.7%[0.1%, 5.8%]26
Regressions ❌
(secondary)
0.6%[0.1%, 1.3%]44
Improvements ✅
(primary)
-0.7%[-4.3%, -0.1%]109
Improvements ✅
(secondary)
-1.7%[-38.2%, -0.0%]93
All ❌✅ (primary)-0.5%[-4.3%, 5.8%]135

Max RSS (memory usage)

Results (primary -1.5%, secondary -0.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
2.2%[2.2%, 2.2%]1
Regressions ❌
(secondary)
3.7%[1.0%, 6.7%]12
Improvements ✅
(primary)
-1.6%[-6.0%, -0.5%]31
Improvements ✅
(secondary)
-2.6%[-7.9%, -0.7%]25
All ❌✅ (primary)-1.5%[-6.0%, 2.2%]32

Cycles

Results (primary -0.5%, secondary -4.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
4.8%[3.4%, 6.2%]2
Regressions ❌
(secondary)
8.8%[2.6%, 18.8%]6
Improvements ✅
(primary)
-3.1%[-5.0%, -2.1%]4
Improvements ✅
(secondary)
-10.3%[-39.4%, -2.1%]13
All ❌✅ (primary)-0.5%[-5.0%, 6.2%]6

Binary size

Results (primary -0.7%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.5%[0.0%, 1.4%]4
Regressions ❌
(secondary)
3.2%[0.0%, 7.5%]12
Improvements ✅
(primary)
-0.8%[-3.3%, -0.0%]129
Improvements ✅
(secondary)
-1.7%[-23.6%, -0.0%]123
All ❌✅ (primary)-0.7%[-3.3%, 1.4%]133

Bootstrap: 476.631s -> 471.922s (-0.99%)
Artifact size: 391.32 MiB -> 388.56 MiB (-0.70%)

@rustbotrustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 10, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

Ooh that's pretty good :D

@m-ou-se

m-ou-se commented Nov 10, 2025

Copy link
Copy Markdown
MemberAuthor

Pretty much everything looks like a great improvement. Not only number of instructions executed, but also memory usage and binary size. 🎉

Only two significant negative results:

1. "image-0.25.6 opt incr-patched:println" with almost +6% instructions:u.

Looking at the detailed results, it looks like that's all LLVM. Probably because llvm got more optimization opportunities. That's not necessarily a bad thing.

2. The fmt-write-str runtime benchmark with over +12% instructions:u.

This could be concerning, but I can't seem to fully replicate it locally.

If I recompile and run this benchmark 100 times in both nightly and with this PR, I do get this interesting result though:

Violin plot

With the nightly compiler, the results vary, with many measurements clustered close to 25ms but also many around 40ms. With this PR, the results are very consistent, all clustered around 27ms. (Update: It's around 26ms now, after a minor optimization.)

So, the median result is worse, but the average is better.

My guess is that the indirection (a slice of string slices) can make things unpredictable, as the strings aren't always in the optimal place for caching. The lack of indirection in the new version then makes it much more predictable. This is just a guess though.

@rustbotrustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Nov 11, 2025
@m-ou-se

Copy link
Copy Markdown
MemberAuthor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-borsBot added a commit that referenced this pull request Nov 11, 2025
 Experiment: New fmt::Arguments implementation (another one)
@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 11, 2025
@m-ou-sem-ou-se changed the title Experiment: New fmt::Arguments implementation (another one)New format_args!() and fmt::Arguments implementationNov 11, 2025
@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (503dce3): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.7%[0.1%, 5.7%]17
Regressions ❌
(secondary)
0.6%[0.1%, 1.1%]40
Improvements ✅
(primary)
-0.7%[-4.4%, -0.1%]120
Improvements ✅
(secondary)
-1.6%[-38.5%, -0.0%]106
All ❌✅ (primary)-0.5%[-4.4%, 5.7%]137

Max RSS (memory usage)

Results (primary -1.5%, secondary -1.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
3.8%[1.4%, 7.0%]3
Regressions ❌
(secondary)
3.8%[1.2%, 5.8%]8
Improvements ✅
(primary)
-2.2%[-6.0%, -0.6%]23
Improvements ✅
(secondary)
-3.1%[-7.3%, -0.6%]29
All ❌✅ (primary)-1.5%[-6.0%, 7.0%]26

Cycles

Results (primary -2.3%, secondary 0.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
5.2%[3.4%, 6.9%]2
Regressions ❌
(secondary)
7.6%[1.6%, 18.5%]19
Improvements ✅
(primary)
-4.4%[-7.9%, -1.5%]7
Improvements ✅
(secondary)
-11.0%[-40.0%, -1.8%]12
All ❌✅ (primary)-2.3%[-7.9%, 6.9%]9

Binary size

Results (primary -0.8%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.5%[0.0%, 1.4%]4
Regressions ❌
(secondary)
3.0%[0.0%, 6.9%]12
Improvements ✅
(primary)
-0.8%[-3.3%, -0.0%]129
Improvements ✅
(secondary)
-1.7%[-23.6%, -0.0%]123
All ❌✅ (primary)-0.8%[-3.3%, 1.4%]133

Bootstrap: 476.356s -> 473.309s (-0.64%)
Artifact size: 391.04 MiB -> 388.34 MiB (-0.69%)

@m-ou-se
m-ou-se deleted the new-fmt-args-alt branch November 13, 2025 09:57
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 13, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
rust-timer added a commit that referenced this pull request Nov 14, 2025
Rollup merge of #148906 - m-ou-se:fmt-args-from-str, r=dtolnay
Expose fmt::Arguments::from_str as unstable.
Now that #148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: #148905
SebastianSpeitel

This comment was marked as off-topic.

@m-ou-se

This comment was marked as off-topic.

tautschnig added a commit to tautschnig/kani that referenced this pull request Nov 17, 2025
Relevant upstream PR:
- rust-lang/rust#148789 (New format_args!() and
fmt::Arguments implementation)
Resolves: model-checking#4474
github-merge-queueBot pushed a commit to model-checking/kani that referenced this pull request Nov 17, 2025
Relevant upstream PR:
- rust-lang/rust#148789 (New format_args!() and
fmt::Arguments implementation)
Resolves: #4474
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
@panstromek

Copy link
Copy Markdown
Contributor

Perf triage:

Improvements outweigh regressions.

@rustbot label: +perf-regression-triaged

@rustbotrustbot added the perf-regression-triaged The performance regression has been triaged. label Nov 19, 2025
github-actionsBot pushed a commit to model-checking/verify-rust-std that referenced this pull request Nov 30, 2025
Expose fmt::Arguments::from_str as unstable.
Now that rust-lang#148789 is merged, we can have a fmt::Arguments::from_str. I don't know if we want to commit to always having an implementation that allows for this, but we can expose it as unstable for now so we can play with it.
Tracking issue: rust-lang#148905
oxalica added a commit to oxalica/palc that referenced this pull request Mar 19, 2026
It is expected to have a big change since Rust 1.93 includes a significant optimization for `format_args`.
See: <rust-lang/rust#148789>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-fmtArea: `core::fmt`A-run-makeArea: port run-make Makefiles to rmake.rsmerged-by-borsThis PR was explicitly merged by bors.perf-regressionPerformance regression.perf-regression-triagedThe performance regression has been triaged.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-clippyRelevant to the Clippy team.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

14 participants

@m-ou-se@rust-timer@rust-log-analyzer@nyurik@bors@rustbot@Zalathar@jdonszelmann@panstromek@jhpratt@SUPERCILEX@SebastianSpeitel@yhx-12243@WaffleLapkin