RFR: 8291065: Creating a VarHandle for a static field triggers class initialization [v8]
ExE Boss
duke at openjdk.org
Sun Jun 4 16:16:10 UTC 2023
On Sun, 4 Jun 2023 13:23:46 GMT, Chen Liang <liach at openjdk.org> wrote:
>> Also fixed the bug with NPE in `IndirectVarHandle::isAccessModeSupported`.
>>
>> A few implementation-detail methods in VarHandle are now documented with the implied constraints to avoid subtle problems in the future. Changed `IndirectVarHandle` to call `asDirect` lazily to accomodate the lazy VarHandle changes. Also changed VarHandleBaseTest to report the whole incorrect type of exception caught than swallow it and leaving only a message.
>>
>> Current problems:
>> - [ ] The lazy var handle is quite slow on the first invocation.
>> - As seen in the benchmark, users can first call `Lookup::ensureInitialized` to create an eager handle.
>> - After that, the lazy handle has a performance on par with the regular var handle.
>> - [ ] The class-loading-based test is not in a unit test
>> - The test frameworks don't seem to offer fine-grained control for class-loading detection or reliable unloading
>>
>>
>> Benchmark Mode Cnt Score Error Units
>> VarHandleLazyStaticInvocation.initializedInvocation avgt 30 0.817 ± 0.012 ns/op
>> VarHandleLazyStaticInvocation.lazyInvocation avgt 30 0.805 ± 0.007 ns/op
>>
>>
>> Benchmark Mode Cnt Score Error Units
>> LazyStaticColdStart.methodHandleCreateEager ss 10 50.980 ± 9.454 us/op
>> LazyStaticColdStart.methodHandleCreateLazy ss 10 24.350 ± 6.701 us/op
>> LazyStaticColdStart.methodHandleInitializeCallEager ss 10 65.140 ± 7.552 us/op
>> LazyStaticColdStart.methodHandleInitializeCallLazy ss 10 118.360 ± 20.320 us/op
>> LazyStaticColdStart.varHandleCreateEager ss 10 49.500 ± 4.277 us/op
>> LazyStaticColdStart.varHandleCreateLazy ss 10 26.690 ± 5.157 us/op
>> LazyStaticColdStart.varHandleInitializeCallEager ss 10 87.930 ± 12.643 us/op
>> LazyStaticColdStart.varHandleInitializeCallLazy ss 10 1057.120 ± 189.810 us/op
>
> Chen Liang has updated the pull request incrementally with one additional commit since the last revision:
>
> Improve subsequent VH call performance, thanks exeboss
src/java.base/share/classes/java/lang/invoke/LazyInitializingVarHandle.java line 125:
> 123: cache[i] = callTarget;
> 124: }
> 125: }
Unlike calling `Unsafe::ensureClassInitialized(…)` (which performs its own synchronisation) or writing `true` to `this.initialized` (which is a benign race), updating the lambda forms this way is probably not quite correct to do under a race and probably needs some synchronisation.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/13821#discussion_r1216862767
More information about the core-libs-dev
mailing list