ConcurrentHashMap - Removal for unnecessary bounds check i >= n || i + n >= nextn from transfer()

Sreyan Chakravarty sreyan32 at gmail.com
Mon Aug 21 13:43:48 UTC 2023


Hi,

I am trying to get my head around to why you need

 i >= n || i + n >= nextn

https://github.com/openjdk/jdk/blob/af5bf81754072fa5879726cfacb7404892b553f0/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L2465


in the transfer method of the CHM.

https://github.com/openjdk/jdk/blob/af5bf81754072fa5879726cfacb7404892b553f0/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L2426

The reasons why I think that this is not required is because :

1) At a time there can be only 1 active transfer. Since there is only one
table variable and only one newTable variable.

2) transferIndex is always set to oldTable.length before a transfer() takes
place

https://github.com/openjdk/jdk/blob/af5bf81754072fa5879726cfacb7404892b553f0/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L2440

3) transferIndex is always set to nextIndex - stride, nextIndex is always
set to transferIndex which in turn is always set to n (oldTable.length)

https://github.com/openjdk/jdk/blob/af5bf81754072fa5879726cfacb7404892b553f0/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L2456

https://github.com/openjdk/jdk/blob/af5bf81754072fa5879726cfacb7404892b553f0/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L2452

4) All threads that modifies the CHM must help with the transfer() before
finalizing the newTable

https://github.com/openjdk/jdk/blob/af5bf81754072fa5879726cfacb7404892b553f0/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L2467

Please let me know if I am going wrong anywhere

or if there are any code paths which can make i >=n when advance = false

Any help would be great.
-- 
Regards,
Sreyan Chakravarty
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20230821/45754fd3/attachment-0001.htm>


More information about the core-libs-dev mailing list