RFR: 8291065: Creating a VarHandle for a static field triggers class initialization [v7]
Chen Liang
liach at openjdk.org
Sun Jun 4 13:23:46 UTC 2023
On Sat, 3 Jun 2023 03:47:40 GMT, Chen Liang <liach at openjdk.org> wrote:
>> src/java.base/share/classes/java/lang/invoke/LazyInitializingVarHandle.java line 106:
>>
>>> 104: UNSAFE.ensureClassInitialized(refc);
>>> 105: this.initialized = true;
>>> 106: }
>>
>> This should probably have a fast‑path when `this.initialized` is `true`, so that it can be better constant folded by the **JIT**:
>> Suggestion:
>>
>> private void ensureInitialized() {
>> if (this.initialized) {
>> return;
>> }
>> UNSAFE.ensureClassInitialized(refc);
>> this.initialized = true;
>> }
>
> I think I should probably update the MH lambda form as well, to skip the ensure call entirely if JVM has not constant-fold the inefficient form yet.
Indeed, updating lambda form and cached mh works: the average invocation performance becomes ~0.8ns/op, equal to the direct version, compared to the original ~1ms/op. See the updated benchmark results in the main PR body.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/13821#discussion_r1216694641
More information about the core-libs-dev
mailing list