Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么java.util.ArrayList中有OutofBoundsMSSG私有方法?_Java_Arraylist_Exception Handling_Guava_Jvm Hotspot - Fatal编程技术网

为什么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
  • “此大纲显示的效果最好…”的含义
  • 我应该开始重构代码,为异常构造函数包含字符串返回方法吗
“此大纲显示的效果最好…”的含义

它与内联相反,但不是一个标准术语,这就是它被引用的原因

为什么需要专用OutofBoundsMSSG

这就是“大纲”将代码提取到一个单独的方法的意义

我应该开始重构代码,为异常构造函数包含字符串返回方法吗


如果您关心每次抛出没有消息字符串文本的异常时浪费的3纳秒,那么是的。换句话说:没有。

OutofBoundsMSSG
不是“必需的”,Java开发人员(显然还有Google Collections)发现这对他们的库来说是一个足够的性能改进(可能是在仔细测试之后)。这也是一种优化,可能不适用于所有Java版本和实现。