This is meant for hundreds or single-digit thousands of elements that need to be rapidly added and randomly or sequentially removed while avoiding object allocation.
On Intel Core i7, 2.3Mhz, Mac Java 8:
- adds per second single-threaded => ~32,598,500 for 100 - adds per second single-threaded => ~23,200,000 for 10,000 - adds + removes per second single-threaded => 15,562,100 for 100 - adds + removes per second single-threaded => 8,760,000 for 10,000
Benchmark (size) Mode Samples Score Score error Units
r.i.IndexedRingBufferPerf.indexedRingBufferAdd 100 thrpt 5 263571.721 9856.994 ops/s
r.i.IndexedRingBufferPerf.indexedRingBufferAdd 10000 thrpt 5 1763.417 211.998 ops/s
r.i.IndexedRingBufferPerf.indexedRingBufferAddRemove 100 thrpt 5 139850.115 17143.705 ops/s
r.i.IndexedRingBufferPerf.indexedRingBufferAddRemove 10000 thrpt 5 809.982 72.931 ops/s
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | |
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| private final IndexedRingBuffer. | |
| pack-priv final AtomicInteger | |
| private final IndexedRingBuffer. | |
| pack-priv final AtomicInteger | |
| pack-priv static final int |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public int | Returns: the index where the element was addedthe element to add e)Add an element and return the index where it was added to allow removal. |
| public int | |
| public int | Returns: int of next index to process, or last index seen if it exited earlythat processes each item and returns true if it wants to continue to the next action, int at which index the loop should start startIndex)Loop through each element in the buffer and call a specific function. |
| private int | |
| private IndexedRingBuffer. | |
| private synchronized int | |
| private synchronized int | Returns: the index or -1 if noneReturns -1 if nothing, 0 or greater if the index should be used |
| private IndexedRingBuffer. | |
| public static <T> IndexedRingBuffer | |
| public boolean | isUnsubscribed()
Implements rx. Subscription is currently unsubscribed.
|
| private synchronized void | |
| public void | |
| public E | |
| public void | unsubscribe()
Implements rx. Subscriber that was registered when this Subscription
was received.
|
| elements | back to summary |
|---|---|
| private final IndexedRingBuffer. | |
| index | back to summary |
|---|---|
| pack-priv final AtomicInteger index | |
| removed | back to summary |
|---|---|
| private final IndexedRingBuffer. | |
| removedIndex | back to summary |
|---|---|
| pack-priv final AtomicInteger removedIndex | |
| SIZE | back to summary |
|---|---|
| pack-priv static final int SIZE | |
| IndexedRingBuffer | back to summary |
|---|---|
| pack-priv IndexedRingBuffer() | |
| add | back to summary |
|---|---|
| public int add(E e) Add an element and return the index where it was added to allow removal.
| |
| forEach | back to summary |
|---|---|
| public int forEach(Func1<? super E, Boolean> action) | |
| forEach | back to summary |
|---|---|
| public int forEach(Func1<? super E, Boolean> action, int startIndex) Loop through each element in the buffer and call a specific function. | |
| forEach | back to summary |
|---|---|
| private int forEach(Func1<? super E, Boolean> action, int startIndex, int endIndex) | |
| getElementSection | back to summary |
|---|---|
| private IndexedRingBuffer. | |
| getIndexForAdd | back to summary |
|---|---|
| private synchronized int getIndexForAdd() | |
| getIndexFromPreviouslyRemoved | back to summary |
|---|---|
| private synchronized int getIndexFromPreviouslyRemoved() Returns -1 if nothing, 0 or greater if the index should be used
| |
| getIndexSection | back to summary |
|---|---|
| private IndexedRingBuffer. | |
| getInstance | back to summary |
|---|---|
| public static <T> IndexedRingBuffer | |
| isUnsubscribed | back to summary |
|---|---|
| public boolean isUnsubscribed() Implements rx. Doc from rx. Indicates whether this
| |
| pushRemovedIndex | back to summary |
|---|---|
| private synchronized void pushRemovedIndex(int elementIndex) | |
| releaseToPool | back to summary |
|---|---|
| public void releaseToPool() This resets the arrays, nulls out references and returns it to the pool. This extra CPU cost is far smaller than the object allocation cost of not pooling. | |
| remove | back to summary |
|---|---|
| public E remove(int index) | |
| unsubscribe | back to summary |
|---|---|
| public void unsubscribe() Implements rx. Doc from rx. Stops the receipt of notifications on the
This allows deregistering an
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final AtomicReferenceArray | |
| pack-priv final AtomicReference |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv IndexedRingBuffer. |
| array | back to summary |
|---|---|
| pack-priv final AtomicReferenceArray<E> array | |
| next | back to summary |
|---|---|
| pack-priv final AtomicReference<IndexedRingBuffer. | |
| ElementSection | back to summary |
|---|---|
| pack-priv ElementSection() | |
| getNext | back to summary |
|---|---|
| pack-priv IndexedRingBuffer. | |
| Modifier and Type | Field and Description |
|---|---|
| private final AtomicReference | |
| private final AtomicIntegerArray |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public int | |
| pack-priv IndexedRingBuffer. | |
| public void |
| _next | back to summary |
|---|---|
| private final AtomicReference<IndexedRingBuffer. | |
| unsafeArray | back to summary |
|---|---|
| private final AtomicIntegerArray unsafeArray | |
| IndexSection | back to summary |
|---|---|
| pack-priv IndexSection() | |
| getAndSet | back to summary |
|---|---|
| public int getAndSet(int expected, int newValue) | |
| getNext | back to summary |
|---|---|
| pack-priv IndexedRingBuffer. | |
| set | back to summary |
|---|---|
| public void set(int i, int elementIndex) | |