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

Need method to create pre-sized HashMap

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 19
    • None
    • core-libs

      JDK contains number of usages of HashMap, which are created with non-optimal initial capacity.
      It seems that people normally assume that the constructor HashMap(int) requires the expected size as the argument.
      In fact, the argument is the initial capacity, which should be bigger than the expected size. As a result, unnecessary reallocations can occur while the maps are filled.

      Retrospectively, is seems to be more convenient to have a constructor which specifies the expected size, like is was done with IdentityHashMap, for example.

      Here is (presumably non complete) list of places under jdk/src/java.base where the initial capacity wasn't chosen optimal:

      share/classes/sun/security/ssl/HandshakeStateManager.java: handshakeTypes = new HashMap<>(15);

      share/classes/sun/security/provider/certpath/OCSPResponse.java: singleResponseMap = new HashMap<>(singleResponseDer.length);

      share/classes/sun/security/provider/certpath/OCSPResponse.java: new HashMap<>(extDer.length);

      share/classes/sun/security/util/Cache.java: Map<K,V> kvmap = new HashMap<>(cacheMap.size());

      share/classes/sun/util/locale/provider/LocaleDataMetaInfo-XLocales.java.template: private static final Map<String, String> resourceNameToLocales = new HashMap<>(9);

      share/classes/java/lang/Character.java: new HashMap<>((int)(510 / 0.75f + 1.0f));

      share/classes/java/lang/Character.java: aliases = new HashMap<>(134);

      share/classes/java/lang/Class.java: directory = new HashMap<>(2 * universe.length);

      share/classes/java/lang/invoke/VarHandle.java: methodNameToAccessMode = new HashMap<>(AccessMode.values().length);

      share/classes/java/lang/Thread.java: Map<Thread, StackTraceElement[]> m = new HashMap<>(threads.length);

      share/classes/java/security/cert/PKIXRevocationChecker.java: Map<X509Certificate, byte[]> copy = new HashMap<>(responses.size());

      share/classes/java/security/cert/PKIXRevocationChecker.java: Map<X509Certificate, byte[]> copy = new HashMap<>(ocspResponses.size());

      share/classes/java/security/cert/CertificateRevokedException.java: extensions = new HashMap<>(size);

      share/classes/java/text/NumberFormat.java: private static final Map<String, Field> instanceMap = new HashMap<>(11);

      share/classes/java/text/AttributedCharacterIterator.java: private static final Map<String, Attribute> instanceMap = new HashMap<>(7);

      share/classes/java/text/DateFormat.java: private static final Map<String, Field> instanceMap = new HashMap<>(18);

      share/classes/java/util/jar/Attributes.java: map = new LinkedHashMap<>(size);

      share/classes/java/util/jar/JarVerifier.java: signerMap = new HashMap<>(verifiedSigners.size() + sigFileSigners.size());

      share/classes/java/util/ListResourceBundle.java: HashMap<String,Object> temp = new HashMap<>(contents.length);

      share/classes/jdk/internal/module/ModuleInfo.java: Map<String, byte[]> map = new HashMap<>(hash_count);

      share/classes/javax/net/ssl/SSLParameters.java: sniNames = new LinkedHashMap<>(serverNames.size());

      share/classes/javax/net/ssl/SSLParameters.java: sniMatchers = new HashMap<>(matchers.size());

      share/classes/com/sun/java/util/jar/pack/Package.java: allInnerClassesByThis = new HashMap<>(allInnerClasses.size());

      unix/classes/sun/nio/fs/MimeTypesFileTypeDetector.java: mimeTypeMap = new HashMap<>(lines.size());


      (smarks 2022-02-18)

      In addition, see the cases covered by the tests in JDK-8282120. These are 1) the Enum constant directory creation in java.lang.Class and 20 the map creation in Character.UnicodeBlock.

            smarks Stuart Marks
            igerasim Ivan Gerasimov
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: