9
0
mirror of https://github.com/VolmitSoftware/Iris.git synced 2025-12-19 15:09:18 +00:00

optimize noise cache

This commit is contained in:
Julian Krings
2025-10-26 13:49:03 +01:00
parent 0bf5da2ca1
commit 38a579453d
2 changed files with 26 additions and 15 deletions

View File

@@ -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<T> {
private final AtomicReferenceArray<T> cache;
private final Entry<T>[] 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<Integer, Integer, T> resolver) {
int key = ((z & 15) * 16) + (x & 15);
T t = cache.get(key);
if (t == null) {
t = resolver.apply(x, z);
cache.set(key, t);
return cache[key].compute(x, z, resolver);
}
private static class Entry<T> {
private volatile T t;
private T compute(int x, int z, Function2<Integer, Integer, T> resolver) {
if (t != null) return t;
synchronized (this) {
if (t == null) t = resolver.apply(x, z);
return t;
}
}
}
}

View File

@@ -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<T> {
private final KCache<Long, ChunkCache2D<T>> chunks;
private final ConcurrentLinkedHashMap<Long, ChunkCache2D<T>> chunks;
private final Function2<Integer, Integer, T> resolver;
public WorldCache2D(Function2<Integer, Integer, T> resolver) {
this.resolver = resolver;
chunks = new KCache<>((x) -> new ChunkCache2D<>(), 1024);
chunks = new ConcurrentLinkedHashMap.Builder<Long, ChunkCache2D<T>>()
.initialCapacity(1024)
.maximumWeightedCapacity(1024)
.concurrencyLevel(32)
.build();
}
public T get(int x, int z) {
ChunkCache2D<T> chunk = chunks.get(Cache.key(x >> 4, z >> 4));
ChunkCache2D<T> 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;
}
}