这是来自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.Preconditions的片段
/*
* 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.希望有人能阐明:
outOfBoundsMsg发布于 2014-03-04 14:32:35
意思是“这个大纲表现最好.”
这是相反的内联,但不是一个标准的术语,这就是为什么它是常见的。
为什么需要私有outOfBoundsMsg
这就是“大纲”的意思--将代码提取到单独的方法中。
是否应该开始重构代码以包含异常构造函数的字符串返回方法?
如果您关心每次抛出没有字符串文字的异常时浪费3纳秒时间,那么是的。换句话说:没有。
https://stackoverflow.com/questions/22174682
复制相似问题