Pass vtable data by value#826
Merged
Merged
Conversation
The `drop`, `into_vec`, and `into_mut` vtable slots now take the loaded `*mut ()` (via `with_mut`) instead of `&[mut] AtomicPtr<()>`. This breaks the borrow-back into `Bytes`, letting LLVM infer `captures(none)` on the indirect call and elide the temporary `memcpy` when `Bytes` is passed by value. `clone` and `is_unique` keep `&AtomicPtr<()>` (called via `&self`; `promotable_*_clone` also needs the address for `compare_exchange`). Minimal repro (https://godbolt.org/z/jz1437Enz): ```rust use bytes::Bytes; pub struct MyStuff { pub _stuff: [u8; 200], pub bytes: Bytes, } #[unsafe(no_mangle)] #[inline(never)] pub fn lookup(blocking: bool, val: MyStuff) -> u64 { if blocking { return lookup_blocking(val); } 1 } #[cold] #[inline(never)] pub fn lookup_blocking(_val: MyStuff) -> u64 { 0 } ```
Member
|
I'm pretty sure we sometimes perform a compare-and-swap on this pointers, which we can't do if you pass it by value. |
Contributor
Author
|
Yes, but only for clone, which is described in the description. We're talking about swapping the AtomicPtr that's inside of Bytes, which is not ever shared when Bytes itself isn't (&mut or owned). This only happens in Clone when turning into a shared vtable. |
Contributor
Author
|
Hey @Darksonn, any chance this could be released? It's a pretty minor change but it can cut down stack usage quite a bit in functions when using Bytes or structures containing it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
drop,into_vec, andinto_mutvtable slots now take the loaded*mut ()(viawith_mut) instead of&[mut] AtomicPtr<()>. This breaks the borrow-back intoBytes, letting LLVM infercaptures(none)on the indirect call and elide the temporarymemcpywhenBytesis passed by value.cloneandis_uniquekeep&AtomicPtr<()>(called via&self;promotable_*_clonealso needs the address forcompare_exchange).Minimal repro (https://godbolt.org/z/jz1437Enz):
Details
Before:
After: