-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Optimizations not applied when separate variables are used #45126
Comments
if you replace the a and b variables with constants you get the same samme assembly. |
similar to #36001 |
Potentially related to #41160 |
@andjo403: Good to know, but I'd still tend to consider it unnecessarily unoptimized unless there's a good reason. Supposing even if they were mut, if the final value was identical for all inputs to the function, I'd hope that the compiler would be able to automatically reduce and simplify it. For vec! I'd be a bit more on the fence since the implementation is expected to allocate on the heap, which could be seen as a side effect, though even then I probably wouldn't blame the compiler for optimizing it out if it could. For the sort of use cases where mandating that something is actually allocated (probably cases where you are doing very low level sort of work) I'd think it'd be better to "opt-in" to deoptimization with a compiler attribute. People worrying a lot about the exact state of the stack will be more likely to recognize the need for such an attribute, and will likely want more explicit control over the optimizations applied anyway. |
Closing as a duplicate of #36001 |
I noticed a case from this thread where the optimizations applied by the compiler are determined by the presence or absence of intermediate variables
Looking at the assembly in the first case, it's clearly generating the two arrays separately.
In the second case, it does not appear to generate the arrays.
This is a little bit surprising to me and seems a bit undesirable since this means that making code more readable by storing intermediate operations in variables could lead to optimizations not being applied.
The text was updated successfully, but these errors were encountered: