Optimize NibbleArray to use pooled buffers

Massively reduces memory allocation of 2048 byte buffers by using
an object pool for these.
This commit is contained in:
Aikar
2020-05-07 01:32:02 -04:00
parent bb418b5532
commit 9204e8c641
3 changed files with 255 additions and 20 deletions

View File

@@ -2090,18 +2090,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+
+public final class PooledObjects<E> {
+
+ public static final PooledObjects<MutableInt> POOLED_MUTABLE_INTEGERS = new PooledObjects<>(new PooledObjectHandler<MutableInt>() {
+ @Override
+ public MutableInt createNew() {
+ return new MutableInt();
+ }
+
+ @Override
+ public void onAcquire(final MutableInt value) {}
+
+ @Override
+ public void onRelease(final MutableInt value) {}
+ }, 200, -1);
+ public static final PooledObjects<MutableInt> POOLED_MUTABLE_INTEGERS = new PooledObjects<>(MutableInt::new, 200, -1);
+
+ private final PooledObjectHandler<E> handler;
+ private final int maxPoolSize;
@@ -2171,16 +2160,16 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ /** This object is restricted from interacting with any pool */
+ static interface PooledObjectHandler<E> {
+ public static interface PooledObjectHandler<E> {
+
+ /**
+ * Must return a non-null object
+ */
+ E createNew();
+
+ void onAcquire(final E value);
+ default void onAcquire(final E value) {}
+
+ void onRelease(final E value);
+ default void onRelease(final E value) {}
+ }
+
+ protected static class IsolatedPool<E> {