> > Is there something the JLS (or elsewhere) that forbids the compiler
> > from performing these kinds of optimizations?
[quoted text clipped - 3 lines]
> be calls to private, static, or final, methods in the same class. By "the same
> class" I mean the /same/ class ;-) Nested classes, etc, don't count.
In fact, HotSpot does a lot of so called speculative optimizations, and
so do some other JIT compilers. Suppose the HotSpot engine decides to
JIT compile a method BigBar() and detects that a certain method
SmallFoo() is worth inlining into BigBar(), but SmallFoo() is not
declared as final. If SmallFoo() is not overridden in any of the
classes loaded so far, HotSpot will inline it into BigBar() and compile
BigBar() to native code. If at some later point a class overriding
SmallFoo() gets loaded, HotSpot will simply discard the results of
BigBar() compilation and BigBar() will again run on the interpreter.
> The "binary compatibility stuff" is basically how the dynamic nature of the JVM
> is reflected in the Java language spec. I haven't checked how the spec works
[quoted text clipped - 4 lines]
> The JVM can optimise a lot harder, because it has /all/ the relevant
> information available at any given moment.
Basically, at any moment of time the JVM can make whatever assumptions
and do whatever optimizations that are correct for the set of classes
loaded at that moment, provided it will be able to undo any such
optimization should loading a new class break the respective
assumptions.
> I suppose that a Java-like architecture could be defined where the compiler
> emitted two sets of bytecode, one generated on the assumption that nothing
> significant would change (and the classfile would include a list of what
> the exact assumptions were), and the other very conservative in its
> assumptions. The JVM could then select which bytecode to use depending on
> whether the assumptions turned out to be valid at runtime.
I'd say there is no need for such architecture, the JVMs are already
smart enough.
> Static compilers
> like Excelsior JET could use similar techniques, and -- for all I know -- maybe
> that's what they do...
Excelsior JET uses a somewhat different technique for the above
scenario. Being a static compiler, it cannot undo optimizations, so it
implements a very fast runtime check for inlined non-final methods.
That is, if the instance method supposed to be called at the given
point is indeed the method that was inlined during static compilation,
the inlined copy will be executed, otherwise a virtual call will occur.
LDV
http://www.excelsior-usa.com/jet.html
Thomas Hawtin - 03 Jun 2006 07:21 GMT
> In fact, HotSpot does a lot of so called speculative optimizations, and
> so do some other JIT compilers. Suppose the HotSpot engine decides to
[quoted text clipped - 5 lines]
> SmallFoo() gets loaded, HotSpot will simply discard the results of
> BigBar() compilation and BigBar() will again run on the interpreter.
That's certainly not how the server version of HotSpot (C2) works.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Marc Dzaebel - 03 Jun 2006 08:59 GMT
>>> That's certainly not how the server version of HotSpot (C2) works.
Hi Thomas,
I'd be interested in the different behaviors of the server and client VM
with respect to inlining. As of today, I assume that both the client and
server VM reasonably handles inlining. That means, that my framework design
could break methods into pieces without suffering from performance
degradation.
Thanks, Marc
ldv@mail.com - 05 Jun 2006 09:47 GMT
>> [skip]
> That's certainly not how the server version of HotSpot (C2) works.
Well, the article
http://java.sun.com/developer/technicalArticles/Networking/HotSpot/inlining.html
does not say "HotSpot Client", but just "HotSpot". The article is
rather old, though.
LDV
Marc Dzaebel - 05 Jun 2006 12:16 GMT
>>> [skip]
>> That's certainly not how the server version of HotSpot (C2) works.
> http://java.sun.com/developer/technicalArticles/Networking/HotSpot/inlining.html
>
> does not say "HotSpot Client", but just "HotSpot". The article is
> rather old, though.
good link anyway, as it shows that the final keyword could defeat the entire
facility of modularization and reusability. HotSpot optimizes even without
final declarations.
Thanks, Marc
dimitar - 05 Jun 2006 13:04 GMT
> does not say "HotSpot Client", but just "HotSpot". The article is
> rather old, though.
From what I know, the only difference in the Client and the Server
versions of HotSpot is the actual thresholds used for different
optimizations.
Thomas Hawtin - 05 Jun 2006 13:48 GMT
> From what I know, the only difference in the Client and the Server
> versions of HotSpot is the actual thresholds used for different
> optimizations.
heh heh
http://blogs.sun.com/fatcatair/20050826
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
dimitar - 05 Jun 2006 14:39 GMT
Thanks, that was a good reading!
Chris Uppal - 03 Jun 2006 11:21 GMT
[me:]
> > I suspect that the binary compatibility stuff might make it effectively
> > impossible for most cases. [...]
>
> In fact, HotSpot does a lot of so called speculative optimizations, and
> so do some other JIT compilers.
By "the compiler" I meant javac, and I assumed (perhaps wrongly) that Oliver
did too. Looking back, I can see that I didn't make that clear. Apologies for
the confusion.
> > I suppose that a Java-like architecture could be defined where the
> > compiler emitted two sets of bytecode, one generated on the assumption
[quoted text clipped - 6 lines]
> I'd say there is no need for such architecture, the JVMs are already
> smart enough.
Agreed. Still, not all VM implementations are, or can be, that smart.
> Excelsior JET uses a somewhat different technique for the above
> scenario. Being a static compiler, it cannot undo optimizations, so it
> implements a very fast runtime check for inlined non-final methods.
> That is, if the instance method supposed to be called at the given
> point is indeed the method that was inlined during static compilation,
> the inlined copy will be executed, otherwise a virtual call will occur.
Interesting, thanks.
-- chris
Oliver Wong - 05 Jun 2006 14:41 GMT
> [me:]
>> > I suspect that the binary compatibility stuff might make it effectively
[quoted text clipped - 8 lines]
> Apologies for
> the confusion.
Yeah, I was talking about javac too. I know HotSpot can do all sorts of
crazy stuff. I was just surprised to find out that javac can't do some of
that stuff ahead of time of HotSpot.
- Oliver
ldv@mail.com - 07 Jun 2006 07:29 GMT
> Yeah, I was talking about javac too. I know HotSpot can do all sorts of
> crazy stuff. I was just surprised to find out that javac can't do some of
> that stuff ahead of time of HotSpot.
Optimizing bytecode ahead-of-time can negatively affect the JIT
compiler's optimziation abilities. javac used to have -O option before
the first Java 2 release (J2SE 1.3), where HotSpot was introduced. To
be more precise, the option was left in place to ensure backward
compatibility of the various tools that invoke javac, but the emitted
code was the same regardless of whether -O was specified. :)
LDV