Java 6类加载的速度有多快? [英] What's faster in Java 6 classloading?

查看:92
本文介绍了Java 6类加载的速度有多快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ProGuard 主页列出了一项功能:

The ProGuard home page lists as a feature:


  • 重新定位和预验证Java 6的现有类文件,以便充分利用Java 6的更快的类加载。

  • Retargeting and preverifying existing class files for Java 6, to take full advantage of Java 6's faster class loading.

这涉及到Java 6的区别是什么?

What is the difference in Java 6 that this refers to?

重要吗?

它是否会影响多线程通过默认类加载器的同步方面导致的减速?

Does it have impact on slowdowns caused by multithreading through the synchronized aspects of the default classloader?

推荐答案

正如 ProGuard 常见问题解答提示


Java 6编译器添加预验证信息到类文件

the Java 6 compiler add preverification information to the class files

查看 Java虚拟机规范 按类型检查验证部分:


如果a Java虚拟机实现曾尝试通过版本50.0类文件的类型推断来执行验证,它必须在所有验证的情况下执行验证通过类型检查失败。

If a Java virtual machine implementation ever attempts to perform verification by type inference on version 50.0 class files, it must do so in all cases where verification by typechecking fails.

这意味着Java虚拟机实现不能选择在一次情况下而不是在另一种情况下使用类型推断。它必须拒绝不通过类型检查验证的类文件,或者每当类型检查失败时始终故障转移到类型推断验证器。

This means that a Java virtual machine implementation cannot choose to resort to type inference in once case and not in another. It must either reject class files that do not verify via typechecking, or else consistently failover to the type inferencing verifier whenever typechecking fails.

类型检查器需要堆栈映射列表具有Code属性的每个方法的帧。类型检查器读取每个此类方法的堆栈映射帧,并使用这些映射生成Code属性中指令类型安全性的证明。

The type checker requires a list of stack map frames for each method with a Code attribute. The type checker reads the stack map frames for each such method and uses these maps to generate a proof of the type safety of the instructions in the Code attribute.

从Java 6开始,类文件50.0及更高版本,JVM可以在类文件验证期间使用类型检查或类型推断。在尝试理解性能优势之前,什么是类型检查和类型推断?本文,面向对象编程的类型检查和类型推断语言声明:

Beginning with Java 6, class files 50.0 and above, a JVM may use type checking or type inference during class file verification. Before trying to understand performance benefits, what is type-checking and type-inference? This paper, Type-Checking and Type-Inference for Object-Oriented Programming Languages states:


类型系统是编程语言的重要组成部分。完全依赖于运行时类型检查的语言提供了高度的灵活性,但通常必须牺牲性能。

A type system is an important part of a programming language. Languages that rely completely on run-time type-checking provide a high degree of flexibility but must usually sacrifice performance to do so.

来自维基百科的类型推断

类型推断是在编译时自动推断表达式类型的能力。 [...]

Type inference is the ability to automatically deduce, either partially or fully, the type of an expression at compile time. [...]

要获取推断表达式类型所需的信息,编译器会将此信息收集为聚合并随后减少给定的类型注释因为它的子表达式,或者通过隐含地理解各种原子价值的类型[...]。

To obtain the information required to infer the type of an expression, the compiler either gathers this information as an aggregate and subsequent reduction of the type annotations given for its subexpressions, or through an implicit understanding of the type of various atomic values [...].

OpenJDK HotSport运行时概述很好地解释了:


目前有两种分析字节码的方法,以确定每条指令将出现的操作数的类型和数量。传统方法称为类型推断,通过对每个字节码执行抽象解释并在分支目标或异常句柄处合并类型状态来进行操作。分析迭代字节码,直到找到类型的稳定状态。如果找不到稳定状态,或者结果类型违反某些字节码约束,则抛出VerifyError。 [...]

There are currently two methods of analyzing the bytecodes to determine the types and number of operands that will be present for each instruction. The traditional method is called "type inference", and operates by performing an abstract interpretation of each bytecode and merging type states at branch targets or exception handles. The analysis iterates over the bytecode until a steady state for the types are found. If a steady state cannot be found, or if the resulting types violate some bytecode constraint, then a VerifyError is thrown. [...]

JDK6中的新功能是第二种验证方法,称为类型验证。在此方法中,Java编译器通过代码属性StackMapTable为每个分支或异常目标提供稳态类型信息。 StackMapTable由许多堆栈映射帧组成,每个堆栈映射帧指示表达式堆栈上的项目类型以及方法中某些偏移量的局部变量。然后,JVM只需要执行一次字节码传递,以验证类型的正确性,以验证字节码。 [...]

New in JDK6 is the second method for verification which is called "type verification". In this method the Java compiler provides the steady-state type information for each branch or exception target, via the code attribute, StackMapTable. The StackMapTable consists of a number of stack map frames, each which indicates the types of the items on the expression stack and in the local variables at some offset in the method. The JVM needs to then only perform one pass through the bytecode to verify the correctness of the types to verify the bytecode. [...]

类型检查意味着JVM可以通过类文件进行一次传递来验证类型系统;类型推断需要多次传递。这是一项显着的性能节省吗?它可能与您在应用程序中拥有的类的总数有关,以及您有多少类文件小于50.0(Java 6)和50.0及更高。如果您的应用程序不是性能关键应用程序,我不会担心它;如果是,那么在编译应用程序到Java 5和Java 6类文件时,可以运行一些基准来比较性能差异。

Type-checking means the JVM can do one pass through the class file to verify the type system; type-inference requires multiple passes. Is this a significant performance savings? It's probably relative to the total number of classes you have in your application and how many class files you have that are less than 50.0 (Java 6) and 50.0 and greater. If your application is not a performance critical application, I wouldn't worry about it; if it is, then you could run some benchmarks comparing performance differences when compiling your application to Java 5 and Java 6 class files.

这篇关于Java 6类加载的速度有多快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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