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

(coll) minor optimizations in java.util.AbstractList & java.util.ArrayList

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • tbd
    • 8
    • core-libs
    • None

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

      AbstractList.java defines addAll as follows:

          public boolean addAll(int index, Collection<? extends E> c) {
              rangeCheckForAdd(index);
              boolean modified = false;
              for (E e : c) {
                  add(index++, e);
                  modified = true;
              }
              return modified;
          }

      with unnecessary 'modified' var; by definition of foreach, only case in which
      'modified == false' is when e.isEmpty; code should be replaced by shorted and
      faster version, e.g.

          public boolean addAll(int index, Collection<? extends E> c) {
              rangeCheckForAdd(index);
              for (E e : c)
                  add(index++, e);
              return !c.isEmpty();
          }

      ArrayList uses following code in some places (example of fastRemove here, but
      similar code is used in e.g. remove, addAll, removeRange etc.)

          private void fastRemove(int index) {
              modCount++;
              int numMoved = size - index - 1;
              if (numMoved > 0)
                  System.arraycopy(elementData, index+1, elementData, index,
                                   numMoved);
              elementData[--size] = null; // Let gc do its work
          }

      it can possibly be optimized to (clearer code, one less var initialized, less
      math done):

          private void fastRemove(int index) {
              modCount++;
              size--;
              if (size != index)
                  System.arraycopy(elementData, index+1, elementData, index, size -
      index);
              elementData[size] = null; // Let gc do its work
          }
      or, similarily (in removeRange):

              int numMoved = size - toIndex;
              System.arraycopy(elementData, toIndex, elementData, fromIndex,
                               numMoved);
      to just (numMoved is not used anywhere else)
              System.arraycopy(elementData, toIndex, elementData, fromIndex,
                               size - toIndex);

      btw, please inform me if I posted it in a wrong place; I'm not used to Java's
      bugzilla.

            Unassigned Unassigned
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: