Skip to content

Switch PooledBlockAllocatorProvider's ConcurrentLinkedQueue to an ArrayBlockingQueue #371

@zslayton

Description

@zslayton

The PooledBlockAllocatorProvider stores its pooled values in a ConcurrentLinkedQueue:

https://github.com/amzn/ion-java/blob/f4e2e6b7b715a2e46bfa82c7ab047cfdc3de3efc/src/com/amazon/ion/impl/bin/PooledBlockAllocatorProvider.java#L39

Later, when one of those instances is returned to the pool, this code runs:

https://github.com/amzn/ion-java/blob/f4e2e6b7b715a2e46bfa82c7ab047cfdc3de3efc/src/com/amazon/ion/impl/bin/PooledBlockAllocatorProvider.java#L60-L63

Unfortunately, ConcurrentLinkedQueue's documentation for the size() method says:

Beware that, unlike in most collections, this method is NOT a constant-time operation. Because of the asynchronous nature of these queues, determining the current number of elements requires an O(n) traversal. Additionally, if elements are added or removed during execution of this method, the returned result may be inaccurate. Thus, this method is typically not very useful in concurrent applications.

Under high contention, this could lead to some number of instances being discarded rather than added to the pool or vice versa.

We're not relying on the non-blocking nature of the ConcurrentLinkedQueue, so we can probably just swap this out for an ArrayBlockingQueue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions