Java 8 requireNonNull 方法与供应商参数性能
Java 8 requireNonNull method with supplier parameter performance
在 Java 8 中添加了 Supplier
的方法 Objects.requireNonNull,但我不确定声明的性能改进是什么:
While this may confer a performance advantage in the non-null case, when deciding to call this method care should be taken that the costs of creating the message supplier are less than the cost of just creating the string message directly.
如果参数不为空,则使用字符串的方法将忽略参数:
public static <T> T requireNonNull(T obj, String message) {
if (obj == null)
throw new NullPointerException(message);
return obj;
}
我找到了JDK-8011800 : Add java.util.Objects.requireNonNull(T, Supplier)
In JDK 7, java.util.Objects included several methods to check for null, including one that took a message to return if a null was found. With lambdas in JDK 8, another variant to include is a requireNonNull method which takes a string supplier instead of a string. That why the cost of creating the string message can be avoided for the non-null case. Note that the lambda capture can have a nonzero cost though.
带有注释表示没有性能影响:
The non-zero capture cost does worry me. I am concerned that it will frequently erase any advantage of using a Supplier.
09-04-2013
我发现了其他问题,但没有提到(为什么)发送String参数有性能成本
它是否特定于 lambda expressions/stream 用法?
考虑一下,generateString
做了很多事情以便从 someParam
:
生成一个字符串
Objects.requireNonNull(obj, generateString(someParam));
参数在 Java 中急切求值,这意味着 generateString
将在调用 requireNonNull
之前求值。因此,无论 obj
是否为 null,都会计算它。
您可以通过将其更改为以下内容来解决此问题:
Objects.requireNonNull(obj, () -> generateString(someParam));
在这种情况下,只有当 obj
实际上为 null 时才会调用 generateString
。当 generateString
比创建 Supplier
对象更昂贵时,这更有效。
如果您的字符串参数只是文字,您应该只使用普通的非 lambda 方法,例如:
Objects.requireNonNull(obj, "obj was null!");
在 Java 8 中添加了 Supplier
的方法 Objects.requireNonNull,但我不确定声明的性能改进是什么:
While this may confer a performance advantage in the non-null case, when deciding to call this method care should be taken that the costs of creating the message supplier are less than the cost of just creating the string message directly.
如果参数不为空,则使用字符串的方法将忽略参数:
public static <T> T requireNonNull(T obj, String message) {
if (obj == null)
throw new NullPointerException(message);
return obj;
}
我找到了JDK-8011800 : Add java.util.Objects.requireNonNull(T, Supplier)
In JDK 7, java.util.Objects included several methods to check for null, including one that took a message to return if a null was found. With lambdas in JDK 8, another variant to include is a requireNonNull method which takes a string supplier instead of a string. That why the cost of creating the string message can be avoided for the non-null case. Note that the lambda capture can have a nonzero cost though.
带有注释表示没有性能影响:
The non-zero capture cost does worry me. I am concerned that it will frequently erase any advantage of using a Supplier. 09-04-2013
我发现了其他问题,但没有提到(为什么)发送String参数有性能成本
它是否特定于 lambda expressions/stream 用法?
考虑一下,generateString
做了很多事情以便从 someParam
:
Objects.requireNonNull(obj, generateString(someParam));
参数在 Java 中急切求值,这意味着 generateString
将在调用 requireNonNull
之前求值。因此,无论 obj
是否为 null,都会计算它。
您可以通过将其更改为以下内容来解决此问题:
Objects.requireNonNull(obj, () -> generateString(someParam));
在这种情况下,只有当 obj
实际上为 null 时才会调用 generateString
。当 generateString
比创建 Supplier
对象更昂贵时,这更有效。
如果您的字符串参数只是文字,您应该只使用普通的非 lambda 方法,例如:
Objects.requireNonNull(obj, "obj was null!");