mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-04-05 07:16:22 +00:00
3x
This commit is contained in:
27
src/main/java/com/volmit/iris/util/cache/ChunkCache2D.java
vendored
Normal file
27
src/main/java/com/volmit/iris/util/cache/ChunkCache2D.java
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.volmit.iris.util.cache;
|
||||
|
||||
import com.volmit.iris.util.data.ChunkCache;
|
||||
import com.volmit.iris.util.function.Function2;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReferenceArray;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ChunkCache2D<T> {
|
||||
private final AtomicReferenceArray<T> cache;
|
||||
|
||||
public ChunkCache2D() {
|
||||
this.cache = new AtomicReferenceArray<>(256);
|
||||
}
|
||||
|
||||
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 t;
|
||||
}
|
||||
}
|
||||
34
src/main/java/com/volmit/iris/util/cache/WorldCache2D.java
vendored
Normal file
34
src/main/java/com/volmit/iris/util/cache/WorldCache2D.java
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
package com.volmit.iris.util.cache;
|
||||
|
||||
import com.volmit.iris.engine.data.cache.Cache;
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
import com.volmit.iris.util.context.ChunkedDataCache;
|
||||
import com.volmit.iris.util.data.KCache;
|
||||
import com.volmit.iris.util.function.Function2;
|
||||
import com.volmit.iris.util.mantle.Mantle;
|
||||
import com.volmit.iris.util.scheduling.ChronoLatch;
|
||||
import it.unimi.dsi.fastutil.longs.Long2LongMaps;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class WorldCache2D<T> {
|
||||
private final KCache<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);
|
||||
}
|
||||
|
||||
public T get(int x, int z) {
|
||||
ChunkCache2D<T> chunk = chunks.get(Cache.key(x >> 4, z >> 4));
|
||||
return chunk.get(x, z, resolver);
|
||||
}
|
||||
|
||||
public long getSize() {
|
||||
return chunks.getSize() * 256L;
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,10 @@ import com.github.benmanes.caffeine.cache.LoadingCache;
|
||||
import com.volmit.iris.engine.framework.MeteredCache;
|
||||
import com.volmit.iris.util.math.RollingSequence;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.TemporalUnit;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class KCache<K, V> implements MeteredCache {
|
||||
private final long max;
|
||||
private CacheLoader<K, V> loader;
|
||||
@@ -46,7 +50,6 @@ public class KCache<K, V> implements MeteredCache {
|
||||
return Caffeine
|
||||
.newBuilder()
|
||||
.maximumSize(max)
|
||||
.softValues()
|
||||
.initialCapacity((int) (max))
|
||||
.build((k) -> loader == null ? null : loader.load(k));
|
||||
}
|
||||
|
||||
@@ -65,8 +65,7 @@ public class ChunkDataHunkHolder extends AtomicHunk<BlockData> {
|
||||
for(int k = 0; k < getDepth(); k++) {
|
||||
BlockData b = super.getRaw(j, i, k);
|
||||
|
||||
if(b != null)
|
||||
{
|
||||
if(b != null) {
|
||||
chunk.setBlock(j, i + chunk.getMinHeight(), k, b);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,12 +18,14 @@
|
||||
|
||||
package com.volmit.iris.util.hunk.view;
|
||||
|
||||
import com.volmit.iris.util.data.B;
|
||||
import com.volmit.iris.util.hunk.Hunk;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.generator.ChunkGenerator.ChunkData;
|
||||
|
||||
@SuppressWarnings("ClassCanBeRecord")
|
||||
public class ChunkDataHunkView implements Hunk<BlockData> {
|
||||
private static final BlockData AIR = B.getAir();
|
||||
private final ChunkData chunk;
|
||||
|
||||
public ChunkDataHunkView(ChunkData chunk) {
|
||||
@@ -54,17 +56,44 @@ public class ChunkDataHunkView implements Hunk<BlockData> {
|
||||
chunk.setRegion(x1, y1 + chunk.getMinHeight(), z1, x2, y2 + chunk.getMinHeight(), z2, t);
|
||||
}
|
||||
|
||||
|
||||
public BlockData get(int x, int y, int z) {
|
||||
return getRaw(x, y, z);
|
||||
}
|
||||
|
||||
public void set(int x, int y, int z, BlockData t) {
|
||||
setRaw(x, y, z, t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRaw(int x, int y, int z, BlockData t) {
|
||||
if(t == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
chunk.setBlock(x, y + chunk.getMinHeight(), z, t);
|
||||
try {
|
||||
|
||||
chunk.setBlock(x, y + chunk.getMinHeight(), z, t);
|
||||
}
|
||||
|
||||
catch(Throwable ignored)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockData getRaw(int x, int y, int z) {
|
||||
return chunk.getBlockData(x, y + chunk.getMinHeight(), z);
|
||||
try {
|
||||
|
||||
return chunk.getBlockData(x, y + chunk.getMinHeight(), z);
|
||||
}
|
||||
|
||||
catch(Throwable e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return AIR;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.volmit.iris.core.service.PreservationSVC;
|
||||
import com.volmit.iris.engine.data.cache.Cache;
|
||||
import com.volmit.iris.engine.framework.Engine;
|
||||
import com.volmit.iris.engine.framework.MeteredCache;
|
||||
import com.volmit.iris.util.cache.WorldCache2D;
|
||||
import com.volmit.iris.util.data.KCache;
|
||||
import com.volmit.iris.util.hunk.Hunk;
|
||||
import com.volmit.iris.util.hunk.storage.ArrayHunk;
|
||||
@@ -31,7 +32,7 @@ import com.volmit.iris.util.stream.ProceduralStream;
|
||||
|
||||
public class CachedStream2D<T> extends BasicStream<T> implements ProceduralStream<T>, MeteredCache {
|
||||
private final ProceduralStream<T> stream;
|
||||
private final KCache<Long, T> cache;
|
||||
private final WorldCache2D<T> cache;
|
||||
private final Engine engine;
|
||||
private boolean chunked = true;
|
||||
|
||||
@@ -39,7 +40,7 @@ public class CachedStream2D<T> extends BasicStream<T> implements ProceduralStrea
|
||||
super();
|
||||
this.stream = stream;
|
||||
this.engine = engine;
|
||||
cache = new KCache<>(k -> stream.get(Cache.keyX(k), Cache.keyZ(k)), size);
|
||||
cache = new WorldCache2D<>(stream::get);
|
||||
Iris.service(PreservationSVC.class).registerCache(this);
|
||||
}
|
||||
|
||||
@@ -56,7 +57,7 @@ public class CachedStream2D<T> extends BasicStream<T> implements ProceduralStrea
|
||||
@Override
|
||||
public T get(double x, double z) {
|
||||
//return stream.get(x, z);
|
||||
return cache.get(Cache.key((int) x, (int) z));
|
||||
return cache.get((int) x, (int) z);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -71,12 +72,12 @@ public class CachedStream2D<T> extends BasicStream<T> implements ProceduralStrea
|
||||
|
||||
@Override
|
||||
public KCache<?, ?> getRawCache() {
|
||||
return cache;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMaxSize() {
|
||||
return cache.getMaxSize();
|
||||
return 256 * 32;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -25,7 +25,11 @@ public class ContextInjectingStream<T> extends BasicStream<T> {
|
||||
ChunkContext chunkContext = context.getChunkContext();
|
||||
|
||||
if(chunkContext != null && (int)x >> 4 == chunkContext.getX() >> 4 && (int)z >> 4 == chunkContext.getZ() >> 4) {
|
||||
return contextAccessor.apply(chunkContext, (int)x&15, (int)z&15);
|
||||
T t = contextAccessor.apply(chunkContext, (int)x&15, (int)z&15);
|
||||
|
||||
if(t != null) {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user