Does boxing hurt performance issues?

I am working on a project in which we create a language that compiles in java. The frame we use (xtext) makes a fruitful use of boxing in its generated code. In particular, if you have an operator like:

int i = 1;
int j = 2;
int k = i + j;

Then the compiled code is as follows:

IntegerExtensions.operator_plus(((Integer)i), ((Integer)j))

Now, in the project I'm working on, there are certain situations where specific basic binary operations will be extremely common (especially increments and comparisons).

My question is: will this be a performance issue, or will the JIT (or similar JVM smart features) just figure out what's going on and fix it all?

PLEASE READ BEFORE POSITION: I'm not interested in getting answers saying "you don't care, make it readable." This code is generated, and I just don't care about the readability of the generated code. I take care that we do not get significant success in performance.

thank

+5
source share
4 answers

It can actually have an effect. When a translation occurs in Integer, it converts intto Integerusing the method Integer.valueOf(int n). This method checks if the value is in the cache range (-128 to 127), and if it is not created,new Integer(n)

The number of strokes can be many or few, you have to test yourself.

+4

, , , . , , , , .

, , Autoboxing, :

.

, autoboxing int/Integer

: autoboxing int/Integer?

: 15 .

+3

:

  • . , . - , .

  • . , , - , , .

    An int Java 4 8 ( JVM) 32 . Integer 20 24 64- - . , , (x4) - .

    " " " " - . , , , .

, : "no value exists" null.

+3

: .

, . , .

+1

All Articles