Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8225339

Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 8, 11, 13, 14
    • Fix Version/s: 14
    • Component/s: core-libs
    • Labels:
      None

      Description

      Currently HashMap.keySet().toArray(), HashMap.values().toArray(), HashSet.toArray(), LinkedHashMap.keySet().toArray(), LinkedHashMap.values().toArray(), LinkedHashSet.toArray() delegate to AbstractCollection implementation which creates an iterator. This is suboptimal as the corresponding iterator (e.g. HashMap.KeyIterator) should track current state which is quite non-trivial for HashMap. Also modCount is constantly compared with expectedModCount which is also redundant. It seems that existing JIT compilers (C2, Graal) cannot totally eliminate the overhead. I believe that these methods are used quite often (especially HashSet.toArray()) to deserve a specialized implementation similar to LinkedList.toArray().

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              tvaleev Tagir Valeev
              Reporter:
              tvaleev Tagir Valeev
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: