I’m working on a project in which we are producing a language which compiles to java. The framework we are using (xtext) makes prolific use of boxing in its generated code.
Specifically, if you have a statement like:
int i = 1;
int j = 2;
int k = i + j;
Then the compiled code looks like:
IntegerExtensions.operator_plus(((Integer)i), ((Integer)j))
Now, in the project I’m working on, there are certain situations where particular basic binary operations are going to be extremely common (especially increments and comparisons).
My question is: is this going to be a problem in terms of performance, or will JIT (or similarly intelligent JVM features) simply realize what’s going on and fix it all?
PLEASE READ BEFORE POSTING: I’m not interested in getting responses saying “you shouldn’t care, make it readable”. This code is generated, and I simply don’t care about the readability of the generated code. What I do care about is that we don’t take a significant performance hit from this.
Thanks
This can in fact have an impact. When the cast to
Integeroccurs it will convert theinttoIntegerusingInteger.valueOf(int n)method. This method will check to see if the value is within the cache range (-128 to 127) and if it is not it will createnew Integer(n)The amount of an impact may be a lot or little, you would have to test yourself.