Add MessageList.array() / Rewrite MessageList.add(T[], int, int)

- MessageList.array() should give better performance + concise code
- MessageList.add(T[], int, int) iterated over the source array 3 times at worst case. This commit reduces that to 1 time.
This commit is contained in:
Trustin Lee 2013-06-28 20:47:19 +09:00
parent 0dcf352f4c
commit 7cf88a1a3c

View File

@ -253,23 +253,53 @@ public final class MessageList<T> implements Iterable<T> {
* and return itself. * and return itself.
*/ */
public MessageList<T> add(T[] src, int srcIdx, int srcLen) { public MessageList<T> add(T[] src, int srcIdx, int srcLen) {
checkElements(src, srcIdx, srcLen); if (src == null) {
throw new NullPointerException("src");
}
modifications ++; modifications ++;
int oldSize = size;
int newSize = oldSize + srcLen; int dstIdx = size;
final int newSize = dstIdx + srcLen;
ensureCapacity(newSize); ensureCapacity(newSize);
System.arraycopy(src, srcIdx, elements, oldSize, srcLen);
size = newSize; final int srcEndIdx = srcIdx + srcLen;
int i = srcIdx;
try {
if (byteBufsOnly) { if (byteBufsOnly) {
for (int i = srcIdx; i < srcIdx; i++) { for (; i < srcEndIdx; i ++) {
if (!(src[i] instanceof ByteBuf)) { T m = src[srcIdx];
if (m == null) {
throw new NullPointerException("src[" + srcIdx + ']');
}
elements[dstIdx ++] = m;
if (!(m instanceof ByteBuf)) {
byteBufsOnly = false; byteBufsOnly = false;
break; break;
} }
} }
} }
for (; i < srcEndIdx; i ++) {
T m = src[srcIdx];
if (m == null) {
throw new NullPointerException("src[" + srcIdx + ']');
}
elements[dstIdx ++] = m;
}
} finally {
if (dstIdx != newSize) {
// Could not finish iteration.
Arrays.fill(elements, size, dstIdx, null);
}
}
assert dstIdx == newSize;
size = newSize;
return this; return this;
} }
@ -413,6 +443,23 @@ public final class MessageList<T> implements Iterable<T> {
return new MessageListIterator(); return new MessageListIterator();
} }
/**
* Returns the backing array of this list. Use this array when you want to iterate over the list fast:
* <pre>
* {@link MessageList} list = ...;
* for (Object m: list.array()) {
* if (m == null) {
* break;
* }
*
* handleMessage(m);
* }
* </pre>
*/
public T[] array() {
return elements;
}
/** /**
* Returns {@code true} if all messages contained in this {@link MessageList} are assignment-compatible with the * Returns {@code true} if all messages contained in this {@link MessageList} are assignment-compatible with the
* object represented by this {@link Class}. * object represented by this {@link Class}.
@ -474,18 +521,6 @@ public final class MessageList<T> implements Iterable<T> {
} }
} }
private void checkElements(T[] src, int srcIdx, int srcLen) {
if (src == null) {
throw new NullPointerException("src");
}
int end = srcIdx + srcLen;
for (int i = srcIdx; i < end; i ++) {
if (src[i] == null) {
throw new NullPointerException("src[" + i + ']');
}
}
}
private final class MessageListIterator implements Iterator<T> { private final class MessageListIterator implements Iterator<T> {
private int index; private int index;
private int expectedModifications = modifications; private int expectedModifications = modifications;