Talk:Java performance: Difference between revisions

Content deleted Content added
Alliumnsk (talk | contribs)
Line 202:
Such differences are numerous and result in more differences than peephole optimizations.[[User:Alliumnsk|Alliumnsk]] ([[User talk:Alliumnsk|talk]]) 09:28, 23 October 2011 (UTC)
: There are two reasons why Java JIT compiler can rearrange calls to foo and bar, first is because there are no pointers in java, making it easier for the compiler to prove that the reordering (or inlining) will not change anything, second is because of deoptimization, the JIT compiler can make aggressive optimizations based on assumptions (not proof), and deoptimize the code (un-JIT it) if it appear over the time that its assumptions were not true anymore. Concerning your Apple / Fruit example, arrays are objects not pointers in Java, so you are not allowed to put something which is not an Apple in an Apple array. In C/C++, you can put anything in it since an array is just a pointer. [[User:Hervegirod|Hervegirod]] ([[User talk:Hervegirod|talk]]) 17:18, 23 October 2011 (UTC)
:: In this example, it cannot be 'easier' because cost of this for C/C++ is zero. Aggressive (dangerous) optimizations, again, involve run-time check. "No pointers" is more a naming issue. In C, if you have struct complex { float re,im; }; and never take addresses of its fields - re and im (why would you?), but only take addresses of struct complex - you get the same "no pointers" situation as in Java. You miss the Apple/Fruit example entirely. Java disallows this only at run-time, inserting dynamic type-checks. C++ disallows at compile-time. Well you can still put a Pear in Apple array (do you even know how? a simple typecast won't work), but it violates the rules. For one thing you can be sure, there will be no dynamic check for that just because compiler wasn't clever enough to prove it is not necessary.
<source lang="java">
class aa {
static class Fruit{}
static class Apple extends Fruit{}
static class Pear extends Fruit{}
public static void main(String args[]) {
Apple[] a = new Apple[7];
Fruit[] b = a; // since we are downcasting, there is no need to type-check
b[5]= new Pear(); // uh-oh! dynamic typecheck is hidden somewhere
}
}
</source>