| 1 | package net.minecraft.world.gen.layer;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 |
|
| 6 | public class IntCache
|
| 7 | {
|
| 8 | private static int intCacheSize = 256;
|
| 9 | private static List<int[]> freeSmallArrays = Lists.<int[]>newArrayList();
|
| 10 | private static List<int[]> inUseSmallArrays = Lists.<int[]>newArrayList();
|
| 11 | private static List<int[]> freeLargeArrays = Lists.<int[]>newArrayList();
|
| 12 | private static List<int[]> inUseLargeArrays = Lists.<int[]>newArrayList();
|
| 13 |
|
| 14 | public static synchronized int[] getIntCache(int p_76445_0_)
|
| 15 | {
|
| 16 | if (p_76445_0_ <= 256)
|
| 17 | {
|
| 18 | if (freeSmallArrays.isEmpty())
|
| 19 | {
|
| 20 | int[] aint4 = new int[256];
|
| 21 | inUseSmallArrays.add(aint4);
|
| 22 | return aint4;
|
| 23 | }
|
| 24 | else
|
| 25 | {
|
| 26 | int[] aint3 = (int[])freeSmallArrays.remove(freeSmallArrays.size() - 1);
|
| 27 | inUseSmallArrays.add(aint3);
|
| 28 | return aint3;
|
| 29 | }
|
| 30 | }
|
| 31 | else if (p_76445_0_ > intCacheSize)
|
| 32 | {
|
| 33 | intCacheSize = p_76445_0_;
|
| 34 | freeLargeArrays.clear();
|
| 35 | inUseLargeArrays.clear();
|
| 36 | int[] aint2 = new int[intCacheSize];
|
| 37 | inUseLargeArrays.add(aint2);
|
| 38 | return aint2;
|
| 39 | }
|
| 40 | else if (freeLargeArrays.isEmpty())
|
| 41 | {
|
| 42 | int[] aint1 = new int[intCacheSize];
|
| 43 | inUseLargeArrays.add(aint1);
|
| 44 | return aint1;
|
| 45 | }
|
| 46 | else
|
| 47 | {
|
| 48 | int[] aint = (int[])freeLargeArrays.remove(freeLargeArrays.size() - 1);
|
| 49 | inUseLargeArrays.add(aint);
|
| 50 | return aint;
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | /**
|
| 55 | * Mark all pre-allocated arrays as available for re-use by moving them to the appropriate free lists.
|
| 56 | */
|
| 57 | public static synchronized void resetIntCache()
|
| 58 | {
|
| 59 | if (!freeLargeArrays.isEmpty())
|
| 60 | {
|
| 61 | freeLargeArrays.remove(freeLargeArrays.size() - 1);
|
| 62 | }
|
| 63 |
|
| 64 | if (!freeSmallArrays.isEmpty())
|
| 65 | {
|
| 66 | freeSmallArrays.remove(freeSmallArrays.size() - 1);
|
| 67 | }
|
| 68 |
|
| 69 | freeLargeArrays.addAll(inUseLargeArrays);
|
| 70 | freeSmallArrays.addAll(inUseSmallArrays);
|
| 71 | inUseLargeArrays.clear();
|
| 72 | inUseSmallArrays.clear();
|
| 73 | }
|
| 74 |
|
| 75 | /**
|
| 76 | * Gets a human-readable string that indicates the sizes of all the cache fields. Basically a synchronized static
|
| 77 | * toString.
|
| 78 | */
|
| 79 | public static synchronized String getCacheSizes()
|
| 80 | {
|
| 81 | return "cache: " + freeLargeArrays.size() + ", tcache: " + freeSmallArrays.size() + ", allocated: " + inUseLargeArrays.size() + ", tallocated: " + inUseSmallArrays.size();
|
| 82 | }
|
| 83 | }
|
| 84 |
|