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 Nov 29, 2021
1 parent 377d0d1 commit aa09b8b
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 @@ -104,13 +104,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 @@ -197,7 +197,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 @@ -209,11 +209,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 @@ -185,7 +185,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 @@ -27,6 +27,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 @@ -158,7 +159,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 @@ -167,7 +168,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 aa09b8b

Please sign in to comment.