Java Typecasting Internals

J

jsc59

What actually happens in JVM code when you execute the following

-- snippet ------------------------------
Vector vec = new Vector()
MyClass a, b;
vec.add(a); ***
b = (MyClass)vec.get(0); ***
---------------------------------------------

Specifically in the last two (starred) lines, how are the casts to
Object and MyClass performed.

I'm trying to create a high performance java application and I need to
know if casts are prohibitively expensive.

Thanks,
-Jordan
 
R

Roedy Green

I'm trying to create a high performance java application and I need to
know if casts are prohibitively expensive.

How is a cast implemented?

On the left you have a type on the right you have a type. You want to
know if the item on the right is an instance of the type on the left
to allow the cast to proceed. So that boils down to how could you
efficiently implement instanceof.

The flat footed way would be to compare the address pointers to the
class object in the each side. If they don't watch chase the chain to
the superclass on the left. Compare that with the original class on
the left. Percolate your way up the chain of superclasses.

You could prepare a list of all those left superclass class object
addresses and cache them, right next to each other. If the list is
very long, you might create a mini-hash lookup or a binary search
lookup.

Whenever I travel on a ferry, for some reason, my mind chews on the
problem of a more efficient instanceof, and some overhead free way to
do virtual functions. I play with numbering the classes in a magic
way, but the problem is new classes come into being all the time
disturbing my carefully constructed tables.

It might be interesting to see how HotSpot and Jet have been able to
implement it.
 
T

Tom Hawtin

Roedy said:
The flat footed way would be to compare the address pointers to the
class object in the each side. If they don't watch chase the chain to
the superclass on the left. Compare that with the original class on
the left. Percolate your way up the chain of superclasses.

You could prepare a list of all those left superclass class object
addresses and cache them, right next to each other. If the list is
very long, you might create a mini-hash lookup or a binary search
lookup.

For a class you know how many superclasses it has. So for each
instantiatable class, you can have a table of its super classes. So
checking if an object is an instance of a class is as easy as loading a
pointer to the class data from the instance header, and then testing
whether a pointer to the target class is at a fixed offset.

Anyway there is a bit more adaptive compiler magic than this, and
interfaces are more complex. So if you really are concerned, benchmark
your code. The answer should is almost certainly that it makes very
little difference.

Tom Hawtin
 
J

Joshua Cranmer

What actually happens in JVM code when you execute the following

-- snippet ------------------------------
Vector vec = new Vector()
MyClass a, b;
vec.add(a); ***
b = (MyClass)vec.get(0); ***
---------------------------------------------

The equivalent bytecode would like something like this:

new Ljava/util/Vector;
dup
invokespecial Ljava/util/Vector;.<init>()V
astore_1
aload_1
aload_2
invokevirtual Ljava/util/Vector;.add(Ljava/lang/Object)Z
pop
aload_1
iconst_0
invokevirtual Ljava/util/Vector;.get(I)Ljava/lang/Object;
checkcast LMyClass;
astore_2

Obviously, casting to Object shouldn't involve any penalties (it knows
it's an Object already). The cast to MyClass, however, requires checking
the classes for some sort of hierarchy; it is possible that the VM caches
the hierarchy into a small hashtable.
 
L

ldv

How is a cast implemented?

On the left you have a type on the right you have a type. You want to
know if the item on the right is an instance of the type on the left
to allow the cast to proceed. So that boils down to how could you
efficiently implement instanceof.

The flat footed way would be to compare the address pointers to the
class object in the each side. If they don't watch chase the chain to
the superclass on the left. Compare that with the original class on
the left. Percolate your way up the chain of superclasses.

You could prepare a list of all those left superclass class object
addresses and cache them, right next to each other. If the list is
very long, you might create a mini-hash lookup or a binary search
lookup.

Whenever I travel on a ferry, for some reason, my mind chews on the
problem of a more efficient instanceof, and some overhead free way to
do virtual functions. I play with numbering the classes in a magic
way, but the problem is new classes come into being all the time
disturbing my carefully constructed tables.

It might be interesting to see how HotSpot and Jet have been able to
implement it.

One trick is to cache the class of the last object for which this
particular instanceof() was called. Chances are good that the next
time the object in question will have the same class. (I mean, in real
programs, not in a benchmark designed specifically against the above
speculation.)

LDV
 
R

Roedy Green

For a class you know how many superclasses it has. So for each
instantiatable class, you can have a table of its super classes. So
checking if an object is an instance of a class is as easy as loading a
pointer to the class data from the instance header, and then testing
whether a pointer to the target class is at a fixed offset.

So for a class instanceof is relatively quick. However that won't
work for interfaces. I have heard that instanceof for interfaces used
to be much slower, but now it is souped up. I don't know how it works
though.
 
R

Roedy Green

For a class you know how many superclasses it has. So for each
instantiatable class, you can have a table of its super classes. So
checking if an object is an instance of a class is as easy as loading a
pointer to the class data from the instance header, and then testing
whether a pointer to the target class is at a fixed offset.

I feel such an ninny. I went to add your observation to the entry at
http://mindprod.com/jgloss/instanceof.html and discovered it was
already there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top