2025-03-11 00:23:54 +01:00
|
|
|
package game.worldgen.layer;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2025-03-25 11:31:48 +01:00
|
|
|
import game.collect.Lists;
|
2025-03-11 00:23:54 +01:00
|
|
|
|
|
|
|
public class IntCache
|
|
|
|
{
|
|
|
|
private static int intCacheSize = 256;
|
|
|
|
private static List<int[]> freeSmallArrays = Lists.<int[]>newArrayList();
|
|
|
|
private static List<int[]> inUseSmallArrays = Lists.<int[]>newArrayList();
|
|
|
|
private static List<int[]> freeLargeArrays = Lists.<int[]>newArrayList();
|
|
|
|
private static List<int[]> inUseLargeArrays = Lists.<int[]>newArrayList();
|
|
|
|
|
|
|
|
public static synchronized int[] getIntCache(int p_76445_0_)
|
|
|
|
{
|
|
|
|
if (p_76445_0_ <= 256)
|
|
|
|
{
|
|
|
|
if (freeSmallArrays.isEmpty())
|
|
|
|
{
|
|
|
|
int[] aint4 = new int[256];
|
|
|
|
inUseSmallArrays.add(aint4);
|
|
|
|
return aint4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int[] aint3 = (int[])freeSmallArrays.remove(freeSmallArrays.size() - 1);
|
|
|
|
inUseSmallArrays.add(aint3);
|
|
|
|
return aint3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (p_76445_0_ > intCacheSize)
|
|
|
|
{
|
|
|
|
intCacheSize = p_76445_0_;
|
|
|
|
freeLargeArrays.clear();
|
|
|
|
inUseLargeArrays.clear();
|
|
|
|
int[] aint2 = new int[intCacheSize];
|
|
|
|
inUseLargeArrays.add(aint2);
|
|
|
|
return aint2;
|
|
|
|
}
|
|
|
|
else if (freeLargeArrays.isEmpty())
|
|
|
|
{
|
|
|
|
int[] aint1 = new int[intCacheSize];
|
|
|
|
inUseLargeArrays.add(aint1);
|
|
|
|
return aint1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int[] aint = (int[])freeLargeArrays.remove(freeLargeArrays.size() - 1);
|
|
|
|
inUseLargeArrays.add(aint);
|
|
|
|
return aint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark all pre-allocated arrays as available for re-use by moving them to the appropriate free lists.
|
|
|
|
*/
|
|
|
|
public static synchronized void resetIntCache()
|
|
|
|
{
|
|
|
|
if (!freeLargeArrays.isEmpty())
|
|
|
|
{
|
|
|
|
freeLargeArrays.remove(freeLargeArrays.size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!freeSmallArrays.isEmpty())
|
|
|
|
{
|
|
|
|
freeSmallArrays.remove(freeSmallArrays.size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
freeLargeArrays.addAll(inUseLargeArrays);
|
|
|
|
freeSmallArrays.addAll(inUseSmallArrays);
|
|
|
|
inUseLargeArrays.clear();
|
|
|
|
inUseSmallArrays.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a human-readable string that indicates the sizes of all the cache fields. Basically a synchronized static
|
|
|
|
* toString.
|
|
|
|
*/
|
|
|
|
public static synchronized String getCacheSizes()
|
|
|
|
{
|
|
|
|
return "cache: " + freeLargeArrays.size() + ", tcache: " + freeSmallArrays.size() + ", allocated: " + inUseLargeArrays.size() + ", tallocated: " + inUseSmallArrays.size();
|
|
|
|
}
|
|
|
|
}
|