Skip to content
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

Weak reference concurrent pool #10787

Merged
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
acbca5a
Experiment with a weakly referenced ConcurrentPool
gregw Oct 25, 2023
9eebabb
Experiment with a weakly referenced ConcurrentPool
gregw Oct 25, 2023
4fbcb0a
Experiment with a weakly referenced ConcurrentPool
gregw Oct 25, 2023
86ecaa2
Experiment with a weakly referenced ConcurrentPool
gregw Oct 25, 2023
9e3d183
Experiment with a weakly referenced ConcurrentPool
gregw Oct 25, 2023
9218c7d
Experiment with a weakly referenced ConcurrentPool
gregw Oct 25, 2023
9014d68
Experiment with a weakly referenced ConcurrentPool
gregw Oct 26, 2023
db414fd
Merge remote-tracking branch 'origin/jetty-12.0.x' into experiment/je…
gregw Oct 26, 2023
1f91daf
Experiment with a weakly referenced ConcurrentPool
gregw Oct 26, 2023
ae9b7e8
Modify ArrayByteBufferPool and ConcurrentPool to make weak-referencin…
lorban Oct 26, 2023
c0eaa4a
Experiment with a weakly referenced ConcurrentPool
gregw Oct 26, 2023
8ce0260
Experiment with a weakly referenced ConcurrentPool
gregw Oct 27, 2023
11a3dff
Experiment with a weakly referenced ConcurrentPool
gregw Oct 29, 2023
0d54890
Experiment with a weakly referenced ConcurrentPool
gregw Oct 31, 2023
c58905a
Merge branch 'jetty-12.0.x' into experiment/jetty-12/WeakReference-Co…
gregw Oct 31, 2023
bbcc930
A weakly referenced ConcurrentPool
gregw Oct 31, 2023
2604eb6
Merge branch 'jetty-12.0.x' into experiment/jetty-12/WeakReference-Co…
gregw Nov 1, 2023
c28979f
Merge branch 'jetty-12.0.x' into experiment/jetty-12/WeakReference-Co…
gregw Nov 1, 2023
dd53992
A weakly referenced ConcurrentPool
gregw Nov 1, 2023
8799ea4
* Avoid using deprecated ConcurrentPool constructors.
sbordet Nov 9, 2023
3d0fb16
Merged branch 'jetty-12.0.x' into 'experiment/jetty-12/WeakReference-…
sbordet Nov 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public class ArrayByteBufferPool implements ByteBufferPool, Dumpable

