向下压铸时的铸造性能不同 [英] Casting performance in different levels when casting down

查看:109
本文介绍了向下压铸时的铸造性能不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我们有三个(或更多)类

Lets assume we have three (or more) classes

公共类A {}

公共类B扩展了A {}

public class B extends A {}

公共类C扩展了B的实现G {}

public class C extends B implements G {}

假设每个类都有自己的20种(或更多)方法。

Assume each class has its own 20 (or more) methods.

强制转换为C与强制转换为A是否会对性能产生较大影响? Java强制转换如何工作?

Is casting to C vs casting to A has larger impact on performance? How does Java casting work under the hood?

在强制转换时,是否必须通过反射检查所有方法和字段的存在性?

Does it have to check for all methods and fields for presence by reflection when casting down?

编辑:
类的大小(字段和方法的数量)是否会影响投射时的性能?
我对 OpenJRE Dalvik 都感兴趣。

作为参考,我知道

推荐答案

强制转换的性能取决于JVM的实现。

The performance of the casting depends on the JVM implementation.

JLS 5.5 仅确定转换的要求(其中包括递归算法),而未在实现时设置任何要求。实际上,5.5中的运行时强制转换规则.3 的确定方法也相同。产生与建议算法相同结果的所有JVM实现都被接受为正确的JVM。

The JLS 5.5 only determines the requirements for the casting (which includes a recursive algorithm), but does not set any requirements upon the implementation. Actually the runtime cast rules in 5.5.3 are also determined the same way. All JVM implementations that produce th same result as the proposed algorithm are accepted as a proper JVM.

通常,将其转换为 C 需要更多时间,因为JVM必须检查对象的运行时类型。强制转换为 A 时,没有理由进行相同的检查,因为 B 会扩展 A

Generally, casting down to C takes a little bit more time since the JVM must examine the runtime type of the object. When casting up to A it has no reason to do the same check, since B extends A.

实际上,JVM并不关心方法和字段的数量。它仅比较类型层次结构,您可以通过反射进行检查( o.getClass()

Actually, the JVM does not care about the number of methods and fields. It only compares the type hierarchy, the same you can examine with reflection (o.getClass())

I制作了一个示例代码,如下所示,一个向下转换,然后一个向上转换:

I made a sample code as follows, one downcast, then an upcast:

Object o = new Integer(1);
Integer i = (Integer) o;

Object o2 = i;

编译后的字节码如下:

 0  new java.lang.Integer [16]
 3  dup
 4  iconst_1       <-- 1 as a parameter to the constructor
 5  invokespecial java.lang.Integer(int) [18]   <-- constructor
 8  astore_1 [o]       <-- store in 'o'
 9  aload_1 [o]
10  checkcast java.lang.Integer [16]    <-- DOWNCAST CHECK, SPECIAL BYTECODE
13  astore_2 [i]
14  aload_2 [i]
15  astore_3 [o2]   <-- WITH UPCAST NO CHECK

因此,有一条特定的JVM指令使用给定的值检查堆栈顶部的元素类。

So, there is a specific JVM instruction that checks the element on the top of the stack with a given class.

上流了,根本没有检查。

With upcast, there is no check at all.

类的大小(字段数,方法,实际的占用空间)没关系,因为转换会检查 Class (元数据,它实际上是一个对象)。

The size of the classes (number of fields, methods, actual footprint) does not matter, because the casting examines the Class (the metadata, which is actually an object).

层次结构级别的数量,以及已实现接口的数量(如果强制转换为接口)很重要,因为这是要遍历的继承/实现树

The number of hierarchy levels, and the number if implemented interfaces (if casting to an interface) does matter, because that is the traversable inheritance/implementation tree to check.

如果此检查没有某种缓存,我会感到惊讶。

I would be surprised if there wouldn't be some kind of cache for this check.

这篇关于向下压铸时的铸造性能不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