addressalign-toparrow-leftarrow-leftarrow-right-10x10arrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscontroller-playcredit-cardcrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobe--smallglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1languagelaunch-new-window--smalllight-bulblightning-boltlinklocation-pinlockm-swarmSearchmailmediummessagesminusmobilemoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonprintShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstar-shapestartickettrashtriangle-downtriangle-uptwitteruserwarningyahooyoutube

Re: [ljc] Java compile time optimizations?

From: user 8.
Sent on: Monday, April 29, 2013, 11:03 AM
Hello Bruno,

You probably know that the garbage collector is your best friend but also your worst enemy.

So, I would recommend you "get rid of GC" as much as possible. I mean: first focus on eliminating unnecessary object creation/destruction, then (and only after that) think about other performance issues.

If you are using JCF, I would recommend you substitute the standard implementation by an alternative one. In particular, I recommend fastutil http://fastutil.di.unimi.it/ which performs from 2 to 5 times faster (or sometimes 10 times faster!) than the standard implementation. In a nutshell, fastutil implements the standard JFC interfaces, so you are able to replace one by another easily. More than that, there are additional interfaces which avoid boxing/unboxing.

The second recommendation is not much different from the first one: allocate data structures and pin them in memory, reusing space (like circular buffers, for example) instead of creating/destroying objects.

The third recommendation [ you will see that it ] is not different from the first one, actually: do not use objects. Use primitive types as much as possible. The Java developer conscious about performance, codes like an "old school pure C" developer.

The last recommendation is: immutability. I don't know details about how JIT optimizations, but it's obvious that immutability potentially helps the code optimizer. Immutability is also a very important point if you are using multithreading. If you are not using multithreading, you could use "divide to conquer", using all resources your processors (or multiple cores) can offer.

I hope it helps

Richard Gomes
http://rgomes.info
[masked]

On 29/04/13 10:39, Bruno Medeiros wrote:
Hello LJC.

I've recently been developing Java code which is more computationally intensive (a parser) than what I typically work in. And so I've been more sensitive to low-level performance issues (ie, not algorithmic performance) than I think I've ever been, when programming in Java. Due to that, I've been reading a bit more about performance features of the JVM.

I was particularly interested in Java eliminating unnecessary allocations using escape analysis, since I've had an idea for a code idiom that I could use pervasively in my code under development that would make it quite more elegant and safe. But it uses several temporary objects, so it could make it slower, unless the compiler eliminates those unnecessary allocations.

So, from what I've looked up online, I've been surprised, and even a bit annoyed, by some of my findings. First, fortunately, Java does do escape analysis, although a bit scaringly, it was only first introduced in Java6, and then even so only to elide locks and other synchronization stuff. If I understood correctly, only recently in Java7 was escape analysis actually used to elide some object allocations as well... (you'd think that with so much effort put into GC optimization and technology, these other optimizations would have come a lot sooner!)

But the thing that annoys me, and the point of this post, is that all these and other optimizations occur in the JIT phase. They don't occur when you compile your source code. In fact, from my understanding the Java compiler (at least the Oracle JDK one) does little to no optimizations during source compilation. Just some inlining of constants, removing unused variables and that's it, nothing complex.

I know that a few people here in the LJC are quite involved with the high-tiers of development of the Java technology itself (the language, the JDK, etc.) , so does anyone know if there is a good reasoning for that? Is it something that could be changing in the future, since Oracle took over and JDK development is picking up more pace?

I do know that the JVM does runtime performance profiling, and uses that profiling to make better choices for JIT compilation and optimization, using runtime data that would not be available at compile-time. That's all good and fine, but it doesn't mean compile-time optimization should be abandoned entirely. There are still many compile-time optimizations that are worthwhile to do, like the aforementioned escape analysis, even if in some cases they could be optimized even further in runtime by the JIT compiler, for certain runtime scenarios.

Some consequences of this (unfortunate, in my view) status quo is the dichotomy of having both a Server and Client HotSpot runtimes to chose from (http://stackoverflow.com/questions/198577/real-differences-between-java-server-and-java-client). This is annoying because it is a tricky choice to make for desktop (non-server) applications. For server applications it's easy, you nearly always want the server HotSpot, but otherwise the choice is not so clear. Sometimes for a desktop application you will get better overall performance by running it with the Server Hotspot if your application runs long enough, despite the overhead caused by the more complexing JITting. But again, why isn't most of this overhead just dealt at development compile-time?...

Cheers.




--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Bruno Medeiros ([address removed]) from LJC - London Java Community.
To learn more about Bruno Medeiros, visit his/her member profile
Set my mailing list to email me As they are sent | In one daily email | Don't send me mailing list messages

Meetup, POB 4668 #37895 NY NY USA 10163 | [address removed]

People in this
group are also in: