I have an unusual requirement: My application generates Java code automatically from a very long script (written in a dynamically-typed language). The script is so long that I hit the maximum method size of 65k of the JVM.
The script consists only of simple instructions on primitive types (no call to other functions besides mathematical ones). It may look like:
...
a = b * c + sin(d)
...
if a>10 then
e = a * 2
else
e = a * abs(b)
end
...
... which is transformed as:
...
double a = b * c + Math.sin(d);
...
double e;
if(a>10){
e = a * 2;
}else{
e = a * Math.abs(b);
}
...
My first idea to overcome the method size limitation was the following:
- Turn all local variables into fields
- Split the code every 100 lines (or longer if needed in case of a if/else block) in separate methods.
Something like:
class AutoGenerated {
double a,b,c,d,e,....;
void run1(){
...
a = b * c + sin(d);
...
run2();
}
void run2(){
...
if(a>10){
e = a * 2;
}else{
e = a * Math.abs(b);
}
...
run3();
}
...
}
Do you know of any other way that would be more efficient? Note that I need the code to run as fast as possible as it will be executed in long loops. I cannot resort to compiling to C, as interoperability is also an issue...
I would also appreciate pointers to libraries that might help me.