Skip to main content

Obsolete Java Optimization Tips



There are number of performance tips made obsolete by Java compiler and especially Profile-guided optimization . For example, these platform-provided optimizations can drastically (according to sources) reduces the cost of virtual function calls. VM is also capable of method inlining, loop unrolling etc.





What are other performance optimization techniques you came around still being applied but are actually made obsolete by optimization mechanisms found in more modern JVMs?


Comments

  1. The final modifier on methods and method parameters doesn't help with the performance at all.

    Also, the Java HotSpot wiki gives a good overview of the optimizations used by HotSpot and how to efficiently use them in Java code.

    ReplyDelete
  2. People replacing String a = "this" + var1 + " is " + var2; with multiple calls to StringBuilder or StringBuffer. It actually already uses StringBuilder behind the scenes.

    ReplyDelete
  3. In 2001 I made apps for a J2ME phone. It was the size of a brick. And very nearly the computational power of a brick.

    Making Java apps run acceptably on it required writing them in as procedural fashion as possible. Furthermore, the very large performance improvement was to catch the ArrayIndexOutOfBoundsException to exit for-loops over all items in a vector. Think about that!

    Even on Android there are 'fast' loops through all items in an array and 'slow' ways of writing the same thing, as mentioned in the Google IO videos on dalvik VM internals.

    However, in answer to your question, I would say that it is most unusual to have to micro-optimise this kind of thing these days, and I'd further expect that on a JIT VM (even the new Android 2.2 VM, which adds JIT) these optimisations are moot.
    In 2001 the phone ran KVM interpreter at 33MHz. Now it runs dalvik - a much faster VM than KVM - at 500MHz to 1500MHz, with a much faster ARM architecture (better processor even allowing for clock speed gains) with L1 e.t.c. and JIT arrives.

    We are not yet in the realms where I'd be comfortable doing direct pixel manipulation in Java - either on-phone or on the desktop with an i7 - so there are still normal every-day code that Java isn't fast enough for. Here's an interesting blog that claims an expert has said that Java is 80% of C++ speed for some heavy CPU task; I am sceptical, I write image manipulation code and I see an order of magnitude between Java and native for loops over pixels. Maybe I'm missing some trick...? :D

    ReplyDelete
  4. It is necessary to define time/memory trade-offs before starting the performance optimization. This is how I do it for my memory/time critical application (repeating some answers above, to be complete):


    Rule #1 Never do performance optimization on the earlier stage of development. Never do it if your don't need it really. If decided to do it, then:
    use profiler to find bottlenecks, review the source code to find the reasons for bottlenecks;
    choose appropriate data structure with the best fit into the defined time/memory trade-offs;
    choose appropriate algorithms (e.g. iteration vs recursion, etc);
    avoid using synchronized objects from java library, if you don't need it really;
    avoid explicitly/implicitly new object creation;
    override/re-implement data types/algorithms coming with the java if and only if you are sure they doesn't fit your requirements.
    Use small, independent tests to test the performance of chosen algos/data structures.

    ReplyDelete
  5. "Premature optimization is the root of all evil"(Donald Knutt)
    It is useful to optimize only the bottlenecks places.
    You should analyze the code in each situation. May be you can replace the TreeSet by more fast HashSet, because you don't need sorting feature or may be you can use float instead of double( look in Android SDK).
    If no technique helps, you can try to rewrite a piece of code and call it via JNI, so that native code is working.

    ReplyDelete
  6. Don't manually call the garbage collector, it hurts performance on modern JVM implementations.
    Integer instead of Long will not save much space, but will limit the range of the numbers.
    Avoid hand generated Enum classes and use the built in Enum instead. Java 1.5 introduced real Enums, use them.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex