RFR: 8359936: StableValues can release the underlying function after complete computation [v2]

Per Minborg pminborg at openjdk.org
Thu Jun 19 09:11:40 UTC 2025


On Thu, 19 Jun 2025 08:57:44 GMT, Viktor Klang <vklang at openjdk.org> wrote:

>> Per Minborg has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   Fix performance regression
>
> src/java.base/share/classes/jdk/internal/lang/stable/StableValueImpl.java line 150:
> 
>> 148:                 // The mutex is not reentrant so we know newValue should be returned
>> 149:                 wrapAndSet(newValue);
>> 150:                 if (underlyingHolder != null) {
> 
> Under what circumstances can the underlyingHolder be null here?

If we call this method via the public `StableValue::orElseSet`, the underlying holder will be `null`. In this case, there is no underlying function stored. Instead, it is typically a lambda or an anonymous class provided on the fly.

> src/java.base/share/classes/jdk/internal/lang/stable/UnderlyingHolder.java line 35:
> 
>> 33:         this.counter = counter;
>> 34:         // Safe publication
>> 35:         UNSAFE.storeStoreFence();
> 
> This may actually be substitutable by making the initial write to counter a volatile write:
> 
> Suggestion:
> 
>         Unsafe.putIntVolatile(this, COUNTER_OFFSET, counter); // Safe publication of underlying and counter

True. We could also use piggybacking.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/25878#discussion_r2156518370
PR Review Comment: https://git.openjdk.org/jdk/pull/25878#discussion_r2156520221


More information about the core-libs-dev mailing list