-
Notifications
You must be signed in to change notification settings - Fork 55
Add a note about uninhabited-struct layout optimization #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
scottmcm
wants to merge
5
commits into
rust-lang:main
Choose a base branch
from
scottmcm:scottmcm-patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
13a9e79
Add a note about uninhabited-struct layout optimization
scottmcm c01fff9
Update src/frequently-requested-changes.md
scottmcm 0c9d7a8
Remove the `union` mention
scottmcm 2dbc56c
Fix incorrect negation and simplify wording around enum variants
joshtriplett eaba985
Expand acronym "ZSTs" in heading
joshtriplett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the name “ZST” does not capture the underlying idea very well. The real thing we want is a -∞ sized type, ZST is just one specific way of implementing it. Although I don’t have a better title yet.
Also, do you think it’s a good idea to also add the discussion of
Inhabittedtrait here? In case someone reading this come up with that idea again.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not as clear as you'd think. Rust has unsafe, and types need a layout, so "a ZST with always-false validity invariant" actually has some important benefits. With a -∞ sized/aligned type you can't even start executing a function that has a
!variable in it because its whole stack frame would be uninhabited; yet it's easy to write such a function: justpanic!().Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Size is a natural number, so a size of -∞ doesn't even make sense. (One could define notions of size where that does make sense, but that's not the discussion we are having here. The notion of size here is the notion currently used in Rust. Given the proposed resolution for this question, it's also not useful to consider these other notions of size.)
Therefore, ZST is exactly the right term here. There's no way a type can be smaller than that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently size is (at the very least) the amount of bytes required to uniquely represent an arbitrary member of the set of a type, in the Information Theory sense.
The minimum value possible there is zero: if person A needs to describe an element x of type T to person B, and in order to do so, no bytes need to be sent to B, that's because B already knows all it needs to know about x.
And that can only happen when T is either a singleton set or the empty set.
I'm guessing that the -oo notion comes from the logarithm in the Information Theory definition. If we take the logarithm of the size of the set described by the type T, we get:
|T| = 1|T| = 0But another restriction we impose over sizes is that they be positive. This (I believe) is because we need sizes to describe a layout budget.
Thus, on one hand the logarithm tells us the number of bits is at least -oo, but on the other hand we know it to also be at least 0. So it ends up being 0.
A ZST :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those two concerns get put together by the use of the ceiling operator; the logarithm of an integer is not necessarily an integer, even when it's not -∞, so we actually do ⌈log₂|T|⌉ where we choose to define ⌈-∞⌉=0.