-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Optimize Rc::<str>::default() implementation
#136099
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
Conversation
|
r? @ibraheemdev rustbot has assigned @ibraheemdev. Use |
This PR lets `impl Default for Rc<str>` re-use the implementation for `Rc::<[u8]>::default()`. The previous version only calculted the memory layout at runtime, even though it should be known at compile time, resulting in an additional function call. The same optimization is done for `Rc<CStr>`. Generated byte code: <https://godbolt.org/z/dfq73jsoP>. Resolves <rust-lang#135784>.
72d71e3 to
e090db8
Compare
|
Approving this without a perf run because the codegen is clearly better (and a perf run is unlikely to provide meaningful results). Thanks! @bors r+ |
|
FWIW, there are guidelines on how to write comments regarding the use of unsafe functions: https://std-dev-guide.rust-lang.org/policy/safety-comments.html |
|
Normally I follow that guideline, but I adapted to what I found in the file I changed: No "SAFETY" keywords and short sentences (and most |
|
Definitely there there are some SAFETY keywords in this file, although as you have noticed, they are not present everywhere they should be.
There were attempts in the past, like #63793, but as you can probably tell, this is not a trivial task. I wouldn't block this PR on that, but I hope to raise awareness for the future contributions. |
Ah, I missed the 18 instances in a 4000+ lines file. But you are right, newer functions further down do follow the guideline. I'll do too on future contributions regardless if the rest of a file does not. |
This PR lets
impl Default for Rc<str>re-use the implementation forRc::<[u8]>::default(). The previous version only calculted the memory layout at runtime, even though it should be known at compile time, resulting in an additional function call.The same optimization is done for
Rc<CStr>.Generated byte code: https://godbolt.org/z/dfq73jsoP.
Resolves #135784.
Cc @Billy-Sheppard.