Skip to content

Commit

Permalink
Issue #6974 - allow empty buffers to be pooled
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Dec 3, 2021
1 parent 6fd2213 commit 51dd155
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ public ArrayByteBufferPool(int minCapacity, int factor, int maxCapacity, int max
_minCapacity = minCapacity;

// Initialize all buckets in constructor and never modify the array again.
int length = bucketFor(maxCapacity);
int length = bucketFor(maxCapacity) + 1;
_direct = new ByteBufferPool.Bucket[length];
_indirect = new ByteBufferPool.Bucket[length];
for (int i = 0; i < length; i++)
{
_direct[i] = newBucket(i + 1, true);
_indirect[i] = newBucket(i + 1, false);
_direct[i] = newBucket(i, true);
_indirect[i] = newBucket(i, false);
}
}

Expand Down Expand Up @@ -202,7 +202,7 @@ protected void releaseMemory(boolean direct)

protected int bucketFor(int capacity)
{
return Math.max((int)Math.ceil((double)capacity / getCapacityFactor()), 1);
return (int)Math.ceil((double)capacity / getCapacityFactor());
}

protected int capacityFor(int bucket)
Expand All @@ -214,11 +214,11 @@ private ByteBufferPool.Bucket bucketFor(int capacity, boolean direct)
{
if (capacity < _minCapacity)
return null;
int index = bucketFor(capacity) - 1;
if (index >= _direct.length)
int bucket = bucketFor(capacity);
if (bucket >= _direct.length)
return null;
Bucket[] buckets = bucketsFor(direct);
return buckets[index];
return buckets[bucket];
}

@ManagedAttribute("The number of pooled direct ByteBuffers")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ protected void releaseMemory(boolean direct)

protected int bucketFor(int capacity)
{
return Math.max((int)Math.ceil((double)capacity / getCapacityFactor()), 1);
return (int)Math.ceil((double)capacity / getCapacityFactor());
}

protected int capacityFor(int bucket)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -163,7 +164,7 @@ public void testMaxMemory()
// Now the oldest buffer should be gone and we have: 1+2x2+3=8
long memory = bufferPool.getMemory(true);
assertThat(memory, lessThan((long)maxMemory));
assertTrue(buckets.get(4).isEmpty());
assertNull(buckets.get(4));

// Create and release a large buffer.
// Max memory is exceeded and buckets 3 and 1 are cleared.
Expand All @@ -172,7 +173,7 @@ public void testMaxMemory()
bufferPool.release(buffer);
memory = bufferPool.getMemory(true);
assertThat(memory, lessThanOrEqualTo((long)maxMemory));
assertTrue(buckets.get(1).isEmpty());
assertTrue(buckets.get(3).isEmpty());
assertNull(buckets.get(1));
assertNull(buckets.get(3));
}
}

0 comments on commit 51dd155

Please sign in to comment.