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