-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow users to pass in a BufferPool implementation
- Loading branch information
1 parent
92c6058
commit dd2588e
Showing
6 changed files
with
147 additions
and
78 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,50 +1,23 @@ | ||
package com.github.luben.zstd; | ||
|
||
import java.lang.ref.SoftReference; | ||
import java.util.Map; | ||
import java.util.HashMap; | ||
import java.util.Deque; | ||
import java.util.ArrayDeque; | ||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* An pool of buffers which uses a simple reference queue to recycle old buffers. | ||
* An an interface that allows users to customize how buffers are recycled. | ||
*/ | ||
class BufferPool { | ||
private static final Map<Integer, SoftReference<BufferPool>> pools = new HashMap<Integer, SoftReference<BufferPool>>(); | ||
public interface BufferPool { | ||
|
||
static BufferPool get(int length) { | ||
synchronized (pools) { | ||
SoftReference<BufferPool> poolReference = pools.get(length); | ||
BufferPool pool; | ||
if (poolReference == null || (pool = poolReference.get()) == null) { | ||
pool = new BufferPool(length); | ||
poolReference = new SoftReference<BufferPool>(pool); | ||
pools.put(length, poolReference); | ||
} | ||
return pool; | ||
} | ||
} | ||
/** | ||
* Fetch a buffer from the pool. | ||
* @param capacity the desired size of the buffer | ||
* @return a heap buffer with arrayOffset of 0 | ||
*/ | ||
ByteBuffer get(int capacity); | ||
|
||
private final int length; | ||
private final Deque<byte[]> queue; | ||
/** | ||
* Return a buffer to the pool. | ||
* @param buffer the buffer to return | ||
*/ | ||
void release(ByteBuffer buffer); | ||
|
||
BufferPool(int length) { | ||
this.length = length; | ||
this.queue = new ArrayDeque<byte[]>(); | ||
} | ||
|
||
synchronized byte[] checkOut() { | ||
byte[] buffer = queue.pollFirst(); | ||
if (buffer == null) { | ||
buffer = new byte[length]; | ||
} | ||
return buffer; | ||
} | ||
|
||
synchronized void checkIn(byte[] buffer) { | ||
if (length != buffer.length) { | ||
throw new IllegalStateException("buffer size mismatch"); | ||
} | ||
queue.addLast(buffer); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/github/luben/zstd/RecyclingBufferPool.java
This file contains 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,46 @@ | ||
package com.github.luben.zstd; | ||
|
||
import java.lang.ref.SoftReference; | ||
import java.nio.ByteBuffer; | ||
import java.util.Map; | ||
import java.util.HashMap; | ||
import java.util.Deque; | ||
import java.util.ArrayDeque; | ||
|
||
/** | ||
* An pool of buffers which uses a simple reference queue to recycle old buffers. | ||
*/ | ||
public class RecyclingBufferPool implements BufferPool { | ||
public static final BufferPool INSTANCE = new RecyclingBufferPool(); | ||
|
||
private final Map<Integer, SoftReference<Deque<ByteBuffer>>> pools; | ||
|
||
private RecyclingBufferPool() { | ||
this.pools = new HashMap<Integer, SoftReference<Deque<ByteBuffer>>>(); | ||
} | ||
|
||
private Deque<ByteBuffer> getDeque(int capacity) { | ||
SoftReference<Deque<ByteBuffer>> dequeReference = pools.get(capacity); | ||
Deque<ByteBuffer> deque; | ||
if (dequeReference == null || (deque = dequeReference.get()) == null) { | ||
deque = new ArrayDeque<ByteBuffer>(); | ||
dequeReference = new SoftReference<Deque<ByteBuffer>>(deque); | ||
pools.put(capacity, dequeReference); | ||
} | ||
return deque; | ||
} | ||
|
||
@Override | ||
public synchronized ByteBuffer get(int capacity) { | ||
ByteBuffer buffer = getDeque(capacity).pollFirst(); | ||
if (buffer == null) { | ||
buffer = ByteBuffer.allocate(capacity); | ||
} | ||
return buffer; | ||
} | ||
|
||
@Override | ||
public synchronized void release(ByteBuffer buffer) { | ||
getDeque(buffer.capacity()).addLast(buffer); | ||
} | ||
} |
This file contains 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 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 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 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