Skip to content

Commit

Permalink
[optimise] Simple fix to pathological char array pool
Browse files Browse the repository at this point in the history
Pool miss is horrible, fix it to just look in one place.

simple fix 2
  • Loading branch information
alanpaxton authored and adamretter committed Dec 1, 2024
1 parent 235e8e1 commit 315accd
Showing 1 changed file with 23 additions and 33 deletions.
56 changes: 23 additions & 33 deletions exist-core/src/main/java/org/exist/util/CharArrayPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

import net.jcip.annotations.ThreadSafe;

import java.util.concurrent.atomic.AtomicInteger;

/**
* A pool for char arrays.
*
Expand All @@ -38,50 +36,42 @@
public class CharArrayPool {

private static final int POOL_SIZE = 128;
private static final int MAX = 128;
private static final ThreadLocal<char[][]> pools_ = new PoolThreadLocal();
private static final AtomicInteger slot_ = new AtomicInteger();
private static final int MIN_SIZE = 8;

private static final ThreadLocal<PoolContent> POOLS = new ThreadLocal<PoolContent>() {
@Override
protected PoolContent initialValue() {
return new PoolContent();
}
};

private CharArrayPool() {
}

public static char[] getCharArray(final int size) {
if (MAX > size) {
final char[][] pool = pools_.get();
for (int i = 0; i < pool.length; i++) {
if (pool[i] != null && pool[i].length == size) {
final char[] b = pool[i];
pool[i] = null;
return b;
}
final PoolContent poolContent = POOLS.get();
if (POOL_SIZE > size) {
final int alloc = Math.max(size, MIN_SIZE);
final char[][] pool = poolContent.pool;
if (pool[alloc] != null) {
final char[] b = POOLS.get().pool[alloc];
pool[alloc] = null;
return b;
}
}
return new char[size];
return new char[Math.max(size, MIN_SIZE)];
}

public static void releaseCharArray(final char[] b) {
if (b == null || b.length > MAX) {
if (b == null || b.length > POOL_SIZE) {
return;
}
final char[][] pool = pools_.get();
for (int i = 0; i < pool.length; i++) {
if (pool[i] == null) {
pool[i] = b;
return;
}
}
final PoolContent poolContent = POOLS.get();
poolContent.pool[b.length] = b;
}

int s = slot_.incrementAndGet();
if (s < 0) {
s = -s;
}
pool[s % pool.length] = b;
private static class PoolContent {
final char[][] pool = new char[POOL_SIZE][];
}

private static final class PoolThreadLocal extends ThreadLocal<char[][]> {
@Override
protected char[][] initialValue() {
return new char[POOL_SIZE][];
}
}
}

0 comments on commit 315accd

Please sign in to comment.