RFR: 8322149: ConcurrentHashMap smarter presizing for copy constructor and putAll [v3]

Joshua Cao duke at openjdk.org
Fri Jan 19 18:41:29 UTC 2024


On Thu, 18 Jan 2024 07:17:20 GMT, ExE Boss <duke at openjdk.org> wrote:

>> src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java line 1088:
>> 
>>> 1086:     public void putAll(Map<? extends K, ? extends V> m) {
>>> 1087:         if (table != null) {
>>> 1088:             tryPresize(size() + m.size());
>> 
>> Is overflow not an issue here because calling tryPresize with a negative value will invoke tableSizeFor?
>
> When `tableSizeFor` is called with a negative value greater than `Integer.MIN_VALUE`, it’ll just return `+1`:
> https://github.com/openjdk/jdk/blob/ff8cc268fdaaf85299c94088a226b73e7eaf6bdb/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L704-L707
> 
> This will in turn cause `tryPresize` to do nothing when `table` is already initialized.

In the worse case with overflow, we may not presize when we would have liked. I'd like to say its an edge case we don't have to cover. The sum of map sizes would have to be 2^31. Not sure if maps ever get this big in practice.

If its a concern, we could do `max(this.size() + m.size(), m.size())`. I feel its unnecessary.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/17116#discussion_r1459529675


More information about the core-libs-dev mailing list