diff --git a/core/src/main/java/art/arcane/iris/core/modules/IrisBlockRegistry.java b/core/src/main/java/art/arcane/iris/core/modules/IrisBlockRegistry.java index e612a56d5..b2d720eec 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/IrisBlockRegistry.java +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisBlockRegistry.java @@ -19,7 +19,7 @@ package art.arcane.iris.core.modules; import art.arcane.iris.core.loader.IrisData; -import art.arcane.iris.engine.data.cache.LazyBoundedCache; +import art.arcane.volmlib.util.cache.LazyBoundedCache; import art.arcane.iris.engine.object.material.IrisBlockData; import art.arcane.iris.engine.object.material.IrisMaterialPalette; import art.arcane.iris.engine.object.prefab.TileData; diff --git a/core/src/main/java/art/arcane/iris/engine/data/cache/LazyBoundedCache.java b/core/src/main/java/art/arcane/iris/engine/data/cache/LazyBoundedCache.java deleted file mode 100644 index 4e1a0bbe2..000000000 --- a/core/src/main/java/art/arcane/iris/engine/data/cache/LazyBoundedCache.java +++ /dev/null @@ -1,52 +0,0 @@ -package art.arcane.iris.engine.data.cache; - -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.Objects; -import java.util.function.Function; - -public final class LazyBoundedCache { - private final int maximumSize; - private transient LinkedHashMap entries; - - public LazyBoundedCache(int maximumSize) { - if (maximumSize <= 0) { - throw new IllegalArgumentException("Maximum cache size must be positive"); - } - this.maximumSize = maximumSize; - } - - public synchronized V computeIfAbsent(K key, Function mappingFunction) { - K requiredKey = Objects.requireNonNull(key, "Cache key"); - Function requiredMapping = Objects.requireNonNull(mappingFunction, "Cache mapping function"); - if (entries != null) { - V cached = entries.get(requiredKey); - if (cached != null) { - return cached; - } - } - - V computed = requiredMapping.apply(requiredKey); - if (computed == null) { - return null; - } - if (entries == null) { - entries = new LinkedHashMap<>(maximumSize, 1F, true); - } - entries.put(requiredKey, computed); - if (entries.size() > maximumSize) { - Iterator iterator = entries.keySet().iterator(); - iterator.next(); - iterator.remove(); - } - return computed; - } - - synchronized boolean isInitialized() { - return entries != null; - } - - synchronized int size() { - return entries == null ? 0 : entries.size(); - } -} diff --git a/core/src/test/java/art/arcane/iris/engine/data/cache/LazyBoundedCacheTest.java b/core/src/test/java/art/arcane/iris/engine/data/cache/LazyBoundedCacheTest.java deleted file mode 100644 index 9bd86afff..000000000 --- a/core/src/test/java/art/arcane/iris/engine/data/cache/LazyBoundedCacheTest.java +++ /dev/null @@ -1,139 +0,0 @@ -package art.arcane.iris.engine.data.cache; - -import org.junit.Test; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; - -public class LazyBoundedCacheTest { - @Test - public void backingStorageIsLazy() { - LazyBoundedCache cache = new LazyBoundedCache<>(8); - - assertFalse(cache.isInitialized()); - assertEquals(0, cache.size()); - assertNull(cache.computeIfAbsent("missing", ignored -> null)); - assertFalse(cache.isInitialized()); - - Object expected = new Object(); - assertSame(expected, cache.computeIfAbsent("present", ignored -> expected)); - assertTrue(cache.isInitialized()); - assertEquals(1, cache.size()); - } - - @Test - public void capacityUsesAccessOrder() { - LazyBoundedCache cache = new LazyBoundedCache<>(8); - Object first = new Object(); - cache.computeIfAbsent(0, ignored -> first); - for (int key = 1; key < 8; key++) { - cache.computeIfAbsent(key, ignored -> new Object()); - } - - assertSame(first, cache.computeIfAbsent(0, ignored -> new Object())); - cache.computeIfAbsent(8, ignored -> new Object()); - - AtomicInteger reloads = new AtomicInteger(); - cache.computeIfAbsent(1, ignored -> { - reloads.incrementAndGet(); - return new Object(); - }); - assertEquals(1, reloads.get()); - assertEquals(8, cache.size()); - assertSame(first, cache.computeIfAbsent(0, ignored -> new Object())); - } - - @Test - public void concurrentSameKeyComputesOnce() throws Exception { - int threadCount = 16; - LazyBoundedCache cache = new LazyBoundedCache<>(8); - CountDownLatch start = new CountDownLatch(1); - CountDownLatch resolverEntered = new CountDownLatch(1); - CountDownLatch releaseResolver = new CountDownLatch(1); - AtomicInteger computations = new AtomicInteger(); - Object expected = new Object(); - ExecutorService executor = Executors.newFixedThreadPool(threadCount); - List> futures = new ArrayList<>(threadCount); - try { - for (int index = 0; index < threadCount; index++) { - futures.add(executor.submit(() -> { - start.await(); - return cache.computeIfAbsent("shared", ignored -> { - computations.incrementAndGet(); - resolverEntered.countDown(); - await(releaseResolver); - return expected; - }); - })); - } - start.countDown(); - assertTrue(resolverEntered.await(5L, TimeUnit.SECONDS)); - releaseResolver.countDown(); - for (Future future : futures) { - assertSame(expected, future.get(5L, TimeUnit.SECONDS)); - } - } finally { - releaseResolver.countDown(); - executor.shutdownNow(); - assertTrue(executor.awaitTermination(5L, TimeUnit.SECONDS)); - } - - assertEquals(1, computations.get()); - assertEquals(1, cache.size()); - } - - @Test - public void keysRetainIdentityEqualitySemantics() { - Object firstIdentity = new String("same"); - Object secondIdentity = new String("same"); - LazyBoundedCache cache = new LazyBoundedCache<>(8); - Object firstValue = new Object(); - - assertSame(firstValue, cache.computeIfAbsent(new IdentityKey(firstIdentity), ignored -> firstValue)); - assertSame(firstValue, cache.computeIfAbsent(new IdentityKey(firstIdentity), ignored -> new Object())); - assertNotSame(firstValue, cache.computeIfAbsent(new IdentityKey(secondIdentity), ignored -> new Object())); - assertEquals(2, cache.size()); - } - - private static void await(CountDownLatch latch) { - try { - if (!latch.await(5L, TimeUnit.SECONDS)) { - throw new IllegalStateException("Timed out waiting for cache test latch"); - } - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new IllegalStateException(e); - } - } - - private static final class IdentityKey { - private final Object identity; - - private IdentityKey(Object identity) { - this.identity = identity; - } - - @Override - public boolean equals(Object other) { - return other instanceof IdentityKey key && identity == key.identity; - } - - @Override - public int hashCode() { - return System.identityHashCode(identity); - } - } -}