Re: Swing uses bad features
On Mon, 12 Jun 2006 11:57:50 +0100, Chris Uppal wrote:
Owen Jacobson wrote:
Out of curiousity, how
*are* methods dispatched by the JVM? It's obvious that an offset-based
vtable would make no sense; it'd break as soon as a parent class was
recompiled, which demonstrably doesn't happen.
It's difficult to say how method dispatch works in "the" JVM because
it's specific to a particular JVM /implementation/.
Which I knew. Serves me right for posting from bed: I get imprecise, even
when talking of precise things.
I guess that it's something like the following.
Consider a method call like
o.someMethod();
where o is not an interface type.
[...]
Otherwise, if the containing method is executed frequently:
Inline the call to aMethod() if possible and worthwhile.
If not then use a very small (size ~= 1) cache at each call site mapping
concrete types to addresses in memory of the corresponding methods. So
each method call is converted to a test-and-jump when the cached <type,
address> is "correct". Fall back to a vtable if the test fails.
That's... actually, alarmingly elegant. I've done something a lot like
that for a math library which occasionally re-performed the same function
on the same input arguments, by bolting on a one-element cache.
If the call site is known to be megaphorphic[*] (fairly rare) then don't
bother with a cache, use a vtable always. ("megamorphic" means that the
single virtual call site resolves to many different concrete methods
during execution.)
This is for a non-interface invocation site, or I'd disagree with
megamorphism being "rare". :)
It might help you (as a C++-er) to know that what javac does is hardly
even similar to what gcc or MSVC does when compiling C++. The JVM
bytecode set is /not/ like an instruction set definition for a
Von-Neumann machine (it's not similar, even in concept, to the IA32 or
SPARC instruction sets).
Yeah. I've had a lot of fun with javap and the BCEL tools. The Sun JVM
does some funny things if you feed it debatably-valid class files
("interfaces" that contain concrete methods, etc). Or it used to; I
haven't done much with the 1.5 series except write actual code. I'm
actually much more at home with Java than with C++, but I have a firmer
grasp of how C++ compilers handle some details -- like invocations -- than
of how JVMs deal with it.
-O