> 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
[quoted text clipped - 5 lines]
> 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
Roedy Green - 06 Jul 2007 15:13 GMT
On Wed, 04 Jul 2007 12:00:11 +0100, Tom Hawtin
<usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone
who said :
>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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green - 06 Jul 2007 15:16 GMT
On Wed, 04 Jul 2007 12:00:11 +0100, Tom Hawtin
<usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone
who said :
>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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
On Jul 4, 8:57 am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Tue, 03 Jul 2007 21:53:15 -0000, j...@cornell.edu wrote, quoted or
> indirectly quoted someone who said :
[quoted text clipped - 30 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com
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