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

Optimal initial capacity of java.lang.VarHandle.AccessMode.methodNameToAccessMode

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P5 P5
    • 11
    • None
    • core-libs
    • b09
    • 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."

      Currently, the map is created as
                  // Initial capacity of # values is sufficient to avoid resizes
                  // for the smallest table size (32)
                  methodNameToAccessMode = new HashMap<>(AccessMode.values().length);

      Even though the comment suggests that no resizes of the table should occur, in fact the threshold is calculated as 32 * 0.75 = 24, so when 24th element is inserted then the internal storage is reallocated and the content of the table is reinserted.

      If the initial capacity were (AccessMode.values().length / 0.75 + 1) then no reallocation would be necessary.

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

              Created:
              Updated:
              Resolved: