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

Optimal initial capacity of java.lang.Class.enumConstantDirectory

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P5 P5
    • 11
    • None
    • core-libs
    • b08
    • Verified

      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.

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

              Created:
              Updated:
              Resolved: