The CompilerMost people who have used languages such as C/C++ are used to reams of options to optimize their code by doing things such as targeting different processors, different memory models etc. etc. In Java we are lucky to have even the one option we do, and most people don't even know it exists! The option I'm talking about is the -O option which turns inlining on. Inlining is when chunks of code are copied across from one part of the source to another so as to speed up execution. With C/C++ all you have to do is prefix the function with the inline keyword and the compiler does it for you. In Java you tell it to consider inlining functions and variables when you compile with the -O option. For a method/variable to be considered for inlining it must meet the following criteria:
Once javac (the compiler) has this list of candidates, it goes through them and makes the following decisions:
There's a couple of tricks you can use with this: Trick #1If there are a few methods in your program which are proving to be bottlenecks and they can't be inlined, a possible solution is to make a subclass of the class they are contained in and then making them final or private. Bear in mind that this is very bad practice and by no means an ideal solution. Trick #2Static final constants get stored in a class file as well as being inlined. Try putting them in an interface and then implementing that interface in each class which uses them, that way they only get stored once and the calls to them are still fast. Static and Dynamic bindingAnother thing which the -O option affects is static/dynamic binding. When some code calls a method it either dynamically or statically 'looks it up'. With static binding the code knows exactly where the method is going to be and directly calls it. With dynamic binding the code does not know where the method is and has to look for it at runtime. All of this is transparent to the coder. It's the Java Virtual Machine (JVM) and compiler which has to worry about this. Because of Java's architecture, most things are dynamically bound, that means that only the class you modify has to be recompiled. When you use the -O option, it allows static binding for methods and fields which are only used within a class, and cannot be used outside it. Static binding is much faster than dynamic binding.
|
|||||||||||