9 Replies Latest reply on May 11, 2013 4:33 PM by ++sja

    List of optimizations performed by the VM and compiler?

    xStardust!
      Hello there!

      It is well-known that the Java compiler and Virtual Machine perform lots of optimizations on the code but I've yet to find a document explaining exactly what are these optimizations?
      I think it's important to know how my code will be optimized in order to take the right decisions, but even the VM specification doesn't seem to cover this topic.

      Does anybody know where I can find that kind of information? Is there a book about it or something?

      Thanks
        • 1. Re: List of optimizations performed by the VM and compiler?
          EJP
          It is well-known that the Java compiler and Virtual Machine perform lots of optimizations on the code
          No it isn't. It is well-known that the Java compiler doesn't perform any optimisations at all.
          but I've yet to find a document explaining exactly what are these optimizations?
          That's probably because there isn't one, and because it changes from release to release, and because the information is proprietary, and commercially valuable, and ...
          I think it's important to know how my code will be optimized in order to take the right decisions
          I disagree. It is well established that if you write straightforward code, without trying to second-guess the optimiser, it will do a seriously good job.
          but even the VM specification doesn't seem to cover this topic.
          Of course it doesn't. That's not its function. Its function is to define the execution of the language, which forms constraints within which optimisers may operate..
          • 2. Re: List of optimizations performed by the VM and compiler?
            gimbal2
            No it isn't. It is well-known that the Java compiler doesn't perform any optimisations at all.
            I experienced first hand last week that at least Oracle Java 6 does in fact chuck away "empty" useless code like this:
            if(bla == 5){
              // TODO: do stuff here
            }
            Which makes it quite hard to set debug breakpoints on it! I consider that somewhat of an optimization.

            Agreed on the rest. Except maybe for game programming where you need to know some of the inner workings to prevent a stutter in the frame rate due to for example the garbage collector doing heavy work. And on that subject, there is this interesting wiki:

            http://www.java-gaming.org/topics/hotspot-optimization/27009/view.html

            which unfortunately does not have links for all items noted to back it up.
            1 person found this helpful
            • 3. Re: List of optimizations performed by the VM and compiler?
              EJP
              Speaking as a compiler writer, that's not really an optimization, it's a documented feature, the equivalent of #if 0.

              I maintained commercial compilers for many years and the last thing we would have considered publishing was a list of optimization techniques. Just benchmark results ;-)
              • 4. Re: List of optimizations performed by the VM and compiler?
                xStardust!
                What I do is indeed game development and I assure you that knowing more about how the code is optimized would help.
                A game in Java uses lots of native calls which are notorious for being slow, so I can't afford to waste time anywhere else.
                Knowing, for example, how, when, and when not, the JIT inlines methods would allow me to take better decisions on the design of my program. Lots of methods contain trivial code that is used in many places in the code and knowing that I run the risk of needing to perform a function call every time is just creepy. I want to have a guarantee that they will be inlined.

                Anyways, thanks for the link!
                • 5. Re: List of optimizations performed by the VM and compiler?
                  EJP
                  I suggest it does all of the known optimisations, and probably a few you haven't thought of, but only after an unknown threshold.
                  • 6. Re: List of optimizations performed by the VM and compiler?
                    xStardust!
                    I'm looking for the details: the hows, the whens and the when-nots.

                    Let's say for example that I put a trivial, commonly-used method in a long loop, how do I know what will happen?
                    Will the compiler unroll the loop? If so, will it still inline the functions even though it would result in a larger code?
                    If it doesn't unroll the loop, will the methods be inlined then?
                    Under which conditions are methods inlined? Can the JIT inline non-static methods and when will it do so?
                    And I'd ask the same questions for every optimization that the JIT is able to perform.

                    This kind of information is important for writing code that will be efficient at runtime.
                    • 7. Re: List of optimizations performed by the VM and compiler?
                      EJP
                      Good luck with your quest.
                      • 8. Re: List of optimizations performed by the VM and compiler?
                        xStardust!
                        I see
                        that sucks :/

                        Now is there a way to see what the optimized code look like? I assume it would be code for the host machine right?
                        • 9. Re: List of optimizations performed by the VM and compiler?
                          ++sja
                          On recent versions of the JDK, you can print JDK-produced assembly with "java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly". You'll need an assembler pretty printer dll. First google hit for "java printassembly" gives instructions. See if they mention other interesting options like -XX:+LogCompilation.

                          Note there are two compilers in HotSpot, "java -client" (aka C1) and "java -server" (aka C2).

                          Another place where you might find info on optimization is the OpenJDK HotSpot wiki. <- google those three words.