Skip to content

Commit

Permalink
Fixes #1262: Add diagnostic method pooledCount() in RecyclerPool (#1263)
Browse files Browse the repository at this point in the history
(backport to 2.17.1 to help testing)
  • Loading branch information
cowtowncoder committed Apr 18, 2024
1 parent c73bde2 commit 33c4260
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 25 deletions.
61 changes: 55 additions & 6 deletions src/main/java/com/fasterxml/jackson/core/util/RecyclerPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ default boolean clear() {
return false;
}

/**
* Diagnostic method for obtaining an estimate of number of pooled items
* this pool contains, available for recycling.
* Note that in addition to this information possibly not being available
* (denoted by return value of {@code -1}) even when available this may be
* just an approximation.
*<p>
* Default method implementation simply returns {@code -1} and is meant to be
* overridden by concrete sub-classes.
*
* @return Number of pooled entries available from this pool, if available;
* {@code -1} if not.
*
* @since 2.18
*/
default int pooledCount() {
return -1;
}

/*
/**********************************************************************
/* Partial/base RecyclerPool implementations
Expand Down Expand Up @@ -150,6 +169,12 @@ public void releasePooled(P pooled) {
// nothing to do, relies on ThreadLocal
}

// No way to actually even estimate...
@Override
public int pooledCount() {
return -1;
}

// Due to use of ThreadLocal no tracking available; cannot clear
@Override
public boolean clear() {
Expand Down Expand Up @@ -181,6 +206,11 @@ public void releasePooled(P pooled) {
// nothing to do, there is no underlying pool
}

@Override
public int pooledCount() {
return 0;
}

/**
* Although no pooling occurs, we consider clearing to succeed,
* so returns always {@code true}.
Expand Down Expand Up @@ -262,6 +292,11 @@ public void releasePooled(P pooled) {
pool.offerLast(pooled);
}

@Override
public int pooledCount() {
return pool.size();
}

@Override
public boolean clear() {
pool.clear();
Expand Down Expand Up @@ -322,13 +357,13 @@ public void releasePooled(P pooled) {
}
}

protected static class Node<P> {
final P value;
Node<P> next;

Node(P value) {
this.value = value;
@Override
public int pooledCount() {
int count = 0;
for (Node<P> curr = head.get(); curr != null; curr = curr.next) {
++count;
}
return count;
}

// Yes, we can clear it
Expand All @@ -337,6 +372,15 @@ public boolean clear() {
head.set(null);
return true;
}

protected static class Node<P> {
final P value;
Node<P> next;

Node(P value) {
this.value = value;
}
}
}

/**
Expand Down Expand Up @@ -385,6 +429,11 @@ public void releasePooled(P pooled) {
pool.offer(pooled);
}

@Override
public int pooledCount() {
return pool.size();
}

@Override
public boolean clear() {
pool.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public void releasePooled(BufferRecycler r) {
bufferRecycler = r;
}

@Override
public int pooledCount() {
return (bufferRecycler == null) ? 0 : 1;
}

@Override
public boolean clear() {
bufferRecycler = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,41 @@ class JsonBufferRecyclersTest extends JUnit5TestBase

@Test
void parserWithThreadLocalPool() throws Exception {
_testParser(JsonRecyclerPools.threadLocalPool());
_testParser(JsonRecyclerPools.threadLocalPool(), -1, -1);
}

@Test
void parserWithNopLocalPool() throws Exception {
_testParser(JsonRecyclerPools.nonRecyclingPool());
_testParser(JsonRecyclerPools.nonRecyclingPool(), 0, 0);
}

@Test
void parserWithDequeuPool() throws Exception {
_testParser(JsonRecyclerPools.newConcurrentDequePool());
_testParser(JsonRecyclerPools.sharedConcurrentDequePool());
_testParser(JsonRecyclerPools.newConcurrentDequePool(), 0, 1);
_testParser(JsonRecyclerPools.sharedConcurrentDequePool(), null, null);
}

@Test
void parserWithLockFreePool() throws Exception {
_testParser(JsonRecyclerPools.newLockFreePool());
_testParser(JsonRecyclerPools.sharedLockFreePool());
_testParser(JsonRecyclerPools.newLockFreePool(), 0, 1);
_testParser(JsonRecyclerPools.sharedLockFreePool(), null, null);
}

@Test
void parserWithBoundedPool() throws Exception {
_testParser(JsonRecyclerPools.newBoundedPool(5));
_testParser(JsonRecyclerPools.sharedBoundedPool());
_testParser(JsonRecyclerPools.newBoundedPool(5), 0, 1);
_testParser(JsonRecyclerPools.sharedBoundedPool(), null, null);
}

private void _testParser(RecyclerPool<BufferRecycler> pool) throws Exception
private void _testParser(RecyclerPool<BufferRecycler> pool,
Integer expSizeBefore, Integer expSizeAfter) throws Exception
{
JsonFactory jsonF = JsonFactory.builder()
.recyclerPool(pool)
.build();
if (expSizeBefore != null) {
assertEquals(expSizeBefore, pool.pooledCount());
}

JsonParser p = jsonF.createParser(a2q("{'a':123,'b':'foobar'}"));

Expand All @@ -62,44 +66,53 @@ private void _testParser(RecyclerPool<BufferRecycler> pool) throws Exception
assertToken(JsonToken.END_OBJECT, p.nextToken());

p.close();

if (expSizeAfter != null) {
assertEquals(expSizeAfter, pool.pooledCount());
}
}

// // Generators with RecyclerPools:

@Test
void generatorWithThreadLocalPool() throws Exception {
_testGenerator(JsonRecyclerPools.threadLocalPool());
_testGenerator(JsonRecyclerPools.threadLocalPool(), -1, -1);
}

@Test
void generatorWithNopLocalPool() throws Exception {
_testGenerator(JsonRecyclerPools.nonRecyclingPool());
_testGenerator(JsonRecyclerPools.nonRecyclingPool(), 0, 0);
}

@Test
void generatorWithDequeuPool() throws Exception {
_testGenerator(JsonRecyclerPools.newConcurrentDequePool());
_testGenerator(JsonRecyclerPools.sharedConcurrentDequePool());
_testGenerator(JsonRecyclerPools.newConcurrentDequePool(), 0, 1);
_testGenerator(JsonRecyclerPools.sharedConcurrentDequePool(), null, null);
}

@Test
void generatorWithLockFreePool() throws Exception {
_testGenerator(JsonRecyclerPools.newLockFreePool());
_testGenerator(JsonRecyclerPools.sharedLockFreePool());
_testGenerator(JsonRecyclerPools.newLockFreePool(), 0, 1);
_testGenerator(JsonRecyclerPools.sharedLockFreePool(), null, null);
}

@Test
void generatorWithBoundedPool() throws Exception {
_testGenerator(JsonRecyclerPools.newBoundedPool(5));
_testGenerator(JsonRecyclerPools.sharedBoundedPool());
_testGenerator(JsonRecyclerPools.newBoundedPool(5), 0, 1);
_testGenerator(JsonRecyclerPools.sharedBoundedPool(), null, null);
}

private void _testGenerator(RecyclerPool<BufferRecycler> pool) throws Exception

private void _testGenerator(RecyclerPool<BufferRecycler> pool,
Integer expSizeBefore, Integer expSizeAfter) throws Exception
{
JsonFactory jsonF = JsonFactory.builder()
.recyclerPool(pool)
.build();

if (expSizeBefore != null) {
assertEquals(expSizeBefore, pool.pooledCount());
}

StringWriter w = new StringWriter();
JsonGenerator g = jsonF.createGenerator(w);

Expand All @@ -110,6 +123,10 @@ private void _testGenerator(RecyclerPool<BufferRecycler> pool) throws Exception

g.close();

if (expSizeAfter != null) {
assertEquals(expSizeAfter, pool.pooledCount());
}

assertEquals(a2q("{'a':-42,'b':'barfoo'}"), w.toString());
}

Expand Down

0 comments on commit 33c4260

Please sign in to comment.