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
Option<*const T> parameters are not zero-cost. #2513
Comments
Yes, I'd considered switching to Also, thanks to LTCG I think the optimization concern goes away but I haven't tried that myself. |
It's not obvious that |
|
A null pointer also does that without introducing extra syntactic and compilation overhead. Raw pointers can't be avoided in this API anyway, this is just for the null case. If you're passing in a non null pointer, you still need a raw pointer to do that with. As mentioned, the ergonomics are about the same. |
I would definitely agree that
And the only benefit is being able to type
I would use the |
This is certainly debatable and rather subjective. One of the reasons that I went with |
I believe #1939 and this #2036 combine to create many definitions like
ID3D12Resource::Map
where the function is defined as:Where
preadrange
andppdata
are defined asOption<*const/*mut>
. This seems wrong as the size ofOption<*>
is 16 bytes. Raw pointers don't get the niche value optimization forOption
like references. The functions won't be inlined across crate boundaries without the#[inline]
attribute so there will always be 16 bytes for each option pointer sent into the function in either registers or on the stack. This will always waste stack space or register space when calling these APIs compared to a raw, nullable pointer.The
Option
wrappers are not zero-cost, and I question if they add any real value to the Win32/COM bindings. The APIs in this library can't be practically used without referencing the official documentation, so theOption
types don't really add information a user wouldn't have already known when calling the API but do add either syntax or memory overhead.I see two alternatives:
Option<NonNull<T>>
insteadRaw pointers leaves the 'optional-ness' of the parameter unmarked,
Option<NonNull<T>>
obscures theconst
ormut
qualification asNonNull
is always equivalent to*mut
.There isn't really an ideal solution here, but I think adding register or memory pressure needlessly should be avoided.
What do you think?
The text was updated successfully, but these errors were encountered: