Browse Source

HADOOP-3398. Minor improvement to a utility function in that participates in
backoff calculation.



git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@657985 13f79535-47bb-0310-9956-ffa450edef68

Christopher Douglas 17 years ago
parent
commit
a98f001b7a
2 changed files with 15 additions and 10 deletions
  1. 3 0
      CHANGES.txt
  2. 12 10
      src/java/org/apache/hadoop/mapred/ReduceTask.java

+ 3 - 0
CHANGES.txt

@@ -146,6 +146,9 @@ Trunk (unreleased changes)
     HADOOP-3377. Remove TaskRunner::replaceAll and replace with equivalent
     String::replace. (Brice Arnould via cdouglas)
 
+    HADOOP-3398. Minor improvement to a utility function in that participates
+    in backoff calculation. (cdouglas)
+
   OPTIMIZATIONS
 
     HADOOP-3274. The default constructor of BytesWritable creates empty 

+ 12 - 10
src/java/org/apache/hadoop/mapred/ReduceTask.java

@@ -1731,16 +1731,18 @@ class ReduceTask extends Task {
 
   }
 
+  /**
+   * Return the exponent of the power of two closest to the given
+   * positive value, or zero if value leq 0.
+   * This follows the observation that the msb of a given value is
+   * also the closest power of two, unless the bit following it is
+   * set.
+   */
   private static int getClosestPowerOf2(int value) {
-    int power = 0;
-    int approx = 1;
-    while (approx < value) {
-      ++power;
-      approx = (approx << 1);
-    }
-    if ((value - (approx >> 1)) < (approx - value)) {
-      --power;
-    }
-    return power;
+    if (value <= 0)
+      throw new IllegalArgumentException("Undefined for " + value);
+    final int hob = Integer.highestOneBit(value);
+    return Integer.numberOfTrailingZeros(hob) +
+      (((hob >>> 1) & value) == 0 ? 0 : 1);
   }
 }