简体   繁体   English

我应该查看java编译器生成的字节码吗?

[英]Should I look at the bytecode that is produce by a java compiler?

No

  • The JIT compiler may "transform" the bytecode into something completely different anyway. 无论如何,JIT编译器可以将字节码“转换”为完全不同的东西。
  • It will lead you to do premature optimization. 它会引导你做过早的优化。

Yes

  • You do not know which method will be compiled by the JIT, so it is better if you optimize them all. 您不知道JIT将编译哪种方法,因此如果您优化它们会更好。
  • It will make you a better Java programmer. 它将使您成为更好的Java程序员。

I am asking without really knowing (obviously) so feel free to redirect to JIT hyperlinks. 我问的是没有真正知道(显然)所以随意重定向到JIT超链接。

Yes, but to a certain extent -- it's good as an educational opportunity to see what is going on under the hood, but probably should be done in moderation. 是的,但在某种程度上 - 作为一个教育机会,看看幕后发生了什么是好的,但可能应该适度地完成。

It can be a good thing, as looking at the bytecode may help in understanding how the Java source code will be compiled into Java bytecode. 这可能是一件好事,因为查看字节码可能有助于理解如何将Java源代码编译成Java字节码。 Also, it may give some ideas about what kind of optimizations will be performed by the compiler, and perhaps some limitations to the amount of optimization the compiler can perform. 此外,它可能会给出一些关于编译器将执行何种优化的想法,以及编译器可以执行的优化量的一些限制。

For example, if a string concatenation is performed, javac will optimize the concatenation into using a StringBuilder and performing append methods to concatenate the String s. 例如,如果执行字符串连接, javac将优化连接以使用StringBuilder并执行append方法来连接String

However, if the string concatenation is performed in a loop, a new StringBuilder may be instantiated on each iteration, leading to possible performance degradation compared to manually instantiating a StringBuilder outside the loop and only performing append s inside the loop. 但是,如果在循环中执行字符串连接,则可以在每次迭代时实例化新的StringBuilder ,与在循环外手动实例化StringBuilder并仅在循环内执行append s相比,可能导致性能下降。

On the issue of the JIT. 关于JIT的问题。 The just-in-time compilation is going to be JVM implementation specific, so it's not very easy to find out what is actually happening to the bytecode when it is being converted to the native code, and furthermore, we can't tell which parts are being JITted (at least not without some JVM-specific tools to see what kind of JIT compilation is being performed -- I don't know any specifics in this area, so I am just speculating.) 即时编译将特定于JVM实现,因此在将字节码转换为本机代码时,找出字节码实际发生的情况并不容易,而且,我们无法分辨哪些部分是JITted(至少没有一些特定于JVM的工具,看看正在执行什么样的JIT编译 - 我不知道这个领域有什么细节,所以我只是在猜测。)

That said, the JVM is going to execute the bytecode anyway, the way it is being executed is more or less opaque to the developer, and again, JVM-specific. 也就是说,无论如何,JVM将执行字节码,它的执行方式对开发人员来说或多或少是不透明的,同样也是JVM特有的。 There may be some performance tricks that one JVM performs while another doesn't. 一个JVM可能会执行一些性能技巧,而另一个则不会。

When it comes down to the issue of looking at the bytecode generated, it comes down to learning what is actually happening to the source code when it is compiled to bytecode. 当涉及到查看生成的字节码的问题时,它归结为在编译为字节码时学习源代码实际发生的事情。 Being able to see the kinds of optimizations performed by the compiler, but also understanding that there are limits to the way the compiler can perform optimizations. 能够看到编译器执行的各种优化,但也了解编译器可以执行优化的方式有限。

All that said, I don't think it's a really good idea to become obsessive about the bytecode generation and trying to write programs that will emit the most optimized bytecode. 总而言之,我不认为对字节码生成过敏并试图编写将发出最优化字节码的程序是一个非常好的主意。 What's more important is to write Java source code that is readable and maintainable by others. 更重要的是编写其他人可读和可维护的Java源代码。

That depends entirely on what you're trying to do. 这完全取决于你想要做什么。 If you're trying to optimize a method/module, looking at the byte code is going to be a waste of your time. 如果您正在尝试优化方法/模块,查看字节代码将浪费您的时间。 Always profile first to find where your bottlenecks are, then optimize the bottlenecks. 始终首先分析您的瓶颈所在,然后优化瓶颈。 If your bottleneck seems as tight as it possibly can be and you need to make it faster, you may have no choice but to rewrite that in native code and interface with JNI. 如果您的瓶颈看起来尽可能紧,并且您需要加快速度,那么您可能别无选择,只能在本机代码中重写它并与JNI进行交互。

Trying to optimize the generated bytecode will be of little help, since the JIT compiler will do a lot of work, and you won't have much of an idea of exactly what it's doing. 尝试优化生成的字节码将没有多大帮助,因为JIT编译器将做很多工作,并且你不会对它正在做什么有太多的了解。

I wouldn't think so. 我不这么认为。 Short of having to debug the javac compiler or wanting to know as a matter of interest, I cannot think of one good reason why someone would care what bytecode gets generated. 除了必须调试javac编译器或想要知道感兴趣之外,我想不出有人为什么会关心生成什么字节码的一个很好的理由。

Knowing bytecode won't make you a better Java programmer any more than knowing how an internal combustion engine works will make you a better driver. 知道字节码不会让你成为一个更好的Java程序员,只要知道内燃机如何工作将使你成为一个更好的驱动程序。

Think in terms of abstractions. 从抽象的角度思考。 You don't need to know about the actions of quarks or atoms when trying to calculate the orbits of planets. 在计算行星轨道时,你不需要知道夸克或原子的作用。 To be a good Java programmer, you should probably learn ... um .. Java. 要成为一名优秀的Java程序员,你应该学习......嗯.. Java。 Yes, Java, that's it :-) 是的,Java,就是这样:-)

Unless you're developing a high-capacity server of some sort, you'll likely never need to examine the bytecode, except out of curiosity. 除非您正在开发某种类型的高容量服务器,否则您可能永远不需要检查字节码,除了好奇心。 Source code that adheres to acceptable coding practices in your organization will provide ample performance for most applications. 遵循组织中可接受的编码实践的源代码将为大多数应用程序提供充足的性能。

Don't fret over performance until you've found issues after load-testing your application (or the entire customer service force lynches you for that screen that takes "forever" to load). 在对应用程序进行负载测试之后发现问题(或者整个客户服务部门为您完成“永久”加载的屏幕时,您不会担心性能问题)。 Then, hammer away at the bottlenecks and leave the rest of the code alone. 然后,锤击瓶颈并将剩下的代码单独留下。

Bytecode requires a modest learning curve to understand. 字节码需要适度的学习曲线才能理解。 Sure, it never hurts to learn more, but pragmatism suggests putting it off until it's necessary. 当然,学到更多东西永远不会伤害,但实用主义建议在必要时将其推迟。 (And should that moment come, I recommend finding someone to mentor you.) (如果那一刻到来,我建议找人来指导你。)

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

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