(coll) Inefficient calculation of power of two in HashMap

XMLWordPrintable

    • Type: Enhancement
    • Resolution: Fixed
    • Priority: P4
    • 8
    • Affects Version/s: 8
    • Component/s: 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.

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

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: