From 38a579453d26f0f8ae78032db2f3eec6c7fd0a24 Mon Sep 17 00:00:00 2001 From: Julian Krings Date: Sun, 26 Oct 2025 13:49:03 +0100 Subject: [PATCH] optimize noise cache --- .../volmit/iris/util/cache/ChunkCache2D.java | 27 ++++++++++++------- .../volmit/iris/util/cache/WorldCache2D.java | 14 ++++++---- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/com/volmit/iris/util/cache/ChunkCache2D.java b/core/src/main/java/com/volmit/iris/util/cache/ChunkCache2D.java index f048926c3..7fba65e45 100644 --- a/core/src/main/java/com/volmit/iris/util/cache/ChunkCache2D.java +++ b/core/src/main/java/com/volmit/iris/util/cache/ChunkCache2D.java @@ -2,24 +2,31 @@ package com.volmit.iris.util.cache; import com.volmit.iris.util.function.Function2; -import java.util.concurrent.atomic.AtomicReferenceArray; - public class ChunkCache2D { - private final AtomicReferenceArray cache; + private final Entry[] cache; + @SuppressWarnings({"unchecked"}) public ChunkCache2D() { - this.cache = new AtomicReferenceArray<>(256); + this.cache = new Entry[256]; + for (int i = 0; i < cache.length; i++) { + cache[i] = new Entry<>(); + } } public T get(int x, int z, Function2 resolver) { int key = ((z & 15) * 16) + (x & 15); - T t = cache.get(key); + return cache[key].compute(x, z, resolver); + } + + private static class Entry { + private volatile T t; - if (t == null) { - t = resolver.apply(x, z); - cache.set(key, t); + private T compute(int x, int z, Function2 resolver) { + if (t != null) return t; + synchronized (this) { + if (t == null) t = resolver.apply(x, z); + return t; + } } - - return t; } } diff --git a/core/src/main/java/com/volmit/iris/util/cache/WorldCache2D.java b/core/src/main/java/com/volmit/iris/util/cache/WorldCache2D.java index e79b87aeb..8e74b0ab5 100644 --- a/core/src/main/java/com/volmit/iris/util/cache/WorldCache2D.java +++ b/core/src/main/java/com/volmit/iris/util/cache/WorldCache2D.java @@ -1,24 +1,28 @@ package com.volmit.iris.util.cache; +import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap; import com.volmit.iris.engine.data.cache.Cache; -import com.volmit.iris.util.data.KCache; import com.volmit.iris.util.function.Function2; public class WorldCache2D { - private final KCache> chunks; + private final ConcurrentLinkedHashMap> chunks; private final Function2 resolver; public WorldCache2D(Function2 resolver) { this.resolver = resolver; - chunks = new KCache<>((x) -> new ChunkCache2D<>(), 1024); + chunks = new ConcurrentLinkedHashMap.Builder>() + .initialCapacity(1024) + .maximumWeightedCapacity(1024) + .concurrencyLevel(32) + .build(); } public T get(int x, int z) { - ChunkCache2D chunk = chunks.get(Cache.key(x >> 4, z >> 4)); + ChunkCache2D chunk = chunks.computeIfAbsent(Cache.key(x >> 4, z >> 4), $ -> new ChunkCache2D<>()); return chunk.get(x, z, resolver); } public long getSize() { - return chunks.getSize() * 256L; + return chunks.size() * 256L; } }