RFR: 8324646: Avoid Class.forName in SecureRandom constructor [v3]
Oli Gillespie
ogillespie at openjdk.org
Tue Feb 6 13:42:54 UTC 2024
On Thu, 1 Feb 2024 10:39:27 GMT, Oli Gillespie <ogillespie at openjdk.org> wrote:
>> Avoid expensive `Class.forName` call when constructing Providers such as `SecureRandom` which take constructor parameters. This can easily be cached in EngineDescription (this cache already existed before, it was removed in [JDK-8280970](https://bugs.openjdk.org/browse/JDK-8280970) as unused, I'm bringing it back unchanged to support this new usage).
>>
>> Benchmark results on my Linux x86 host show around a 20% reduction in time to create a new `SecureRandom` instance. Most of the remaining overhead is due to a failing constructor lookup - see [JDK-8324648](https://bugs.openjdk.org/browse/JDK-8324648).
>>
>>
>> Before
>> newSecureRandom avgt 2930 ± 50 ns/op
>>
>> After
>> newSecureRandom avgt 2400 ± 33 ns/op
>>
>>
>> I have seen multiple real-world applications which call `new SecureRandom()` on the hot path, so I believe efficiency here is important.
>
> Oli Gillespie has updated the pull request incrementally with one additional commit since the last revision:
>
> Use class literals instead of forName
Thank you :) No change in benchmark score, or perhaps slight improvement. There seem to be two modes, one at 2400ns/op (same as original change) and one around 2200ns/op.
# Fork: 1 of 3
# Warmup Iteration 1: 3552.303 ns/op
# Warmup Iteration 2: 2906.823 ns/op
# Warmup Iteration 3: 2404.065 ns/op
# Warmup Iteration 4: 2396.532 ns/op
# Warmup Iteration 5: 2416.137 ns/op
Iteration 1: 2418.132 ns/op
Iteration 2: 2410.748 ns/op
Iteration 3: 2396.958 ns/op
Iteration 4: 2410.428 ns/op
Iteration 5: 2403.168 ns/op
# Fork: 2 of 3
# Warmup Iteration 1: 3156.931 ns/op
# Warmup Iteration 2: 2606.873 ns/op
# Warmup Iteration 3: 2175.693 ns/op
# Warmup Iteration 4: 2250.570 ns/op
# Warmup Iteration 5: 2152.489 ns/op
Iteration 1: 2190.243 ns/op
Iteration 2: 2153.819 ns/op
Iteration 3: 2175.183 ns/op
Iteration 4: 2179.866 ns/op
Iteration 5: 2150.761 ns/op
# Fork: 3 of 3
# Warmup Iteration 1: 3489.428 ns/op
# Warmup Iteration 2: 2851.548 ns/op
# Warmup Iteration 3: 2410.677 ns/op
# Warmup Iteration 4: 2427.988 ns/op
# Warmup Iteration 5: 2414.204 ns/op
Iteration 1: 2412.798 ns/op
Iteration 2: 2417.093 ns/op
Iteration 3: 2414.075 ns/op
Iteration 4: 2442.164 ns/op
Iteration 5: 2410.162 ns/op
-------------
PR Comment: https://git.openjdk.org/jdk/pull/17559#issuecomment-1929648313
More information about the security-dev
mailing list