如何“重复”? Java代码是由JVM JIT编译器优化的吗?

发布于 2024-09-06 02:31:11 字数 333 浏览 6 评论 0原文

我负责维护一个基于 JSP 的应用程序,该应用程序在 IBM WebSphere 6.1 (IBM J9 JVM) 上运行。所有 JSP 页面都有一个静态包含引用,并且在此包含文件中声明了一些静态 Java 方法。它们包含在所有 JSP 页面中,以提供对这些实用程序静态方法的“轻松访问”。我知道这是一种非常糟糕的工作方式,我正在努力改变这种情况。但是,出于好奇,为了支持我改变这一点的努力,我想知道 JVM JIT 编译器如何优化这些“重复的”静态方法。

  • 即使具有完全相同的签名,它们也是单独优化的吗?
  • JVM JIT 编译器是否“看到”这些方法都是相同的并提供“统一”的 JIT 代码?

I'm in charge of maintaining a JSP based application, running on IBM WebSphere 6.1 (IBM J9 JVM). All JSP pages have a static include reference and in this include file there is some static Java methods declared. They are included in all JSP pages to offer an "easy access" to those utility static methods. I know that this is a very bad way to work, and I'm working to change this. But, just for curiosity, and to support my effort in changing this, I'm wondering how these "duplicated" static methods are optimized by the JVM JIT compiler.

  • They are optimized separately even having the exact same signature?
  • Does the JVM JIT compiler "sees" that these methods are all identical an provides an "unified" JIT'ed code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自此以后,行同陌路 2024-09-13 02:31:11

每个 JSP 页面都被编译为一个唯一的类,因此包含的代码也将被编译为不同的类。 JIT 不会将代码的各个副本合并为一个。

为了避免这种情况,您可以将导入的代码放入真正的 Java 类中,并将其导入到 JSP 中。这样就不会出现重复,因为您正在重用同一个类。

Each JSP page is compiled to a unique class, and so the included code will also be compiled into distinct classes. The JIT will not consolidate the various copies of the code into one.

To avoid this, you can put the imported code into a real Java class, and import that in the JSP. Then there will be no duplicates, since you are reusing the same class.

悟红尘 2024-09-13 02:31:11

@mdma 的答案对于当前的 JVM 来说是正确的,但需要在几个方面进行限定。

  1. 未来 JVM 中的 JIT 可能支持积极优化,以减少本机代码的内存占用。

  2. 每个 JSP 类的一些静态方法的开销很可能不会对您的 Web 应用程序的内存占用产生很大影响。

@mdma's answer is correct for current JVMs, but needs to be qualified in a couple of respects.

  1. The JITs in future JVMs could possibly support aggressive optimizations for reducing the memory footprint of the native code.

  2. The flipside is that unless you have thousands of distinct JSPs, the chances are that the overheads of a few static methods per JSP class won't make a lot of difference to your webapp's memory footprint.

捶死心动 2024-09-13 02:31:11

您可以使用来自单个类的静态导入:<%@ page import="static foo.*" %>。

然后你就不再有所有的重复。除了导入之外,您不需要接触任何其他内容。

You can use static imports from a single class : <%@ page import="static foo.*" %>.

Then you no longer have all that duplication. And other than the import you would not need to touch aything else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文