diff options
| author | Yuki Okushi <jtitor@2k36.org> | 2021-07-30 16:26:52 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-30 16:26:52 +0900 |
| commit | c25b979db6ee65923e46f8e4dcffcbd3a8381668 (patch) | |
| tree | f205aa04b82c65b1f0f92dca8850c08084b06cfd /library/core/src | |
| parent | fe1c942eee3489743d655d81ca89166217db0547 (diff) | |
| parent | 3e82dca65c226324580db910b2609e8dce1850ac (diff) | |
| download | rust-c25b979db6ee65923e46f8e4dcffcbd3a8381668.tar.gz rust-c25b979db6ee65923e46f8e4dcffcbd3a8381668.zip | |
Rollup merge of #87052 - phlopsi:patch-1, r=jyn514
Optimize fmt::PadAdapter::wrap
After adding the first `write!` usage to my project and printing the result to the console, I noticed, that my binary contains the strings "called `Option::unwrap()` on a `None` value`" and more importantly "C:\Users\Patrick Fischer\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\fmt\builders.rs", with my release build being configured as follows:
```
[profile.release]
panic = "abort"
codegen-units = 1
strip = "symbols" # the important bit
lto = true
```
I am in a no_std environment and my custom panic handler is a simple `loop {}`. I did not expect the above information to be preserved. I heavily suspect the edited function to be the culprit. It contains the only direct use of `Option::unwrap` in the entire file and I tracked the symbols in the assembly to be used from the section `_ZN68_$LT$core..fmt..builders..PadAdapter$u20$as$u20$core..fmt..Write$GT$9write_str17ha1d5e5efe167202aE`.
Aside from me suspecting this function to be the culprit, the replaced code performs the same operation as `Option::insert`, but without the `unreachable_unchecked` optimization `Option::insert` provides. Therefore, it makes sense to me to use the more optimized version, instead.
As I don't change any semantics, I hope a simple pull request suffices.
Diffstat (limited to 'library/core/src')
| -rw-r--r-- | library/core/src/fmt/builders.rs | 5 |
1 files changed, 1 insertions, 4 deletions
diff --git a/library/core/src/fmt/builders.rs b/library/core/src/fmt/builders.rs index b660788c051..8e7b03d02f1 100644 --- a/library/core/src/fmt/builders.rs +++ b/library/core/src/fmt/builders.rs @@ -23,10 +23,7 @@ impl<'buf, 'state> PadAdapter<'buf, 'state> { slot: &'slot mut Option<Self>, state: &'state mut PadAdapterState, ) -> fmt::Formatter<'slot> { - fmt.wrap_buf(move |buf| { - *slot = Some(PadAdapter { buf, state }); - slot.as_mut().unwrap() - }) + fmt.wrap_buf(move |buf| slot.insert(PadAdapter { buf, state })) } } |
