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

(coll) Inefficient calculation of power of two in HashMap

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 8
    • 8
    • core-libs
    • b103
    • generic
    • generic
    • Not verified

        This is a SUNBUG for 100189: https://bugs.openjdk.java.net/show_bug.cgi?id=100189

        Constructor does this:

        public HashMap(int initialCapacity, float loadFactor) {
          ...
          int capacity = 1;
          while (capacity < initialCapacity)
              capacity <<= 1;
          ...
        }

        It is magnitude+ faster to do this:

        static final double LOG2 = Math.log(2.0);
        public HashMap(int initialCapacity, float loadFactor) {
          ...
          int capacity = 1 << ((int)Math.ceil(Math.log(initialCapacity)/LOG2 ));
          ...
        }


        Given the error checking in the head of the constructor, this code should just
        plug-in.

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

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: