拳击会导致性能问题吗? [英] Does boxing cause performance issues?

查看:92
本文介绍了拳击会导致性能问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,我们正在其中开发一种可编译为Java的语言.我们正在使用的框架(xtext)在其生成的代码中大量使用了boxing. 具体来说,如果您有如下声明:

I'm working on a project in which we are producing a language which compiles to java. The framework we are using (xtext) makes prolific use of boxing in its generated code. Specifically, if you have a statement like:

int i = 1;
int j = 2;
int k = i + j;

然后,编译后的代码如下:

Then the compiled code looks like:

IntegerExtensions.operator_plus(((Integer)i), ((Integer)j))

现在,在我正在研究的项目中,在某些情况下,特定的基本二进制运算将变得非常普遍(尤其是增量和比较).

Now, in the project I'm working on, there are certain situations where particular basic binary operations are going to be extremely common (especially increments and comparisons).

我的问题是:就性能而言,这将是一个问题,还是JIT(或类似的智能JVM功能)会简单地了解正在发生的事情并修复所有问题?

My question is: is this going to be a problem in terms of performance, or will JIT (or similarly intelligent JVM features) simply realize what's going on and fix it all?

请在发布之前先阅读以下内容:我对收到答复说您应该在乎,使其可读"不感兴趣.这段代码是生成的,我根本不在乎生成的代码的可读性.我关心的是,我们不会因此受到重大的性能影响.

PLEASE READ BEFORE POSTING: I'm not interested in getting responses saying "you shouldn't care, make it readable". This code is generated, and I simply don't care about the readability of the generated code. What I do care about is that we don't take a significant performance hit from this.

谢谢

推荐答案

这实际上可能会产生影响.当强制转换为Integer时,它将使用Integer.valueOf(int n)方法将int转换为Integer.此方法将检查该值是否在缓存范围内(-128至127),如果不是,则会创建new Integer(n)

This can in fact have an impact. When the cast to Integer occurs it will convert the int to Integer using Integer.valueOf(int n) method. This method will check to see if the value is within the cache range (-128 to 127) and if it is not it will create new Integer(n)

影响的大小可能很多,您必须进行自我测试.

The amount of an impact may be a lot or little, you would have to test yourself.

这篇关于拳击会导致性能问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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