Top Description Inners Fields Constructors Methods
rx.internal.util

public final Class IndexedRingBuffer<E>

extends Object
implements Subscription
Class Inheritance
All Implemented Interfaces
rx.Subscription
Imports
java.util.concurrent.atomic.AtomicInteger, .AtomicIntegerArray, .AtomicReference, .AtomicReferenceArray, rx.Subscription, rx.functions.Func1

Add/Remove without object allocation (after initial construction).

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
 

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
pack-priv static class

Field Summary

Modifier and TypeField and Description
private final IndexedRingBuffer.ElementSection<E>
pack-priv final AtomicInteger
private final IndexedRingBuffer.IndexSection
pack-priv final AtomicInteger
pack-priv static final int

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

the index where the element was added
add
(E
the element to add
e
)

Add an element and return the index where it was added to allow removal.

public int
forEach(Func1<? super E, Boolean> action)

public int

Returns:

int of next index to process, or last index seen if it exited early
forEach
(Func1<? super E, Boolean>
that 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
forEach(Func1<? super E, Boolean> action, int startIndex, int endIndex)

private IndexedRingBuffer.ElementSection<E>
getElementSection(int index)

private synchronized int
private synchronized int

Returns:

the index or -1 if none
getIndexFromPreviouslyRemoved
()

Returns -1 if nothing, 0 or greater if the index should be used

private IndexedRingBuffer.IndexSection
getIndexSection(int index)

public static <T> IndexedRingBuffer<T>
public boolean
isUnsubscribed()

Implements rx.Subscription.isUnsubscribed.

Indicates whether this Subscription is currently unsubscribed.
private synchronized void
pushRemovedIndex(int elementIndex)

public void
releaseToPool()

This resets the arrays, nulls out references and returns it to the pool.

public E
remove(int index)

public void
unsubscribe()

Implements rx.Subscription.unsubscribe.

Stops the receipt of notifications on the Subscriber that was registered when this Subscription was received.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

elementsback to summary
private final IndexedRingBuffer.ElementSection<E> elements
indexback to summary
pack-priv final AtomicInteger index
removedback to summary
private final IndexedRingBuffer.IndexSection removed
removedIndexback to summary
pack-priv final AtomicInteger removedIndex
SIZEback to summary
pack-priv static final int SIZE

Constructor Detail

IndexedRingBufferback to summary
pack-priv IndexedRingBuffer()

Method Detail

addback to summary
public int add(E e)

Add an element and return the index where it was added to allow removal.

Parameters
e:E

the element to add

Returns:int

the index where the element was added

forEachback to summary
public int forEach(Func1<? super E, Boolean> action)
forEachback to summary
public int forEach(Func1<? super E, Boolean> action, int startIndex)

Loop through each element in the buffer and call a specific function.

Parameters
action:Func1<? super E, Boolean>

that processes each item and returns true if it wants to continue to the next

startIndex:int

at which index the loop should start

Returns:int

int of next index to process, or last index seen if it exited early

forEachback to summary
private int forEach(Func1<? super E, Boolean> action, int startIndex, int endIndex)
getElementSectionback to summary
private IndexedRingBuffer.ElementSection<E> getElementSection(int index)
getIndexForAddback to summary
private synchronized int getIndexForAdd()
getIndexFromPreviouslyRemovedback to summary
private synchronized int getIndexFromPreviouslyRemoved()

Returns -1 if nothing, 0 or greater if the index should be used

Returns:int

the index or -1 if none

getIndexSectionback to summary
private IndexedRingBuffer.IndexSection getIndexSection(int index)
getInstanceback to summary
public static <T> IndexedRingBuffer<T> getInstance()
isUnsubscribedback to summary
public boolean isUnsubscribed()

Implements rx.Subscription.isUnsubscribed.

Doc from rx.Subscription.isUnsubscribed.

Indicates whether this Subscription is currently unsubscribed.

Returns:boolean

true if this Subscription is currently unsubscribed, false otherwise

Annotations
@Override
pushRemovedIndexback to summary
private synchronized void pushRemovedIndex(int elementIndex)
releaseToPoolback 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.

removeback to summary
public E remove(int index)
unsubscribeback to summary
public void unsubscribe()

Implements rx.Subscription.unsubscribe.

Doc from rx.Subscription.unsubscribe.

Stops the receipt of notifications on the Subscriber that was registered when this Subscription was received.

This allows deregistering an Subscriber before it has finished receiving all events (i.e. before onCompleted is called).

Annotations
@Override
rx.internal.util back to summary

pack-priv final Class IndexedRingBuffer.ElementSection<E>

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final AtomicReferenceArray<E>
pack-priv final AtomicReference<IndexedRingBuffer.ElementSection<E>>

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv IndexedRingBuffer.ElementSection<E>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

arrayback to summary
pack-priv final AtomicReferenceArray<E> array
nextback to summary
pack-priv final AtomicReference<IndexedRingBuffer.ElementSection<E>> next

Constructor Detail

ElementSectionback to summary
pack-priv ElementSection()

Method Detail

getNextback to summary
pack-priv IndexedRingBuffer.ElementSection<E> getNext()
rx.internal.util back to summary

pack-priv Class IndexedRingBuffer.IndexSection

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final AtomicReference<IndexedRingBuffer.IndexSection>
private final AtomicIntegerArray

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public int
getAndSet(int expected, int newValue)

pack-priv IndexedRingBuffer.IndexSection
public void
set(int i, int elementIndex)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_nextback to summary
private final AtomicReference<IndexedRingBuffer.IndexSection> _next
unsafeArrayback to summary
private final AtomicIntegerArray unsafeArray

Constructor Detail

IndexSectionback to summary
pack-priv IndexSection()

Method Detail

getAndSetback to summary
public int getAndSet(int expected, int newValue)
getNextback to summary
pack-priv IndexedRingBuffer.IndexSection getNext()
setback to summary
public void set(int i, int elementIndex)