/**
* Creates a new ArrayByteBufferPool with a default configuration.
* Both {@code maxHeapMemory} and {@code maxDirectMemory} default to 0 to use default heuristic.
* Both {@code maxHeapMemory} and {@code maxDirectMemory} default to 0 to use default heuristic;
* pooled buffers are hard-referenced at all times.
gregw marked this conversation as resolved.
Show resolved Hide resolved
*/
public ArrayByteBufferPool()
{
Expand All @@ -79,7 +80,8 @@ public ArrayByteBufferPool()

/**
* Creates a new ArrayByteBufferPool with the given configuration.
* Both {@code maxHeapMemory} and {@code maxDirectMemory} default to 0 to use default heuristic.
* Both {@code maxHeapMemory} and {@code maxDirectMemory} default to 0 to use default heuristic;
* pooled buffers are hard-referenced at all times.
*
* @param minCapacity the minimum ByteBuffer capacity
* @param factor the capacity factor
Expand All @@ -92,7 +94,8 @@ public ArrayByteBufferPool(int minCapacity, int factor, int maxCapacity)

/**
* Creates a new ArrayByteBufferPool with the given configuration.
* Both {@code maxHeapMemory} and {@code maxDirectMemory} default to 0 to use default heuristic.
* Both {@code maxHeapMemory} and {@code maxDirectMemory} default to 0 to use default heuristic;
* pooled buffers are hard-referenced at all times.
*
* @param minCapacity the minimum ByteBuffer capacity
* @param factor the capacity factor
Expand All @@ -106,6 +109,7 @@ public ArrayByteBufferPool(int minCapacity, int factor, int maxCapacity, int max

/**
* Creates a new ArrayByteBufferPool with the given configuration.
* Pooled buffers are hard-referenced at all times.
*
* @param minCapacity the minimum ByteBuffer capacity
* @param factor the capacity factor
Expand All @@ -116,7 +120,23 @@ public ArrayByteBufferPool(int minCapacity, int factor, int maxCapacity, int max
*/
public ArrayByteBufferPool(int minCapacity, int factor, int maxCapacity, int maxBucketSize, long maxHeapMemory, long maxDirectMemory)
{
this(minCapacity, factor, maxCapacity, maxBucketSize, maxHeapMemory, maxDirectMemory, null, null);
this(minCapacity, factor, maxCapacity, maxBucketSize, maxHeapMemory, maxDirectMemory, null, null, false);
}

/**
* Creates a new ArrayByteBufferPool with the given configuration.
*
* @param minCapacity the minimum ByteBuffer capacity
* @param factor the capacity factor
* @param maxCapacity the maximum ByteBuffer capacity
* @param maxBucketSize the maximum number of ByteBuffers for each bucket
* @param maxHeapMemory the max heap memory in bytes, -1 for unlimited memory or 0 to use default heuristic
* @param maxDirectMemory the max direct memory in bytes, -1 for unlimited memory or 0 to use default heuristic
* @param weakPool true if the pooled buffers should be weakly referenced upon acquisition, false otherwise
*/
public ArrayByteBufferPool(int minCapacity, int factor, int maxCapacity, int maxBucketSize, long maxHeapMemory, long maxDirectMemory, boolean weakPool)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't really like the telescopic additional "weakPool" parameter -- seems an implementation detail that leaks many layers up, and if we change our mind it'll be part of the public APIs for a long time.

Copy link
Contributor Author

@gregw gregw Oct 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbordet Isn't this at odds with your previous comment about preferring "explicit configuration". How would you configure this if not by constructor parameter? Should we just have a different classes for Weak pools?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gregw the comments above are now out of date, and the weakPool parameter is never used, so please remove it.

{
this(minCapacity, factor, maxCapacity, maxBucketSize, maxHeapMemory, maxDirectMemory, null, null, weakPool);
}

/**
Expand All @@ -130,8 +150,9 @@ public ArrayByteBufferPool(int minCapacity, int factor, int maxCapacity, int max
* @param maxDirectMemory the max direct memory in bytes, -1 for unlimited memory or 0 to use default heuristic
* @param bucketIndexFor a {@link IntUnaryOperator} that takes a capacity and returns a bucket index
* @param bucketCapacity a {@link IntUnaryOperator} that takes a bucket index and returns a capacity
* @param weakPool true if the underlying pool should weakly reference pooled buffers when they are acquired, false otherwise
*/
protected ArrayByteBufferPool(int minCapacity, int factor, int maxCapacity, int maxBucketSize, long maxHeapMemory, long maxDirectMemory, IntUnaryOperator bucketIndexFor, IntUnaryOperator bucketCapacity)
protected ArrayByteBufferPool(int minCapacity, int factor, int maxCapacity, int maxBucketSize, long maxHeapMemory, long maxDirectMemory, IntUnaryOperator bucketIndexFor, IntUnaryOperator bucketCapacity, boolean weakPool)
{
if (minCapacity <= 0)
minCapacity = 0;
Expand All @@ -153,8 +174,8 @@ protected ArrayByteBufferPool(int minCapacity, int factor, int maxCapacity, int
for (int i = 0; i < directArray.length; i++)
{
int capacity = Math.min(bucketCapacity.applyAsInt(i), maxCapacity);
directArray[i] = new RetainedBucket(capacity, maxBucketSize);
indirectArray[i] = new RetainedBucket(capacity, maxBucketSize);
directArray[i] = new RetainedBucket(capacity, maxBucketSize, weakPool);
indirectArray[i] = new RetainedBucket(capacity, maxBucketSize, weakPool);
}

_minCapacity = minCapacity;
Expand Down Expand Up @@ -482,13 +503,13 @@ private static class RetainedBucket
private final Pool<RetainableByteBuffer> _pool;
private final int _capacity;

private RetainedBucket(int capacity, int poolSize)
private RetainedBucket(int capacity, int poolSize, boolean weakPool)
gregw marked this conversation as resolved.
Show resolved Hide resolved
{
if (poolSize <= ConcurrentPool.OPTIMAL_MAX_SIZE)
_pool = new ConcurrentPool<>(ConcurrentPool.StrategyType.THREAD_ID, poolSize, false);
_pool = new ConcurrentPool<>(ConcurrentPool.StrategyType.THREAD_ID, poolSize, e -> 1, weakPool);
else
_pool = new CompoundPool<>(
new ConcurrentPool<>(ConcurrentPool.StrategyType.THREAD_ID, ConcurrentPool.OPTIMAL_MAX_SIZE, false),
new ConcurrentPool<>(ConcurrentPool.StrategyType.THREAD_ID, ConcurrentPool.OPTIMAL_MAX_SIZE, e -> 1, weakPool),
new QueuedPool<>(poolSize - ConcurrentPool.OPTIMAL_MAX_SIZE)
);
_capacity = capacity;
Expand Down Expand Up @@ -566,6 +587,11 @@ public Quadratic(int minCapacity, int maxCapacity, int maxBucketSize)
}

public Quadratic(int minCapacity, int maxCapacity, int maxBucketSize, long maxHeapMemory, long maxDirectMemory)
{
this(minCapacity, maxCapacity, maxBucketSize, maxHeapMemory, maxDirectMemory, false);
}

public Quadratic(int minCapacity, int maxCapacity, int maxBucketSize, long maxHeapMemory, long maxDirectMemory, boolean weakPool)
gregw marked this conversation as resolved.
Show resolved Hide resolved
{
super(minCapacity,
-1,
Expand All @@ -574,7 +600,8 @@ public Quadratic(int minCapacity, int maxCapacity, int maxBucketSize, long maxHe
maxHeapMemory,
maxDirectMemory,
c -> 32 - Integer.numberOfLeadingZeros(c - 1),
i -> 1 << i
i -> 1 << i,
weakPool
);
}
}
Expand Down
Loading