Converting local variables to fields can actually adversely affect performance if the code is not optimized by JIT (see this question and related questions for more information). But I see that, depending on the variables that this implies, there are unlikely to be other possible options.
There may be additional restrictions on compilation size and method. Peter Lowry mentioned in the comments that "... methods larger than 8 KB are not compiled by default" - I did not know about this, but he usually knows what he is talking about, so you have to dig a little deeper here, In addition, you you may need to look at HotSpot VM Settings to find out what additional restrictions and settings might be relevant to you. I primarily thought that
-XX:MaxInlineSize=35 : The maximum bytecode size for the inline method.
maybe something to keep in mind.
(In fact, calling so many methods with a MaxInlineSize size that nesting all these calls will exceed 65 thousand bytes for the containing method can be a neatly unpleasant test case for reliability and edge random testing of the insertion process ...)
You have outlined a “telescoping” scheme for the methods:
void run1(){ ... run2(); } void run2(){ ... run3(); }
This can also lead to problems: assuming that you have 650 of these methods (at best), this will at least lead to a deep stack very much and can actually raise a StackOverflowError - again, depending on some memory options . You may need to increase the size of the stack by setting the corresponding -Xss parameter.
The actual description of the problem was a bit vague and without additional information about the code to be generated (also regarding questions about how many local variables you need, which you might have to turn into instance variables, etc.), I'd suggest the following:
- Create lots of small methods if possible (considering
MaxInlineSize ) - Try reusing these small methods (if such reuse can be detected by input with reasonable effort)
Call these methods sequentially, as in
void run() { run0(); run1(); ... run2000(); }
to avoid stack size issues.
However, if you added additional examples or details, you could probably give more focused advice. This may even be a “complete” example - it’s not necessary to include thousands of lines of code, but showing the actual patterns that appear there.