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

Arrays.java micro-optimizations

    Details

    • Subcomponent:
    • Resolved In Build:
      b20
    • CPU:
      x86
    • OS:
      linux
    • Verification:
      Verified

      Description

      A DESCRIPTION OF THE REQUEST :
      java.util.Arrays is know for being very robuts and works well. It can be improved more if we can optimize some critical routines.

      JUSTIFICATION :
      It will improve the performance of java.util.Arrays.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      An improvement in the performance of java.util.Arrays
      ACTUAL -
      It works, but it is not the best implementation possible yet.

      ---------- BEGIN SOURCE ----------
      Problems found:
         /* In this function the loop can start from 1, to remove the if from the loop */
          public static String toString(Object[] a) {
              if (a == null)
                  return "null";
              if (a.length == 0)
                  return "[]";
       
              StringBuilder buf = new StringBuilder();
       
              for (int i = 0; i < a.length; i++) {
                  if (i == 0)
                      buf.append('[');
                  else
                      buf.append(", ");
       
                  buf.append(String.valueOf(a[i]));
              }
       
              buf.append("]");
              return buf.toString();
          }

         /* In this function the loop can start from 1, to remove the if from the loop */
          private static void deepToString(Object[] a, StringBuilder buf,
                                           Set<Object[]> dejaVu) {
              if (a == null) {
                  buf.append("null");
                  return;
              }
              dejaVu.add(a);
              buf.append('[');
              for (int i = 0; i < a.length; i++) {
                  if (i != 0)
                      buf.append(", ");

                  Object element = a[i];
                  if (element == null) {
                      buf.append("null");
                  } else {
                      Class eClass = element.getClass();

                      if (eClass.isArray()) {
                          if (eClass == byte[].class)
                              buf.append(toString((byte[]) element));
                          else if (eClass == short[].class)
                              buf.append(toString((short[]) element));
                          else if (eClass == int[].class)
                              buf.append(toString((int[]) element));
                          else if (eClass == long[].class)
                              buf.append(toString((long[]) element));
                          else if (eClass == char[].class)
                              buf.append(toString((char[]) element));
                          else if (eClass == float[].class)
                              buf.append(toString((float[]) element));
                          else if (eClass == double[].class)
                              buf.append(toString((double[]) element));
                          else if (eClass == boolean[].class)
                              buf.append(toString((boolean[]) element));
                          else { // element is an array of object references
                              if (dejaVu.contains(element))
                                  buf.append("[...]");
                              else
                                  deepToString((Object[])element, buf, dejaVu);
                          }
                      } else { // element is non-null and not an array
                          buf.append(element.toString());
                      }
                  }
              }
              buf.append("]");
              dejaVu.remove(a);
          }
      }



      In sort2(), we can see this loop:
                  for (int k=0; k<numNegZeros; k++)
                      a[++j] = -0.0f;
      /* As the variable numNegZeros is not used after this loop, we can use it to iterate, instead of the k variable. It can be decrementing up to zero. */


         /* The variable cmp is not needed. We can remove the if's from the inner loop if
           we do not use the cmp variable. This improvement can be applied to the float binarySearch() too. */
          private static int binarySearch(double[] a, double key, int low,int high) {
      while (low <= high) {
      int mid = (low + high) >> 1;
      double midVal = a[mid];

                  int cmp;
                  if (midVal < key) {
                      cmp = -1; // Neither val is NaN, thisVal is smaller
                  } else if (midVal > key) {
                      cmp = 1; // Neither val is NaN, thisVal is larger
                  } else {
                      long midBits = Double.doubleToLongBits(midVal);
                      long keyBits = Double.doubleToLongBits(key);
                      cmp = (midBits == keyBits ? 0 : // Values are equal
                             (midBits < keyBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
                              1)); // (0.0, -0.0) or (NaN, !NaN)
                  }

      if (cmp < 0)
      low = mid + 1;
      else if (cmp > 0)
      high = mid - 1;
      else
      return mid; // key found
      }
      return -(low + 1); // key not found.
          }

      ---------- END SOURCE ----------
      ###@###.### 2005-03-11 06:50:47 GMT

        Attachments

          Activity

            People

            • Assignee:
              martin Martin Buchholz
              Reporter:
              jssunw Jitender S (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: