-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add #[inline(always)] to various instances of AsRef::as_ref and AsMut::as_mut #25158
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
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @huonw (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see CONTRIBUTING.md for more information. |
Are you sure that all of these are necessary? In general we try to not provide hints where they're not necessary. For example the implementations that have no generics (e.g. Also, can this just use |
In general I would agree that one should use #[inline] instead, however in this case I can't think of a single case when you wouldn't want those inlined. Since these methods are so tiny a function call is always going to be more expensive both time and size wise, and if not inlined you can incur a huge performance hit (as I did) since it will also prevent other optimizations from running. Not all of them may be necessary, but I don't think it's a bad idea to mark that we want those always inlined, if only to be explicit about it and consistent. |
@koute we generally prefer to rely on LLVM to make optimization deductions rather than going with what we feel is the best option, and that normally involves avoiding |
Yes, I'm perfectly aware of that. Well, okay, I can change those to Also, |
Do keep in mind that this is one of the reasons that the compiler supports LTO, to sidestep this problem altogether.
I am personally much less confident about making an assertion such as this. We've seen it unfortunately too liberally applied in the past, causing huge compile-time (and perf) hits. The rule of thumb should be "stick to
There are often legitimate technical reasons why a function should always be inlined, not necessarily for performance. For example, see this comment
Do remember though that this needs to be taken with a grain of salt. There's a lot of tradeoffs at play here and there's not really an opportunity to make a blanket statement here. For example:
Overall I would like to establish a convention for when and where to apply |
Okay, I've removed the annotations from methods with generics and removed I can certainly see where you're coming from; I'll keep it in mind with any future pull requests, I still don't agree with it though. (: |
I was profiling my code again and this time AsRef<str> for String was eating up a considerable chunk of my runtime; adding the inline annotation made the program run almost twice as fast! While I was at it I also added the annotation to other implementations of AsRef as well as AsMut.
I was profiling my code again and this time AsRef<str> for String was eating up a considerable chunk of my runtime; adding the inline annotation made the program run almost twice as fast! While I was at it I also added the annotation to other implementations of AsRef as well as AsMut.
I was profiling my code again and this time AsRef for String
was eating up a considerable chunk of my runtime; adding the inline
annotation made the program run almost twice as fast!
While I was at it I also added the annotation to other implementations
of AsRef as well as AsMut.