Skip to content

Guarantee 8 bytes of alignment of RawWakerVTable#158186

Open
orlp wants to merge 1 commit into
rust-lang:mainfrom
orlp:rawwaker-vtable-align
Open

Guarantee 8 bytes of alignment of RawWakerVTable#158186
orlp wants to merge 1 commit into
rust-lang:mainfrom
orlp:rawwaker-vtable-align

Conversation

@orlp

@orlp orlp commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

This is similar to an earlier PR I made for Thread::into_raw: #143859.

When using AtomicPtr for synchronization it's incredibly useful when you've got a couple bits you can stuff metadata in. By guaranteeing that RawWakerVTable is aligned to 8 bytes everyone can use the bottom 3 bits to signal other things, such as a critical section, etc. In particular, this can be used to portably implement an AtomicWaker which is always two pointers in size, no more.

On almost all platforms the align is already 8 bytes, and on other platforms it might cause an infinitesimal increase in size. This guarantee is thus very useful and costs us essentially nothing.


r? libs-api

Like last time since this adds a guarantee this probably needs a FCP.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 20, 2026
@BurntSushi

Copy link
Copy Markdown
Member

I like this. Having unused bits in a pointer is really useful.

In particular, this can be used to portably implement an AtomicWaker which is always two pointers in size, no more.

Would it be reasonable to add a basic example of this? It would be great for docs to not just provide the guarantee but give a real example where it's useful.

@orlp

orlp commented Jun 20, 2026

Copy link
Copy Markdown
Contributor Author

@BurntSushi I'm afraid the example would be far too long and too subtle for the docs on this.

@BurntSushi

Copy link
Copy Markdown
Member

Maybe we can shorten it so that it just hows how to (correctly) use those lower bits in the pointer to store something? It can be a pretty subtle thing to get correct in my experience. I know I've toiled over it. It would be a real gem to have something like that in the docs precisely where a guarantee affording such chicanery is written.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant 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.

3 participants