Using LinkedList seems to yield same or better performance

This commit is contained in:
Trustin Lee 2008-09-28 14:21:38 +00:00
parent 40b1f26c34
commit 4d17db6eb1

View File

@ -22,6 +22,9 @@
*/ */
package org.jboss.netty.util; package org.jboss.netty.util;
import java.util.LinkedList;
import java.util.Queue;
/** /**
* @author The Netty Project (netty-dev@lists.jboss.org) * @author The Netty Project (netty-dev@lists.jboss.org)
@ -32,46 +35,35 @@ package org.jboss.netty.util;
*/ */
public class FastQueue<E> { public class FastQueue<E> {
static final int INITIAL_CAPACITY = 4;
// Put // Put
private Object[] elements; private Queue<E> offeredElements;
private int size;
// Take // Take
private Object[] drainedElements; private Queue<E> drainedElements;
private int drainedElementCount;
private int index;
public synchronized void offer(E e) { public synchronized void offer(E e) {
if (elements == null) { if (offeredElements == null) {
elements = new Object[INITIAL_CAPACITY]; offeredElements = new LinkedList<E>();
} else if (size == elements.length) {
Object[] newElements = new Object[size << 1];
System.arraycopy(elements, 0, newElements, 0, size);
elements = newElements;
} }
elements[size ++] = e; offeredElements.offer(e);
} }
public E poll() { public E poll() {
for (;;) { for (;;) {
if (drainedElements == null) { if (drainedElements == null) {
synchronized (this) { synchronized (this) {
drainedElements = elements; drainedElements = offeredElements;
if (elements == null) { if (offeredElements == null) {
break; break;
} }
drainedElementCount = size; offeredElements = null;
elements = null;
size = 0;
} }
index = 0;
} }
if (index < drainedElementCount) { E e = cast(drainedElements.poll());
return cast(drainedElements[index ++]); if (e != null) {
return e;
} }
drainedElements = null; drainedElements = null;