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
Configure Compiler to optimize out unused local variables #973
Configure Compiler to optimize out unused local variables #973
Conversation
First pass looks good, couldn't find the baseline difference warning for o.e.core.jobs anymore mentioned in eclipse-platform/eclipse.platform.releng.aggregator#1653 (comment). |
3f7046d
to
4c0ac05
Compare
I fully support that change. However, at work I got complaints when using it and finally had to revert it. There are lots of people who are used to saving files at arbitrary times instead of using automated saving on launch, and they are totally confused if their (not yet used) just declared variables are gone again. So please prepare for some complaints. :) |
Thanks for the heads-up. But isn't it the default to be asked to save dirty files before launch? Why would one not like to do that? But aside from that thinking about it again I'm not sure anymore if this preference really fixes the comparator error or if I just implicitly enforce a qualifier update for all the affected bundles? |
f7007b0
to
acb7ee2
Compare
I think the problem is more if you add a local variable and then set a breakpoint there you might be surprised if the debugger says the variable is not found ;-) |
acb7ee2
to
9ee1287
Compare
Comparing the content of the class_file generated for the But there are still a few
Mhm, yes. This is indeed sub-optimal. |
Can we overwrite it again in the build-settings? |
That feel suspicious to me ... just assuming that we don't have any unused variables around in the code it should not make a difference. |
That's right and the |
It might be that something like this:
will be transformer by the compiler to
because |
9ee1287
to
48068b2
Compare
Could be possible. But I have no clue. @iloveeclipse can you tell? Anyway with the things said, I more and more have the impression that the same value is not suitable for build-time and IDE. |
I’d rather prefer a validation that flags unused local cars with a warning but keeps them in bytecode. Different compilation artifacts in IDE and in the build ask for confusion. Not seeing some local variables when debugging code that was consumed from the TP isn’t nice either. |
I would revert original PR before starting to change compilation settings everywhere without understanding of all possible side effects. I have no clue about this particular option but I see it is "preserving" by default and I would trust original compiler authors who enabled that. Note: your change succeeds most likely not because of compiler settings effect on the bytecode but because every affected bundle is considered as changed now with preferences files changed. |
@srikanth-sankaran or @jarthana : could you please help here to understand for what |
Agree on that, but as it was discussed above that preference seem to do more than only removing unused variables since the class I verified (
As written above, I suspected the same first too, but when comparing the generated class file for |
48068b2
to
528c118
Compare
528c118
to
6539b58
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry if that was discussed already but you could also use the JDT clean-up to remove unused local variables
Most of the affected classes probably don't have unused local variables, nevertheless the byte-code seems still different with this option set on So yes of course it would be best to have no unused variables in the code eventually, but for the problem it does not make a difference. |
I don't want to use "optimize out". For sure there is a reason that the ui default is "preserve". But i suggest to apply this change to see if it solves eclipse-platform/eclipse.platform.releng.aggregator#1653 (comment) We could then later revert this PR if we get in trouble with "optimize out". what i do not understand: why that option changes the output of for example org.eclipse.core.internal.jobs.ImplicitJobs - that class contains no marker for unused variables! |
@akurtakov submit and start new i build, please? |
I don't feel comfortable pushing this one for the sake of testing if the plan is to revert it shortly after. |
I didn't said aggregator build should NOT use the settings, all what I want is that we have a clear understanding for entire aggregator build what we are doing, which settings are used and that this is applied consistently for all projects participating in aggregator build. |
Can you point to such a project?
I'll execute step 1 (revert the eclipse.platform pom to configure compiler to use project settings now to unblock the build. |
Should fix eclipse-platform/eclipse.platform.releng.aggregator#1653 . Further work on the topic could be continued as described in eclipse-platform#973 (comment)
Should fix eclipse-platform/eclipse.platform.releng.aggregator#1653 . Further work on the topic could be continued as described in #973 (comment)
I would be very curious to see what changes are caused in the class file by this - As others have surmised, this option persists variables in the local variable table There is a single place where this state is consulted to drive class file generation: The relevant block of code is:
A variable being optimized out results in one less entry in the LVT. It can also - perhaps not too obviously - result in changes to byte code emitted (@iloveeclipse, In our discussion earlier today, I said it will not change the opcodes produced - that is incorrect). The VM has special instructions for local variables in positions 0,1,2,3 (aload_{0, 1, 2, 3}) vs a generic aload for everything else. So an earlier variable being persisted or not may influence the opcode used for a later variable. (Among other such changes)
|
@srikanth-sankaran You can use https://download.eclipse.org/eclipse/downloads/drops4/I20231210-2110/buildlogs/comparatorlogs/artifactcomparisons.zip to see the actual bytecode changes. |
@srikanth-sankaran Do you happen to know what is javac's behavior by default? It is interesting to know what the more wider used javac compiler does and maybe even use same default. |
org.eclipse.jdt.annotation\pom.xml |
A simple experiment with this snippet:
shows javac emitting both |
I dislike if such optimization causes debug to fail, it may reduce .class file size, put for performance the hotspot compiler will ignore needless code anyway. |
org.eclipse.jdt.annotation\pom.xml org.eclipse.jdt.ui\pom.xml seem to be the only 2 production bundles with such setting, everything else is parent, example, test and/or duplicate.
Unfortunately, the above analysis didn't help us at all in this case. |
Thanks @akurtakov I analyzed a single case:
There is indeed an unused local in the picture which is the exception parameter In the baseline case it is being stored into the variable (which is likely persisted - difficult to tell because I don't know what tool produced this byte code view - it looks substantially different from ecj disassembler/javap output) but in the build case it is being popped because the exception parameter is eliminated |
Here is a small test case that mimics what happens in Try this once with
Code produced with
Code produced with optimize-out
See the |
This is indeed true, but this single place sets some state namely This concludes my analysis of the change in |
I misspoke - In the baseline it is being popped and the in the build it is being stored. Rest of the analysis stays |
JEP 456 - unnamed variables and patterns will avoid such issues. |
Would be nice if then unused "e" in catch would leed to Problem marker with a quick fix to "_" |
Thank you @srikanth-sankaran for the analysis and explanation. This totally makes sense, I just didn't consider that unused catched exception variables are indeed unused variables.
Agree. Now that #975 has reverted the change and that it is not really desired to optimize out unused variables in the IDE, I think this change is obsolete and can be closed. |
This was really interesting and informative. Thanks everyone! |
eclipse-platform/eclipse.platform#973 identified that IDE builds and maven builds produce different bytecode due to ignoring project settings which shouldn't be the case
eclipse-platform/eclipse.platform#973 identified that IDE builds and maven builds produce different bytecode due to ignoring project settings which shouldn't be the case
eclipse-platform/eclipse.platform#973 identified that IDE builds and maven builds produce different bytecode due to ignoring project settings which shouldn't be the case
eclipse-platform/eclipse.platform#973 identified that IDE builds and maven builds produce different bytecode due to ignoring project settings which shouldn't be the case
Should fix eclipse-platform/eclipse.platform.releng.aggregator#1653 . Further work on the topic could be continued as described in eclipse-platform#973 (comment)
Fixes eclipse-platform/eclipse.platform.releng.aggregator#1653