Skip to content

Commit

Permalink
Un-deprecate quadratic pool + made it respect minCapacity + add heuri…
Browse files Browse the repository at this point in the history
…stic for factor

Signed-off-by: Ludovic Orban <[email protected]>
  • Loading branch information
lorban committed Jan 14, 2025
1 parent 4b3dd73 commit c1c1153
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.ConcurrentPool;
import org.eclipse.jetty.util.Pool;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.annotation.ManagedOperation;
Expand Down Expand Up @@ -723,15 +724,12 @@ protected void acquire()
* A variant of the {@link ArrayByteBufferPool} that
* uses buckets of buffers that increase in size by a power of
* 2 (e.g. 1k, 2k, 4k, 8k, etc.).
* @deprecated Usage of {@code Quadratic} is often wasteful of additional space and can increase contention on
* the larger buffers.
*/
@Deprecated(forRemoval = true, since = "12.1.0")
public static class Quadratic extends ArrayByteBufferPool
{
public Quadratic()
{
this(0, -1, Integer.MAX_VALUE);
this(4096, 65536, Integer.MAX_VALUE);
}

public Quadratic(int minCapacity, int maxCapacity, int maxBucketSize)
Expand All @@ -742,13 +740,13 @@ public Quadratic(int minCapacity, int maxCapacity, int maxBucketSize)
public Quadratic(int minCapacity, int maxCapacity, int maxBucketSize, long maxHeapMemory, long maxDirectMemory)
{
super(minCapacity,
-1,
minCapacity <= 0 ? 1 : minCapacity,
maxCapacity,
maxBucketSize,
maxHeapMemory,
maxDirectMemory,
c -> 32 - Integer.numberOfLeadingZeros(c - 1),
i -> 1 << i
c -> 32 - Integer.numberOfLeadingZeros(c - 1) - Integer.numberOfTrailingZeros(Integer.highestOneBit(TypeUtil.ceilToNextPowerOfTwo(minCapacity))),
i -> 1 << i + Integer.numberOfTrailingZeros(Integer.highestOneBit(TypeUtil.ceilToNextPowerOfTwo(minCapacity)))
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,44 +391,37 @@ public void testAcquireRelease()
}

@Test
@Deprecated(forRemoval = true)
public void testQuadraticPool()
public void testQuadraticPoolBucketSizes()
{
ArrayByteBufferPool pool = new ArrayByteBufferPool.Quadratic();

RetainableByteBuffer retain5 = pool.acquire(5, false);
retain5.release();
RetainableByteBuffer retain6 = pool.acquire(6, false);
assertThat(retain6, not(sameInstance(retain5)));
assertThat(retain6.getByteBuffer(), sameInstance(retain5.getByteBuffer()));
retain6.release();
RetainableByteBuffer retain9 = pool.acquire(9, false);
assertThat(retain9, not(sameInstance(retain5)));
retain9.release();

assertThat(pool.acquire(1, false).capacity(), is(1));
assertThat(pool.acquire(2, false).capacity(), is(2));
RetainableByteBuffer b3 = pool.acquire(3, false);
assertThat(b3.capacity(), is(4));
RetainableByteBuffer b4 = pool.acquire(4, false);
assertThat(b4.capacity(), is(4));

int capacity = 4;
while (true)
{
RetainableByteBuffer b = pool.acquire(capacity - 1, false);
assertThat(b.capacity(), Matchers.is(capacity));
b = pool.acquire(capacity, false);
assertThat(b.capacity(), Matchers.is(capacity));

if (capacity >= pool.getMaxCapacity())
break;

b = pool.acquire(capacity + 1, false);
assertThat(b.capacity(), Matchers.is(capacity * 2));

capacity = capacity * 2;
}
ArrayByteBufferPool pool1 = new ArrayByteBufferPool.Quadratic();
String dump1 = pool1.dump();
assertThat(dump1, containsString("direct size=5\n"));
assertThat(dump1, containsString("{capacity=4096,"));
assertThat(dump1, containsString("{capacity=8192,"));
assertThat(dump1, containsString("{capacity=16384,"));
assertThat(dump1, containsString("{capacity=32768,"));
assertThat(dump1, containsString("{capacity=65536,"));

ArrayByteBufferPool pool2 = new ArrayByteBufferPool.Quadratic(100, 800, Integer.MAX_VALUE);
String dump2 = pool2.dump();
assertThat(dump2, containsString("direct size=4\n"));
assertThat(dump2, containsString("{capacity=128,"));
assertThat(dump2, containsString("{capacity=256,"));
assertThat(dump2, containsString("{capacity=512,"));
assertThat(dump2, containsString("{capacity=800,"));

ArrayByteBufferPool pool3 = new ArrayByteBufferPool.Quadratic(0, 200, Integer.MAX_VALUE);
String dump3 = pool3.dump();
assertThat(dump3, containsString("direct size=9\n"));
assertThat(dump3, containsString("{capacity=1,"));
assertThat(dump3, containsString("{capacity=2,"));
assertThat(dump3, containsString("{capacity=4,"));
assertThat(dump3, containsString("{capacity=8,"));
assertThat(dump3, containsString("{capacity=16,"));
assertThat(dump3, containsString("{capacity=32,"));
assertThat(dump3, containsString("{capacity=64,"));
assertThat(dump3, containsString("{capacity=128,"));
assertThat(dump3, containsString("{capacity=200,"));
}

@Test
Expand Down

0 comments on commit c1c1153

Please sign in to comment.