-
Enhancement
-
Resolution: Duplicate
-
P4
-
8
-
None
-
generic
-
generic
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.
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.
- duplicates
-
JDK-7062169 (coll) micro-optimize ArrayList.remove(Object)
-
- Resolved
-