RFR: 8372410: ThreadFactory used by Thread.Builder::factory uses non-optimal VarHandle
Jorn Vernee
jvernee at openjdk.org
Mon Nov 24 19:26:14 UTC 2025
On Mon, 24 Nov 2025 17:17:37 GMT, Chen Liang <liach at openjdk.org> wrote:
>> This is boosting VirtualThread allocations while using https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Thread.Builder.OfVirtual.html#name(java.lang.String,long)
>>
>> The pattern has been discovered on https://github.com/quarkusio/quarkus/issues/51201 and narrowed down in https://github.com/franz1981/java-puzzles/commit/5cb4216e8d56cdc6c3c23629419158550cbd9068
>>
>> which, on my machine (Ryzen 7950 w fixed frequency at 4.2 GHz):
>>
>> Benchmark Mode Cnt Score Error Units
>> VarHandlePolymorphicSignatureSensitivity.rightVarHandle avgt 10 1.852 ± 0.007 ns/op
>> VarHandlePolymorphicSignatureSensitivity.rightVarHandleConcatIndy avgt 10 9.891 ± 0.103 ns/op
>> VarHandlePolymorphicSignatureSensitivity.wrongVarHandle avgt 10 10.430 ± 0.045 ns/op
>> VarHandlePolymorphicSignatureSensitivity.wrongVarHandleConcatIndy avgt 10 19.312 ± 0.619 ns/op
>>
>> where the `right` indy shows:
>> <img width="1915" height="469" alt="image" src="https://github.com/user-attachments/assets/4c836389-492b-4cc6-94a3-849f8cf6ea09" />
>>
>> vs the `wrong` one:
>> <img width="1915" height="469" alt="image" src="https://github.com/user-attachments/assets/73765060-8e95-42a2-a03a-4f53f927a8b8" />
>
> This is not the first time this VarHandle problem is hit - see #16451
> This has bitten me lot of times @liach 😢 Including others while reading/writing oop fields into arrays too; it would be nice to get an option to fail fast with an exception enabled via diagnostic args eg EnableStrictPolymorphicSignature Although Javac should be able to detect it too, thinking about it twice
This can be done using [`VarHandle::withInvokeExactBehavior`](https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/invoke/VarHandle.html#withInvokeExactBehavior()) on a per var handle basis. C2 can constant fold the type-check away as well.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/28475#issuecomment-3572364123
More information about the core-libs-dev
mailing list