Recycle CSSNodes

Summary: Adds a pool to recycle CSSNodes within UIManager. A follow-up diff will hook this up to a memory pressure listener to drop the pool on memory pressure.

Reviewed By: emilsjolander

Differential Revision: D4189532

fbshipit-source-id: 46583546f71a8c59853e1dd124de31657b3c617b
This commit is contained in:
Andy Street
2016-11-21 09:11:29 -08:00
committed by Facebook Github Bot
parent 7c91f894ba
commit bd8745b1fd
5 changed files with 107 additions and 10 deletions
@@ -0,0 +1,48 @@
// Copyright 2004-present Facebook. All Rights Reserved.
package com.facebook.react.common;
import android.support.v4.util.Pools;
/**
* Like {@link android.support.v4.util.Pools.SynchronizedPool} with the option to clear the pool
* (e.g. on memory pressure).
*/
public class ClearableSynchronizedPool<T> implements Pools.Pool<T> {
private final Object[] mPool;
private int mSize = 0;
public ClearableSynchronizedPool(int maxSize) {
mPool = new Object[maxSize];
}
@Override
public synchronized T acquire() {
if (mSize == 0) {
return null;
}
mSize--;
final int lastIndex = mSize;
T toReturn = (T) mPool[lastIndex];
mPool[lastIndex] = null;
return toReturn;
}
@Override
public synchronized boolean release(T obj) {
if (mSize == mPool.length) {
return false;
}
mPool[mSize] = obj;
mSize++;
return true;
}
public synchronized void clear() {
for (int i = 0; i < mSize; i++) {
mPool[i] = null;
}
mSize = 0;
}
}