RFR: JDK-8302666: Replace CHM with VarHandle in ForeachOrderedTask

Viktor Klang duke at openjdk.org
Thu Feb 16 15:19:28 UTC 2023


On Thu, 16 Feb 2023 15:01:34 GMT, Per Minborg <pminborg at openjdk.org> wrote:

>> I noticed when looking at the code that there was no real need to use a CHM to perform the tracking of activation in an ordered fashion on ForEachOrderedTask, but instead a VarHandle can be used, reducing allocations and indirection.
>
> src/java.base/share/classes/java/util/stream/ForEachOps.java line 372:
> 
>> 370:         private Node<T> node;
>> 371: 
>> 372:         @SuppressWarnings("unused") private volatile ForEachOrderedTask<S, T> next; // Only accessed through the NEXT VarHandle
> 
> I think the variable can be declared without `volatile` as the operations on the `VarHandle` will determine the memory semantics used regardless? So, the `NEXT.set()` at L431 might also be replaced with `NEXT.setVolatile()`

@minborg Good point. Reference here:

"Access modes will override any memory ordering effects specified at the declaration site of a variable. For example, a VarHandle accessing a field using the get access mode will access the field as specified by its access mode even if that field is declared volatile. When mixed access is performed extreme care should be taken since the Java Memory Model may permit surprising results. " - https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/invoke/VarHandle.html

@minborg To clarify, I intended to do a plain write since safe publishing will be done by `fork()`

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

PR: https://git.openjdk.org/jdk/pull/12320


More information about the core-libs-dev mailing list