Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does boxing cause performance issues?

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

like image 723
Jeff Avatar asked Dec 11 '11 23:12

Jeff


3 Answers

This can in fact have an impact. When the cast to Integer occurs it will convert the int to Integer using Integer.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 create new Integer(n)

The amount of an impact may be a lot or little, you would have to test yourself.

like image 83
John Vint Avatar answered Sep 22 '22 03:09

John Vint


To say that it causes performance issues is dependent on what you'd call an issue. And what you'll call an issue is probably dependent on what kind of problems the code will be solving.

There's a section in this answer that sums it up, and also provides a link to the Autoboxing guide, which mentions:

It is not appropriate to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code.

And here's a specific example with benchmarks focusing on int/Integer autoboxing

Simple question: How expensive is autoboxing of int/Integer types?

Simple answer: 15 nanoseconds per boxing.

like image 34
Craig Otis Avatar answered Sep 23 '22 03:09

Craig Otis


A few observations from my experience:

  1. Boxing in general does decrease the performance of an application. How noticeable it is depends on the nature of the implemented algorithms. Whether it's worth fixing and where is something only a profiler and your expected cost-to-benefit ratio can tell you.

  2. Boxing in general does increase the memory usage of an application. This, as far as I am concerned, is very important - probably more important than performance.

    An int in Java takes up 4 to 8 bytes (depending on the JVM implementation) of memory for 32 bits of range. An Integer will take up 20 to 24 bytes on an 64-bit system - and you still need a reference to it. For an application that processes large arrays that could easily quadruple (x4) its memory requirements - or worse.

    In this case, boxing can make the difference between "It works" and "It does not work" - there is only so much memory you can have on a given computer. Performance does not even come into the discussion, although, memory-starved applications will generally be slower as well.

That said, objects do have a useful advantage: there is a native way to say "no value exists" by using null.

like image 32
thkala Avatar answered Sep 20 '22 03:09

thkala