[jdk17u-dev] Integrated: 8324646: Avoid Class.forName in SecureRandom constructor
Oli Gillespie
ogillespie at openjdk.org
Mon Jun 3 14:31:20 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
This pull request has now been integrated.
Changeset: 1ce0c635
Author: Oli Gillespie <ogillespie at openjdk.org>
Committer: Aleksey Shipilev <shade at openjdk.org>
URL: https://git.openjdk.org/jdk17u-dev/commit/1ce0c63577a9480f162e762fd8a1db344955d5a7
Stats: 70 lines in 2 files changed: 47 ins; 10 del; 13 mod
8324646: Avoid Class.forName in SecureRandom constructor
8280970: Cleanup dead code in java.security.Provider
Reviewed-by: phh, shade
Backport-of: 8ef918d6678437a5b351b172bb4cf144eeaa975f
-------------
PR: https://git.openjdk.org/jdk17u-dev/pull/2310
More information about the jdk-updates-dev
mailing list