On Wed, 5 Oct 2022 13:30:08 GMT, Viktor Klang <[email protected]> wrote:
>> PR for Sequenced Collections implementation.
>
> src/java.base/share/classes/java/util/LinkedHashMap.java line 384:
>
>> 382: return this.put(k, v);
>> 383: } finally {
>> 384: putMode = PUT_NORM;
>
> @stuart-marks Would it be an alternative to have an `internalPut(mode, k,
> v)` so there is no need to have an internal variable which needs to be
> read/written multiple time per operation? 🤔
Yeah, the coupling here is rather distasteful. (Otherwise known as a quick and
dirty hack.) Unfortunately the coupling between HashMap and LinkedHashMap is
pretty special-purposed for exactly the intended usage modes (insertion-order
and access-order). It could be improved, but it would probably require some
refactoring in HashMap, which I didn't want to do right now, in order to keep
the sequenced stuff separate.
> src/java.base/share/classes/java/util/SequencedCollection.java line 155:
>
>> 153: */
>> 154: default E getLast() {
>> 155: return this.reversed().iterator().next();
>
> @stuart-marks Are these default implementation expected to be used (actually)
> in the JDK? From a performance PoV, it might make sense to not have default
> implementations unless strictly needed, and instead keep the code in the
> JavaDoc as a guideline for "worst-case" performance profile. 🤔
Good question. They might not actually be used in the JDK... but it isn't
obvious! In fact some of these methods are used in the `sequencedValues()` view
of `SequencedMap`. They could be used if there is a `SortedMap` implementation
that doesn't implement `NavigableMap`. I don't think there are any of these in
the JDK, but there are "in the wild." Yes, there are potentially performance
issues with these, but I think it's valuable to have a complete set of default
implementations available.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/7387#discussion_r1012436476
PR Review Comment: https://git.openjdk.org/jdk/pull/7387#discussion_r1012418175