RFR: 8253179: Replace LinkedList Impl in net.http.Http2Connection

Daniel Fuchs dfuchs at openjdk.java.net
Thu Oct 1 09:46:10 UTC 2020


On Thu, 1 Oct 2020 08:22:35 GMT, Aleksey Shipilev <shade at openjdk.org> wrote:

>> This patch replaces a LinkedList data structure used in the net.http.Http2Connection class with an ArrayList. This
>> issue relates to [JDK-8246048: Replace LinkedList with ArrayLists in
>> java.net](https://bugs.openjdk.java.net/browse/JDK-8246048).  Some justifications for this change are as follows:
>> 
>> - Sequential Access Times for ArrayLists are improved due to locality of reference (i.e ArrayList elements stored in same
>>   memory neighborhood)
>> - Get(index) operations are O(1) time complexity for ArrayLists as opposed to worst-case O(N-1) for LinkedLists
>> - While insertion operations can be expensive (O(N) in the worst case), these operations appear to be
>>   infrequent/non-existent in this case.
>> 
>> Additional justifications or challenges to those listed are welcome! The general idea is that ArrayLists out-perform
>> LinkedLists in this scenario.
>
> src/java.net.http/share/classes/jdk/internal/net/http/Http2Connection.java line 703:
> 
>> 701:         if (initialCause == null) this.cause = t;
>> 702:         client2.deleteConnection(this);
>> 703:         List<Stream<?>> c = new ArrayList<>(streams.values());
> 
> Why can't we dispense with this copy completely, and instead just iterate `streams.values()`?

Good point Aleksey. I guess that the original intent was to avoid `ConcurrentModificationException`. However I see that
`streams` is a `ConcurrentHashMap` now. So maybe we could dispense of the copy:

`    private final Map<Integer,Stream<?>> streams = new ConcurrentHashMap<>();`

Could be worth changing the type from `Map<Integer,Stream<?>>` to `ConcurrentMap<Integer,Stream<?>>` in the declaration
above if we decide to get rid of the copy though.

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

PR: https://git.openjdk.java.net/jdk/pull/431


More information about the net-dev mailing list