Performance of "instanceof"

John Rose John.Rose at Sun.COM
Thu Jan 15 08:09:43 PST 2009


It would be a good idea for some of us to make a page on instanceof  
here:
   http://wikis.sun.com/display/HotSpotInternals

(If someone else starts it I will contribute.)

HotSpot has a 1-1 relation between java.lang.Class instances and Java  
types; any array, instance class, or interface also has an internal  
meta-data object called a Klass.  It is a single indirection to get  
from either to its sibling, so things like foo.class.isAssignableFrom 
(bar.getClass()) tend to cook down to the right things.  The server  
compiler knows how to optimize away things like aConstantClass.klass  
and foo.klass.Class.klass (=> foo.klass).  To observe the  
optimizations which handle this, look in the source code for uses of  
java_lang_Class::klass_offset_in_bytes() and  
Klass::java_mirror_offset_in_bytes().

But, in order to see if your use case is getting helped, there is no  
substitute for examining the code:
   http://wikis.sun.com/display/HotSpotInternals/PrintAssembly

HTH
-- John

On Jan 15, 2009, at 3:21 AM, Ulf Zibis wrote:

> Thanks Tom,
>
> I was asking because of my RFE 8 in http://bugs.sun.com/bugdatabase/ 
> view_bug.do?bug_id=6790402.
> So I can be sure, that my proposed shortcut is much faster than the  
> saved HashMap lookup, and hopefully HotSpot remembers the result of  
> "instanceof", so following cast to String could be skipped.
> I was in doubt, how the right part of "instanceof" would retrieve a  
> reference pointer to the Class class from the given class name. So  
> as I understand you right, MyClass.klass is static final available  
> from bytecode.
>
> -Ulf
>
>
> Am 15.01.2009 03:19, Tom Rodriguez schrieb:
>> instanceof is the fastest way of asking the question it's designed  
>> to ask.



More information about the hotspot-dev mailing list