Enable null pointer discriminant optimisation for XDRFILE#12
Open
Yoshanuikabundi wants to merge 1 commit into
Open
Enable null pointer discriminant optimisation for XDRFILE#12Yoshanuikabundi wants to merge 1 commit into
Yoshanuikabundi wants to merge 1 commit into
Conversation
Contributor
Author
|
I did a lot of reading for this one :P |
Owner
|
Hi, |
Contributor
Author
|
Getting the raw pointer is a no-op, NonNull is just a raw pointer that makes some promises to the compiler. I'd argue that it's not very much complexity in the actual code, and it also helps keep the code self-documenting to have the promise there that the pointer is non-null. But I also understand that there isn't really a problem there. Up to you :) Was worth implementing for me just for the knowledges :) |
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
XDRFile.xdrfilefield is now aNonNull<XDRFILE>rather than a*mut XDRFILE, and aPhantomData<XDRFILE>field was added. NonNull allows a null value to be used as the discriminant inOption<XDRFile>, and the PhantomData makes sure that drop checks, send + sync derivation, anything Rust adds in the future etc treat XDRFile as owning an XDRFILE.For non-generic types like both
XDRFILEandXDRFile,NonNull<T>is just a*mut Twith the extra guarantee that it will never be null. This is constructed safely by theNonNull::new()method, which performs the check and returns anOption<NonNull<T>>. Generic types with lifetimes have some extra implications about covariance but they're irrelevant here.The Rustonomicon (and Vec and Box in the standard library) uses a type called
Unique<T>, which is a hidden type with the following definition:So the effect of that type is replicated here with the
PhantomData. A struct with aPhantomData<T>acts as though it has a field of type<T>for the purposes of drop checking, auto-deriving the Send and Sync traits, and so on. Raw pointers and NonNull do not have this property. I don't think we technically need the PhantomData because XDRFILE doesn't have a drop implementation, but it doesn't do any harm and it future proofs the implementation against changes in type checking and so on. Since the pointer is only constructed by the open method and is private, its guaranteed to be unique which satisfies the requirements for the Unique pointer type anyway.What's the benefit? With
*mut T,Option<T>and other enums of XDRFile require extra memory to store which variant an instance is. With NonNull, the null value is used as the discriminant and Option should be a byte smaller. Although it may not be if the compiler can find somewhere else to put the discriminant.