简体   繁体   English

如何在JAVA中实现instanceof?

[英]How instanceof is implemented inside JAVA?

Now I'm writing an ORM Framework and very care about performance. 现在我正在编写一个ORM框架,非常关心性能。

In this Framework , I have to use instanceof and Class.isAssignableFrom to check type compability. 在此框架中,我必须使用instanceofClass.isAssignableFrom来检查类型兼容性。

So I have a little doubt about the performance of instanceof and Class.isAssignableFrom 所以我对instanceofClass.isAssignableFrom的性能有点怀疑

How slow exactly it is? 究竟有多缓慢?

instanceof is supposed to be faster, it's one bytecode operation instanceof应该更快,它是一个字节码操作

public static void main(String[] args) {
        boolean res1 = args instanceof Object;

bytecode 字节码

ALOAD 0
INSTANCEOF java/lang/Object
ISTORE 1

compare to 相比于

boolean res2 = Object.class.isAssignableFrom(args.getClass());

bytecode 字节码

LDC Ljava/lang/Object;.class
ALOAD 0
INVOKEVIRTUAL java/lang/Object.getClass()Ljava/lang/Class;
INVOKEVIRTUAL java/lang/Class.isAssignableFrom(Ljava/lang/Class;)Z
ISTORE 2

How instanceof is implemented inside JAVA? 如何在JAVA中实现instanceof?

The short answer is that it is platform dependent. 简短的回答是它依赖于平台。

The long answer is that you should be able to find out how it is implemented by writing a test case that uses instanceof , running it in a loop to ensure it gets JIT compiled, and then dumping and examining the native code. 答案很长,你应该能够通过编写一个使用instanceof的测试用例,在循环中运行它以确保它得到JIT编译,然后转储和检查本机代码来了解它是如何实现的。

However, I don't think this is going to be particularly instructive. 但是,我认为这不会特别有启发性。 What you really want to know is whether instanceof or Class.isAssignableFrom is faster. 您真正想知道的是instanceofClass.isAssignableFrom是否更快。 You can measure this by careful micro-benchmarking. 您可以通过仔细的微基准测试来衡量这一点。

FWIW, I predict that you will find that instanceof is faster. FWIW,我预测你会发现instanceof更快。 (I expect that the JIT compiler would be able to optimize instanceof in ways that it couldn't optimize the reflective version.) (我希望JIT编译器能够以无法优化反射版本的方式优化instanceof 。)

But lastly, I'd suggest that you don't waste your time with this level of optimization at this stage. 但最后,我建议你不要在这个阶段浪费你的时间进行这种优化。 Code this using instanceof , and wait until you have some profiling data that tells you that your instanceof usage is really a performance bottleneck. 使用instanceof对此进行编码,并等到您有一些分析数据告诉您instanceof使用确实是性能瓶颈。 (It is all very well to "care about performance" ... but in reality there are more important things that you need to get right BEFORE performance becomes a key issue.) (“关心性能”这一切都很好......但实际上,在性能成为关键问题之前,需要更加重要的事情才能正确。)

1st of all, if youre going to micro-benchmark this, at least run a large loop and average, because youre seeing a lot of noise in your timing. 首先,如果你要进行微观测试,至少要运行一个大循环和平均值,因为你的时间会看到很多噪音。
having said that, yes, reflection is slow. 说过,是的,反思很慢。 if you can design around it and use anything else, do it. 如果你可以围绕它设计并使用其他任何东西,那就去做吧。
for example, if the set of classes you'll work with is small and known in advance, keep them in a Map<Class,[Something]> and look them up there - you'll need all subclasses to be in that map, but the lookup will be much faster than an instanceof (thats basically how a lot of fast serialization libraries avoid reflection) 例如,如果您将使用的类集很小并且事先已知,请将它们保存在Map<Class,[Something]>并在那里查找它们 - 您将需要所有子类都在该映射中,但查找将比instanceof快得多(这基本上是很多快速序列化库避免反射的原因)
if you dont want to (of cant) build this map in advance you can build it as a cache at runtime and then you'll need the instanceOf call only once per new class 如果您不想(无法)提前构建此映射,您可以在运行时将其构建为缓存,然后每个新类只需要一次instanceOf调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM