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

Optimal initial capacity of java.lang.Class.enumConstantDirectory

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P5
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 11
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b08
    • Verification:
      Verified

      Description

      The optimal initial capacity of a HashMap depends on the expected number of elements in the map and the load factor.
      From the documentation:
      https://docs.oracle.com/javase/10/docs/api/java/util/HashMap.html
      "If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur."

      java.lang.Class.enumConstantDirectory is currently created as
      new HashMap<>(2 * universe.length);
      which is not optimal for some values of universe.length.

      For example, when an enum has 5 elements, the underlying storage created is of size 16, while size 8 would be enough (8 * 0.75 == 6 > 5).

      To achieve optimal initial capacity (i.e. such that will be not too low to avoid reallocation and not too high to avoid wasting memory) the formula initialCapacity = (expectedSize / 0.75 + 1) can be used.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              igerasim Ivan Gerasimov
              Reporter:
              igerasim Ivan Gerasimov
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: