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

时间:2021-02-18 17:19:47

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超链接。

4 个解决方案

#1


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. 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.

这可能是一件好事,因为查看字节码可能有助于理解如何将Java源代码编译成Java字节码。此外,它可能会给出一些关于编译器将执行何种优化的想法,以及编译器可以执行的优化量的一些限制。

For example, if a string concatenation is performed, javac will optimize the concatenation into using a StringBuilder and performing append methods to concatenate the Strings.

例如,如果执行字符串连接,javac将优化连接以使用StringBuilder并执行追加方法来连接字符串。

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 appends inside the loop.

但是,如果在循环中执行字符串连接,则可以在每次迭代时实例化新的StringBuilder,与在循环外手动实例化StringBuilder并仅在循环内执行追加相比,可能导致性能下降。

On the issue of the 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.)

关于JIT的问题。即时编译将特定于JVM实现,因此在将字节码转换为本机代码时找出字节码实际发生的情况并不容易,而且,我们无法分辨哪些部分正在被JIT(至少没有一些特定于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. There may be some performance tricks that one JVM performs while another doesn't.

也就是说,无论如何,JVM将执行字节码,它的执行方式对开发人员来说或多或少是不透明的,同样也是JVM特有的。一个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源代码。

#2


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编译器将做很多工作,并且你不会对它正在做什么有太多了解。

#3


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. Yes, Java, that's it :-)

从抽象的角度思考。在计算行星轨道时,你不需要知道夸克或原子的作用。要成为一名优秀的Java程序员,你应该学习......嗯.. Java。是的,Java,就是这样:-)

#4


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.)

字节码需要适度的学习曲线才能理解。当然,学到更多东西永远不会伤害,但实用主义建议在必要时将其推迟。 (如果那一刻到来,我建议找人来指导你。)

#1


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. 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.

这可能是一件好事,因为查看字节码可能有助于理解如何将Java源代码编译成Java字节码。此外,它可能会给出一些关于编译器将执行何种优化的想法,以及编译器可以执行的优化量的一些限制。

For example, if a string concatenation is performed, javac will optimize the concatenation into using a StringBuilder and performing append methods to concatenate the Strings.

例如,如果执行字符串连接,javac将优化连接以使用StringBuilder并执行追加方法来连接字符串。

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 appends inside the loop.

但是,如果在循环中执行字符串连接,则可以在每次迭代时实例化新的StringBuilder,与在循环外手动实例化StringBuilder并仅在循环内执行追加相比,可能导致性能下降。

On the issue of the 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.)

关于JIT的问题。即时编译将特定于JVM实现,因此在将字节码转换为本机代码时找出字节码实际发生的情况并不容易,而且,我们无法分辨哪些部分正在被JIT(至少没有一些特定于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. There may be some performance tricks that one JVM performs while another doesn't.

也就是说,无论如何,JVM将执行字节码,它的执行方式对开发人员来说或多或少是不透明的,同样也是JVM特有的。一个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源代码。

#2


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编译器将做很多工作,并且你不会对它正在做什么有太多了解。

#3


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. Yes, Java, that's it :-)

从抽象的角度思考。在计算行星轨道时,你不需要知道夸克或原子的作用。要成为一名优秀的Java程序员,你应该学习......嗯.. Java。是的,Java,就是这样:-)

#4


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.)

字节码需要适度的学习曲线才能理解。当然,学到更多东西永远不会伤害,但实用主义建议在必要时将其推迟。 (如果那一刻到来,我建议找人来指导你。)