8029574: TreeMap: optimization of method computeRedLevel()
authordl
Mon, 16 Nov 2015 10:14:46 -0800
changeset 33867 c07b6cc0c61d
parent 33866 e4b2a5f4dea0
child 33868 9c1bde39fe18
8029574: TreeMap: optimization of method computeRedLevel() Reviewed-by: martin, shade
jdk/src/java.base/share/classes/java/util/TreeMap.java
--- a/jdk/src/java.base/share/classes/java/util/TreeMap.java	Tue Nov 17 10:29:28 2015 -0800
+++ b/jdk/src/java.base/share/classes/java/util/TreeMap.java	Mon Nov 16 10:14:46 2015 -0800
@@ -2581,19 +2581,17 @@
     }
 
     /**
-     * Find the level down to which to assign all nodes BLACK.  This is the
-     * last `full' level of the complete binary tree produced by
-     * buildTree. The remaining nodes are colored RED. (This makes a `nice'
-     * set of color assignments wrt future insertions.) This level number is
+     * Finds the level down to which to assign all nodes BLACK.  This is the
+     * last `full' level of the complete binary tree produced by buildTree.
+     * The remaining nodes are colored RED. (This makes a `nice' set of
+     * color assignments wrt future insertions.) This level number is
      * computed by finding the number of splits needed to reach the zeroeth
-     * node.  (The answer is ~lg(N), but in any case must be computed by same
-     * quick O(lg(N)) loop.)
+     * node.
+     *
+     * @param size the (non-negative) number of keys in the tree to be built
      */
-    private static int computeRedLevel(int sz) {
-        int level = 0;
-        for (int m = sz - 1; m >= 0; m = m / 2 - 1)
-            level++;
-        return level;
+    private static int computeRedLevel(int size) {
+        return 31 - Integer.numberOfLeadingZeros(size + 1);
     }
 
     /**