mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-06-17 14:21:08 +00:00
Cache Improvements
This commit is contained in:
+86
-53
@@ -11,54 +11,67 @@ public class CacheSampler implements DerivativeNoiseSampler {
|
|||||||
|
|
||||||
private final NoiseSampler sampler;
|
private final NoiseSampler sampler;
|
||||||
private final LoadingCache<DoubleSeededVector2, Double> cache2D;
|
private final LoadingCache<DoubleSeededVector2, Double> cache2D;
|
||||||
// private final LoadingCache<DoubleSeededVector3, Double> cache3D;
|
private final LoadingCache<DoubleSeededVector3, Double> cache3D;
|
||||||
// private final LoadingCache<DoubleSeededVector2, double[]> cache2DDirv;
|
private final LoadingCache<DoubleSeededVector2, double[]> cache2DDirv;
|
||||||
// private final LoadingCache<DoubleSeededVector3, double[]> cache3DDirv;
|
private final LoadingCache<DoubleSeededVector3, double[]> cache3DDirv;
|
||||||
|
|
||||||
private final ThreadLocal<DoubleSeededVector2> mutable =
|
private final ThreadLocal<DoubleSeededVector2> mutable2 =
|
||||||
ThreadLocal.withInitial(() -> new DoubleSeededVector2(0, 0, 0));
|
ThreadLocal.withInitial(() -> new DoubleSeededVector2(0, 0, 0));
|
||||||
|
|
||||||
|
private final ThreadLocal<DoubleSeededVector3> mutable3 =
|
||||||
|
ThreadLocal.withInitial(() -> new DoubleSeededVector3(0, 0, 0, 0));
|
||||||
|
|
||||||
public CacheSampler(NoiseSampler sampler, int dimensions, int generationThreads) {
|
public CacheSampler(NoiseSampler sampler, int dimensions, int generationThreads) {
|
||||||
this.sampler = sampler;
|
this.sampler = sampler;
|
||||||
// if (dimensions == 2) {
|
if (dimensions == 2) {
|
||||||
this.cache2D = Caffeine
|
this.cache2D = Caffeine
|
||||||
.newBuilder()
|
.newBuilder()
|
||||||
.initialCapacity(0)
|
.initialCapacity(0)
|
||||||
.maximumSize(256L * generationThreads)// 1 full chunk (high res)
|
.maximumSize(256L * generationThreads)// 1 full chunk (high res)
|
||||||
.build(vec -> {
|
.build(vec -> {
|
||||||
mutable.remove();
|
mutable2.remove();
|
||||||
return sampler.noise(vec.seed, vec.x, vec.z);
|
return sampler.noise(vec.seed, vec.x, vec.z);
|
||||||
});
|
});
|
||||||
// }
|
cache3D = null;
|
||||||
// cache3D = null;
|
cache3DDirv = null;
|
||||||
// cache3DDirv = null;
|
mutable3.remove();
|
||||||
// if (DerivativeNoiseSampler.isDifferentiable(sampler)) {
|
if (DerivativeNoiseSampler.isDifferentiable(sampler)) {
|
||||||
// this.cache2DDirv = Caffeine
|
this.cache2DDirv = Caffeine
|
||||||
// .newBuilder()
|
.newBuilder()
|
||||||
// .initialCapacity(0)
|
.initialCapacity(0)
|
||||||
// .maximumSize(256L * generationThreads) // 1 full chunk (high res)
|
.maximumSize(256L * generationThreads) // 1 full chunk (high res)
|
||||||
// .build(vec -> ((DerivativeNoiseSampler) sampler).noised(vec.seed, vec.x, vec.z));
|
.build(vec -> {
|
||||||
// } else {
|
mutable2.remove();
|
||||||
// cache2DDirv = null;
|
return ((DerivativeNoiseSampler) sampler).noised(vec.seed, vec.x, vec.z);
|
||||||
// }
|
});
|
||||||
// } else {
|
} else {
|
||||||
// this.cache3D = Caffeine
|
cache2DDirv = null;
|
||||||
// .newBuilder()
|
}
|
||||||
// .initialCapacity(0)
|
} else {
|
||||||
// .maximumSize(256L * generationThreads) // 1 full chunk (high res)
|
this.cache3D = Caffeine
|
||||||
// .build(vec -> sampler.noise(vec.seed, vec.x, vec.y, vec.z));
|
.newBuilder()
|
||||||
// cache2D = null;
|
.initialCapacity(0)
|
||||||
// cache2DDirv = null;
|
.maximumSize(256L * generationThreads) // 1 full chunk (high res)
|
||||||
// if (DerivativeNoiseSampler.isDifferentiable(sampler)) {
|
.build(vec -> {
|
||||||
// this.cache3DDirv = Caffeine
|
mutable3.remove();
|
||||||
// .newBuilder()
|
return sampler.noise(vec.seed, vec.x, vec.y, vec.z);
|
||||||
// .initialCapacity(0)
|
});
|
||||||
// .maximumSize(256L * generationThreads) // 1 full chunk (high res)
|
cache2D = null;
|
||||||
// .build(vec -> ((DerivativeNoiseSampler) sampler).noised(vec.seed, vec.x, vec.y, vec.z));
|
cache2DDirv = null;
|
||||||
// } else {
|
mutable2.remove();
|
||||||
// cache3DDirv = null;
|
if (DerivativeNoiseSampler.isDifferentiable(sampler)) {
|
||||||
// }
|
this.cache3DDirv = Caffeine
|
||||||
// }
|
.newBuilder()
|
||||||
|
.initialCapacity(0)
|
||||||
|
.maximumSize(256L * generationThreads) // 1 full chunk (high res)
|
||||||
|
.build(vec -> {
|
||||||
|
mutable3.remove();
|
||||||
|
return ((DerivativeNoiseSampler) sampler).noised(vec.seed, vec.x, vec.y, vec.z);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
cache3DDirv = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -68,38 +81,58 @@ public class CacheSampler implements DerivativeNoiseSampler {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double[] noised(long seed, double x, double y) {
|
public double[] noised(long seed, double x, double y) {
|
||||||
// return cache2DDirv.get(new DoubleSeededVector2(x, y, seed));
|
DoubleSeededVector2 mutableKey = mutable2.get();
|
||||||
return null;
|
mutableKey.set(x, y, seed);
|
||||||
|
return cache2DDirv.get(mutableKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double[] noised(long seed, double x, double y, double z) {
|
public double[] noised(long seed, double x, double y, double z) {
|
||||||
// return cache3DDirv.get(new DoubleSeededVector3(x, y, z, seed));
|
DoubleSeededVector3 mutableKey = mutable3.get();
|
||||||
return null;
|
mutableKey.set(x, y, z, seed);
|
||||||
|
return cache3DDirv.get(mutableKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double noise(long seed, double x, double y) {
|
public double noise(long seed, double x, double y) {
|
||||||
// DoubleSeededVector2 vec = new DoubleSeededVector2(x, y, seed);
|
DoubleSeededVector2 mutableKey = mutable2.get();
|
||||||
// if (cache2DDirv != null && cache2DDirv.estimatedSize() != 0) {
|
|
||||||
// return cache2DDirv.get(vec)[0];
|
|
||||||
// }
|
|
||||||
DoubleSeededVector2 mutableKey = mutable.get();
|
|
||||||
mutableKey.set(x, y, seed);
|
mutableKey.set(x, y, seed);
|
||||||
|
if (cache2DDirv != null && cache2DDirv.estimatedSize() != 0) {
|
||||||
|
return cache2DDirv.get(mutableKey)[0];
|
||||||
|
}
|
||||||
return cache2D.get(mutableKey);
|
return cache2D.get(mutableKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double noise(long seed, double x, double y, double z) {
|
public double noise(long seed, double x, double y, double z) {
|
||||||
// DoubleSeededVector3 vec = new DoubleSeededVector3(x, y, z, seed);
|
DoubleSeededVector3 mutableKey = mutable3.get();
|
||||||
// if (cache3DDirv != null && cache3DDirv.estimatedSize() != 0) {
|
mutableKey.set(x, y, z, seed);
|
||||||
// return cache3DDirv.get(vec)[0];
|
if (cache3DDirv != null && cache3DDirv.estimatedSize() != 0) {
|
||||||
// }
|
return cache3DDirv.get(mutableKey)[0];
|
||||||
// return cache3D.get(vec);
|
}
|
||||||
return 0;
|
return cache3D.get(mutableKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
private record DoubleSeededVector3(double x, double y, double z, long seed) {
|
private static class DoubleSeededVector3 {
|
||||||
|
double x;
|
||||||
|
double y;
|
||||||
|
double z;
|
||||||
|
long seed;
|
||||||
|
|
||||||
|
public DoubleSeededVector3(double x, double y, double z, long seed) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.z = z;
|
||||||
|
this.seed = seed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(double x, double y, double z, long seed) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.z = z;
|
||||||
|
this.seed = seed;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if(obj instanceof DoubleSeededVector3 that) {
|
if(obj instanceof DoubleSeededVector3 that) {
|
||||||
@@ -118,7 +151,7 @@ public class CacheSampler implements DerivativeNoiseSampler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private class DoubleSeededVector2 {
|
private static class DoubleSeededVector2 {
|
||||||
|
|
||||||
double x;
|
double x;
|
||||||
double z;
|
double z;
|
||||||
|
|||||||
+58
-7
@@ -21,21 +21,33 @@ public class CachingBiomeProvider implements BiomeProvider, Handle {
|
|||||||
private final LoadingCache<SeededVector3, Biome> cache;
|
private final LoadingCache<SeededVector3, Biome> cache;
|
||||||
private final LoadingCache<SeededVector2, Optional<Biome>> baseCache;
|
private final LoadingCache<SeededVector2, Optional<Biome>> baseCache;
|
||||||
|
|
||||||
|
private final ThreadLocal<SeededVector2> mutable2 =
|
||||||
|
ThreadLocal.withInitial(() -> new SeededVector2(0, 0, 0));
|
||||||
|
|
||||||
|
private final ThreadLocal<SeededVector3> mutable3 =
|
||||||
|
ThreadLocal.withInitial(() -> new SeededVector3(0, 0, 0, 0));
|
||||||
|
|
||||||
protected CachingBiomeProvider(BiomeProvider delegate, int generationThreads) {
|
protected CachingBiomeProvider(BiomeProvider delegate, int generationThreads) {
|
||||||
this.delegate = delegate;
|
this.delegate = delegate;
|
||||||
this.res = delegate.resolution();
|
this.res = delegate.resolution();
|
||||||
int size = generationThreads * 98304;
|
int size = generationThreads * 256 * 384;
|
||||||
this.cache = Caffeine
|
this.cache = Caffeine
|
||||||
.newBuilder()
|
.newBuilder()
|
||||||
.scheduler(Scheduler.disabledScheduler())
|
.scheduler(Scheduler.disabledScheduler())
|
||||||
.initialCapacity(size)
|
.initialCapacity(size)
|
||||||
.maximumSize(size) // 1 full chunk (high res)
|
.maximumSize(size) // 1 full chunk (high res)
|
||||||
.build(vec -> delegate.getBiome(vec.x * res, vec.y * res, vec.z * res, vec.seed));
|
.build(vec -> {
|
||||||
|
mutable3.remove();
|
||||||
|
return delegate.getBiome(vec.x * res, vec.y * res, vec.z * res, vec.seed);
|
||||||
|
});
|
||||||
|
|
||||||
this.baseCache = Caffeine
|
this.baseCache = Caffeine
|
||||||
.newBuilder()
|
.newBuilder()
|
||||||
.maximumSize(256L * generationThreads) // 1 full chunk (high res)
|
.maximumSize(256L * generationThreads) // 1 full chunk (high res)
|
||||||
.build(vec -> delegate.getBaseBiome(vec.x * res, vec.z * res, vec.seed));
|
.build(vec -> {
|
||||||
|
mutable2.remove();
|
||||||
|
return delegate.getBaseBiome(vec.x * res, vec.z * res, vec.seed);
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,12 +58,16 @@ public class CachingBiomeProvider implements BiomeProvider, Handle {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Biome getBiome(int x, int y, int z, long seed) {
|
public Biome getBiome(int x, int y, int z, long seed) {
|
||||||
return cache.get(new SeededVector3(x / res, y / res, z / res, seed));
|
SeededVector3 mutableKey = mutable3.get();
|
||||||
|
mutableKey.set(x, y, z, seed);
|
||||||
|
return cache.get(mutableKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Biome> getBaseBiome(int x, int z, long seed) {
|
public Optional<Biome> getBaseBiome(int x, int z, long seed) {
|
||||||
return baseCache.get(new SeededVector2(x / res, z / res, seed));
|
SeededVector2 mutableKey = mutable2.get();
|
||||||
|
mutableKey.set(x, z, seed);
|
||||||
|
return baseCache.get(mutableKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -64,7 +80,26 @@ public class CachingBiomeProvider implements BiomeProvider, Handle {
|
|||||||
return delegate.resolution();
|
return delegate.resolution();
|
||||||
}
|
}
|
||||||
|
|
||||||
private record SeededVector3(int x, int y, int z, long seed) {
|
private static class SeededVector3 {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int z;
|
||||||
|
long seed;
|
||||||
|
|
||||||
|
public SeededVector3(int x, int y, int z, long seed) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.z = z;
|
||||||
|
this.seed = seed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(int x, int y, int z, long seed) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.z = z;
|
||||||
|
this.seed = seed;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if(obj instanceof SeededVector3 that) {
|
if(obj instanceof SeededVector3 that) {
|
||||||
@@ -83,7 +118,23 @@ public class CachingBiomeProvider implements BiomeProvider, Handle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private record SeededVector2(int x, int z, long seed) {
|
private static class SeededVector2 {
|
||||||
|
int x;
|
||||||
|
int z;
|
||||||
|
long seed;
|
||||||
|
|
||||||
|
public SeededVector2(int x, int z, long seed) {
|
||||||
|
this.x = x;
|
||||||
|
this.z = z;
|
||||||
|
this.seed = seed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(int x, int z, long seed) {
|
||||||
|
this.x = x;
|
||||||
|
this.z = z;
|
||||||
|
this.seed = seed;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if(obj instanceof SeededVector2 that) {
|
if(obj instanceof SeededVector2 that) {
|
||||||
|
|||||||
Reference in New Issue
Block a user