-
Notifications
You must be signed in to change notification settings - Fork 125
Adds a pool of UTF-8 decoders, making reader instantiation less expensive. #388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.amazon.ion.impl.bin.utf8; | ||
|
|
||
| /** | ||
| * A thread-safe shared pool of {@link PoolableByteBuffer}s. | ||
| */ | ||
| public class ByteBufferPool extends Pool<PoolableByteBuffer> { | ||
|
|
||
| private static final ByteBufferPool INSTANCE = new ByteBufferPool(); | ||
|
|
||
| // Do not allow instantiation; all classes should share the singleton instance. | ||
| private ByteBufferPool() { | ||
| super(new Allocator<PoolableByteBuffer>() { | ||
| @Override | ||
| public PoolableByteBuffer newInstance(Pool<PoolableByteBuffer> pool) { | ||
| return new PoolableByteBuffer(pool); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * @return a threadsafe shared instance of {@link ByteBufferPool}. | ||
| */ | ||
| public static ByteBufferPool getInstance() { | ||
| return INSTANCE; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package com.amazon.ion.impl.bin.utf8; | ||
|
|
||
| import java.util.concurrent.ArrayBlockingQueue; | ||
|
|
||
| abstract class Pool<T extends Poolable<?>> { | ||
|
|
||
| /** | ||
| * Allocates objects to be pooled. | ||
| * @param <T> the type of object. | ||
| */ | ||
| interface Allocator<T extends Poolable<?>> { | ||
|
|
||
| /** | ||
| * Allocate a new object and link it to the given pool. | ||
| * @param pool the pool to which the new object will be linked. | ||
| * @return a new instance. | ||
| */ | ||
| T newInstance(Pool<T> pool); | ||
| } | ||
|
|
||
| // The maximum number of objects that can be waiting in the queue before new ones will be discarded. | ||
| private static final int MAX_QUEUE_SIZE = 128; | ||
|
|
||
| // A queue of previously initialized objects that can be loaned out. | ||
| private final ArrayBlockingQueue<T> bufferQueue; | ||
|
|
||
| // Allocator of objects to be pooled. | ||
| private final Allocator<T> allocator; | ||
|
|
||
| Pool(Allocator<T> allocator) { | ||
| this.allocator = allocator; | ||
| bufferQueue = new ArrayBlockingQueue<T>(MAX_QUEUE_SIZE); | ||
| } | ||
|
|
||
| /** | ||
| * If the pool is not empty, removes an object from the pool and returns it; | ||
| * otherwise, constructs a new object. | ||
| * | ||
| * @return An object. | ||
| */ | ||
| public T getOrCreate() { | ||
| // The `poll` method does not block. If the queue is empty it returns `null` immediately. | ||
| T object = bufferQueue.poll(); | ||
| if (object == null) { | ||
| // No buffers were available in the pool. Create a new one. | ||
| object = allocator.newInstance(this); | ||
| } | ||
| return object; | ||
| } | ||
|
|
||
| /** | ||
| * Adds the provided instance to the pool. If the pool is full, the instance will | ||
| * be discarded. | ||
| * | ||
| * Callers MUST NOT use an object after returning it to the pool. | ||
| * | ||
| * @param object An object to add to the pool. | ||
| */ | ||
| public void returnToPool(T object) { | ||
| // The `offer` method does not block. If the queue is full, it returns `false` immediately. | ||
| // If the provided instance cannot be added to the pool, we discard it silently. | ||
| bufferQueue.offer(object); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package com.amazon.ion.impl.bin.utf8; | ||
|
|
||
| import java.io.Closeable; | ||
|
|
||
| /** | ||
| * Base class for types that may be pooled. | ||
| * @param <T> the concrete type. | ||
| */ | ||
| abstract class Poolable<T extends Poolable<T>> implements Closeable { | ||
|
|
||
| // The pool to which this object is linked. | ||
| private final Pool<T> pool; | ||
|
|
||
| /** | ||
| * @param pool the pool to which the object will be returned upon {@link #close()}. | ||
| */ | ||
| Poolable(Pool<T> pool) { | ||
| this.pool = pool; | ||
| } | ||
|
|
||
| /** | ||
| * Attempts to return this instance to the pool with which it is associated, if any. | ||
| * | ||
| * Do not continue to use this instance after calling this method. | ||
| */ | ||
| @Override | ||
| public void close() { | ||
| pool.returnToPool((T) this); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package com.amazon.ion.impl.bin.utf8; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
|
|
||
| /** | ||
| * Holds a reusable {@link ByteBuffer}. Instances of this class are reusable but are NOT threadsafe. | ||
| * | ||
| * Instances are vended by {@link ByteBufferPool#getOrCreate()}. | ||
| * | ||
| * Users are expected to call {@link #close()} when the decoder is no longer needed. | ||
| */ | ||
| public class PoolableByteBuffer extends Poolable<PoolableByteBuffer> { | ||
|
|
||
| static final int BUFFER_SIZE_IN_BYTES = 4 * 1024; | ||
|
|
||
| // The reusable buffer. | ||
| private final ByteBuffer buffer; | ||
|
|
||
| /** | ||
| * @param pool the pool to which the object will be returned upon {@link #close()}. | ||
| */ | ||
| PoolableByteBuffer(Pool<PoolableByteBuffer> pool) { | ||
| super(pool); | ||
| buffer = ByteBuffer.allocate(BUFFER_SIZE_IN_BYTES); | ||
| } | ||
|
|
||
| /** | ||
| * @return the buffer. | ||
| */ | ||
| public ByteBuffer getBuffer() { | ||
| return buffer; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type bound is to ensure that only subclasses of
Poolablecan implementPoolable, right? Which provides the guarantee that you'll get instantiated with reference to an appropriatePool?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it allows us to guarantee that we're getting a
Pool<T>(i.e. aPoolfor this type ofPoolable) in the constructor.