为什么java.util.ArrayList中有OutofBoundsMSSG私有方法?
下面是来自为什么java.util.ArrayList中有OutofBoundsMSSG私有方法?,java,arraylist,exception-handling,guava,jvm-hotspot,Java,Arraylist,Exception Handling,Guava,Jvm Hotspot,下面是来自java.util.ArrayList的片段: /** * Constructs an IndexOutOfBoundsException detail message. * Of the many possible refactorings of the error handling code, * this "outlining" performs best with both server and client VMs. */ private String outOfBo
java.util.ArrayList
的片段:
/**
* Constructs an IndexOutOfBoundsException detail message.
* Of the many possible refactorings of the error handling code,
* this "outlining" performs best with both server and client VMs.
*/
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}
以下是com.google.collect.premissions
中的片段:
/*
* All recent hotspots (as of 2009) *really* like to have the natural code
*
* if (guardExpression) {
* throw new BadException(messageExpression);
* }
*
* refactored so that messageExpression is moved to a separate
* String-returning method.
*
* if (guardExpression) {
* throw new BadException(badMsg(...));
* }
*
* The alternative natural refactorings into void or Exception-returning
* methods are much slower. This is a big deal - we're talking factors of
* 2-8 in microbenchmarks, not just 10-20%. (This is a hotspot optimizer
* bug, which should be fixed, but that's a separate, big project).
*
* The coding pattern above is heavily used in java.util, e.g. in ArrayList.
* There is a RangeCheckMicroBenchmark in the JDK that was used to test this.
请有人解释一下:
- 为什么需要私密的
OutofboundsMSSG
- “此大纲显示的效果最好…”的含义
- 我应该开始重构代码,为异常构造函数包含字符串返回方法吗
如果您关心每次抛出没有消息字符串文本的异常时浪费的3纳秒,那么是的。换句话说:没有。
OutofBoundsMSSG
不是“必需的”,Java开发人员(显然还有Google Collections)发现这对他们的库来说是一个足够的性能改进(可能是在仔细测试之后)。这也是一种优化,可能不适用于所有Java版本和实现。