[jdk17u-dev] RFR: 8324646: Avoid Class.forName in SecureRandom constructor

Aleksey Shipilev shade at openjdk.org
Wed Mar 20 07:59:25 UTC 2024


On Tue, 19 Mar 2024 18:42:15 GMT, Oli Gillespie <ogillespie at openjdk.org> wrote:

> Improve performance of SecureRandom constructor by backporting (both clean) two changes:
> 
> 1. [JDK-8280970](https://bugs.openjdk.org/browse/JDK-8280970) removes some unused code in Provider.java. This is not really functionally needed, but it changes the same areas of code and makes the actual performance fix apply cleanly.
> 2. [JDK-8324646](https://bugs.openjdk.org/browse/JDK-8324646) is the actual performance fix - avoiding Class.forName calls in every construction.
> 
> Note - because of the unused code still present in JDK17, there is actually an alternative fix which makes use of it, which is very simple:
> 
> 
> diff --git a/src/java.base/share/classes/java/security/Provider.java b/src/java.base/share/classes/java/security/Provider.java
> index af8ebeeda57..28bf642d0c8 100644
> --- a/src/java.base/share/classes/java/security/Provider.java
> +++ b/src/java.base/share/classes/java/security/Provider.java
> @@ -1851,7 +1851,7 @@ public abstract class Provider extends Properties {
>                          null : constructorParameter.getClass();
>                  } else {
>                      ctrParamClz = cap.constructorParameterClassName == null?
> -                        null : Class.forName(cap.constructorParameterClassName);
> +                        null : cap.getConstructorParameterClass(); // actually make use of the cached class!
>                      if (constructorParameter != null) {
>                          if (ctrParamClz == null) {
>                              throw new InvalidParameterException
> 
> 
> This has the same performance benefits as making the two backports. But, it means 17 will be diverged from later versions, and I think the backported fix is cleaner overall.
> 
> Benchmark results (`make test TEST=micro:org.openjdk.bench.java.security.SecureRandomBench`):
> 
> 
> Before: 2614 ± 127  ns/op
>  After: 2150 ± 116  ns/op

Marked as reviewed by shade (Reviewer).

-------------

PR Review: https://git.openjdk.org/jdk17u-dev/pull/2310#pullrequestreview-1948166326


More information about the jdk-updates-dev mailing list