On Wed, 15 Jun 2022 21:30:24 GMT, Maurizio Cimadamore <[email protected]>
wrote:
>> Avoid doing MH creation when initializing `StringConcatFactory` by lazily
>> initializing to a `@Stable` field instead.
>
> src/java.base/share/classes/java/lang/invoke/StringConcatFactory.java line
> 729:
>
>> 727: MethodHandle base = PREPEND_BASE;
>> 728: if (base == null) {
>> 729: base = PREPEND_BASE = MethodHandles.dropArguments(
>
> I'm curious about this idiom - couldn't this result in the `PREPEND_BASE`
> field being assigned twice, with objects that are morally identical, but
> could have different identities? (thus violating `@Stable` contract)
It's not a violation, it's relying on behavior not discussed by the `@Stable`
doc, which refers to such behavior as "undefined".
It's a carefully correlated dance between the code, interpreter, and C2 relying
on:
1. safe publication of equivalent immutable structures with no dependence on
their identity when operating on them;
2. the field being set in the interpreter; and
3. by the time C2 inlines `prependBase` the field is no longer be updated, thus
C2 can replace the field access with its value.
-------------
PR: https://git.openjdk.org/jdk/pull/9154