Uh oh!
There was an error while loading. Please reload this page.
Add Option::as_(mut_)slice - #105871
Conversation
rustbot
commented
Dec 18, 2022
r? @m-ou-se (rustbot has picked a reviewer for you, use r? to override) |
rustbot
commented
Dec 18, 2022
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
Uh oh!
There was an error while loading. Please reload this page.
37b5b52 to
4a52066CompareUh oh!
There was an error while loading. Please reload this page.
vacuus
commented
Dec 18, 2022
Perhaps use the |
llogiq
commented
Dec 18, 2022
I tried that, but it didn't work; the compiler complained that |
99cbfee to
9c6a7d5CompareGiven the problem of inference errors, I wonder if we should rename the mutable version of |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This comment has been minimized.
This comment has been minimized.
llogiq
commented
Dec 19, 2022
I had to add |
vacuus
commented
Dec 19, 2022
You could also use |
llogiq
commented
Dec 19, 2022
Thank you, I had missed that one. |
Option::as_slice(_mut) and ::into_sliceOption::as_slice(_mut)Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
scottmcm
left a comment
There was a problem hiding this comment.
I'd really rather not take a linkchecker change here (unless there's a bug it can link to saying this is the best option for now), but other than that it looks good!
This comment has been minimized.
This comment has been minimized.
llogiq
commented
Feb 28, 2023
@scottmcm I have reverted the linkchecker change and changed the offending links as per your suggestion, let's hope they work this time. |
scottmcm
commented
Feb 28, 2023
Thanks! I don't really know the exact intradoc link rules nor the markdown parsing ones, so I'm also just 🤞 |
This adds the following functions: * `Option<T>::as_slice(&self) -> &[T]` * `Option<T>::as_slice_mut(&mut self) -> &[T]` The `as_slice` and `as_slice_mut` functions benefit from an optimization that makes them completely branch-free. Note that the optimization's soundness hinges on the fact that either the niche optimization makes the offset of the `Some(_)` contents zero or the mempory layout of `Option<T>` is equal to that of `Option<MaybeUninit<T>>`.
scottmcm
commented
Mar 1, 2023
Ah, glad that one worked! @bors r+ |
bors
commented
Mar 1, 2023
Add `Option::as_`(`mut_`)`slice` This adds the following functions: * `Option<T>::as_slice(&self) -> &[T]` * `Option<T>::as_mut_slice(&mut self) -> &[T]` The `as_slice` and `as_mut_slice_mut` functions benefit from an optimization that makes them completely branch-free. ~~Unfortunately, this optimization is not available on by-value Options, therefore the `into_slice` implementations use the plain `match` + `slice::from_ref` approach.~~ Note that the optimization's soundness hinges on the fact that either the niche optimization makes the offset of the `Some(_)` contents zero or the mempory layout of `Option<T>` is equal to that of `Option<MaybeUninit<T>>`. The idea has been discussed on [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Option.3A.3Aas_slice). Notably the idea for the `as_slice_mut` and `into_slice´ methods came from `@cuviper` and `@Sp00ph` hardened the optimization against niche-optimized Options. The [rust playground](https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=74f8e4239a19f454c183aaf7b4a969e0) shows that the generated assembly of the optimized method is basically only a copy while the naive method generates code containing a `test dx, dx` on x86_64. --- EDIT from reviewer: ACP is rust-lang/libs-team#150
Add `Option::as_`(`mut_`)`slice` This adds the following functions: * `Option<T>::as_slice(&self) -> &[T]` * `Option<T>::as_mut_slice(&mut self) -> &[T]` The `as_slice` and `as_mut_slice_mut` functions benefit from an optimization that makes them completely branch-free. ~~Unfortunately, this optimization is not available on by-value Options, therefore the `into_slice` implementations use the plain `match` + `slice::from_ref` approach.~~ Note that the optimization's soundness hinges on the fact that either the niche optimization makes the offset of the `Some(_)` contents zero or the mempory layout of `Option<T>` is equal to that of `Option<MaybeUninit<T>>`. The idea has been discussed on [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Option.3A.3Aas_slice). Notably the idea for the `as_slice_mut` and `into_slice´ methods came from ``@cuviper`` and ``@Sp00ph`` hardened the optimization against niche-optimized Options. The [rust playground](https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=74f8e4239a19f454c183aaf7b4a969e0) shows that the generated assembly of the optimized method is basically only a copy while the naive method generates code containing a `test dx, dx` on x86_64. --- EDIT from reviewer: ACP is rust-lang/libs-team#150
bors
commented
Mar 1, 2023
bors
commented
Mar 1, 2023
☀️ Test successful - checks-actions |
rust-timer
commented
Mar 1, 2023
Finished benchmarking commit (5423745): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. |
…trieb Use `Option::as_slice` where applicable After rust-lang#105871 introduced `Option::as_slice`, this PR uses it within the compiler. I found it interesting that all cases where `as_slice` could be used were done with different code before; so it seems the new API also has the benefit of being "the obvious solution" where before there was a mix of options, none clearly better than the rest.
…trieb Use `Option::as_slice` where applicable After rust-lang#105871 introduced `Option::as_slice`, this PR uses it within the compiler. I found it interesting that all cases where `as_slice` could be used were done with different code before; so it seems the new API also has the benefit of being "the obvious solution" where before there was a mix of options, none clearly better than the rest.
This adds the following functions:
Option<T>::as_slice(&self) -> &[T]Option<T>::as_mut_slice(&mut self) -> &[T]The
as_sliceandas_mut_slice_mutfunctions benefit from an optimization that makes them completely branch-free.Unfortunately, this optimization is not available on by-value Options, therefore theinto_sliceimplementations use the plainmatch+slice::from_refapproach.Note that the optimization's soundness hinges on the fact that either the niche optimization makes the offset of the
Some(_)contents zero or the mempory layout ofOption<T>is equal to that ofOption<MaybeUninit<T>>.The idea has been discussed on Zulip. Notably the idea for the
as_slice_mutand `into_slice´ methods came from @cuviper and @Sp00ph hardened the optimization against niche-optimized Options.The rust playground shows that the generated assembly of the optimized method is basically only a copy while the naive method generates code containing a
test dx, dxon x86_64.EDIT from reviewer: ACP is rust-lang/libs-team#150