configurable cache sizes

This commit is contained in:
dfsek
2021-01-13 23:51:23 -07:00
parent 244f0fba11
commit 8fd3530653
6 changed files with 69 additions and 53 deletions
@@ -2,6 +2,7 @@ package com.dfsek.terra.biome;
import com.dfsek.tectonic.exception.ConfigException;
import com.dfsek.terra.api.math.vector.Vector2;
import com.dfsek.terra.api.platform.TerraPlugin;
import com.dfsek.terra.api.world.biome.TerraBiome;
import com.dfsek.terra.biome.pipeline.BiomeHolder;
import com.dfsek.terra.biome.pipeline.BiomePipeline;
@@ -14,37 +15,37 @@ import org.jetbrains.annotations.NotNull;
import java.util.concurrent.ExecutionException;
public class StandardBiomeProvider implements BiomeProvider {
private final BiomePipeline pipeline;
private final LoadingCache<Vector2, BiomeHolder> holderCache = CacheBuilder.newBuilder()
.maximumSize(64)
.build(
new CacheLoader<Vector2, BiomeHolder>() {
@Override
public BiomeHolder load(@NotNull Vector2 key) {
return pipeline.getBiomes(key.getBlockX(), key.getBlockZ());
}
}
);
private final LoadingCache<Vector2, TerraBiome> biomeCache = CacheBuilder.newBuilder()
.maximumSize(1024)
.build(
new CacheLoader<Vector2, TerraBiome>() {
@Override
public TerraBiome load(@NotNull Vector2 key) throws ExecutionException {
int x = FastMath.floorToInt(key.getX());
int z = FastMath.floorToInt(key.getZ());
x /= resolution;
z /= resolution;
int fdX = FastMath.floorDiv(x, pipeline.getSize());
int fdZ = FastMath.floorDiv(z, pipeline.getSize());
return holderCache.get(new Vector2(fdX, fdZ)).getBiome(x - fdX * pipeline.getSize(), z - fdZ * pipeline.getSize());
}
}
);
private final LoadingCache<Vector2, BiomeHolder> holderCache;
private final LoadingCache<Vector2, TerraBiome> biomeCache;
private int resolution = 1;
protected StandardBiomeProvider(BiomePipeline pipeline) {
this.pipeline = pipeline;
protected StandardBiomeProvider(BiomePipeline pipeline, TerraPlugin main) {
holderCache = CacheBuilder.newBuilder()
.maximumSize(main == null ? 32 : main.getTerraConfig().getProviderCache())
.build(
new CacheLoader<Vector2, BiomeHolder>() {
@Override
public BiomeHolder load(@NotNull Vector2 key) {
return pipeline.getBiomes(key.getBlockX(), key.getBlockZ());
}
}
);
biomeCache = CacheBuilder.newBuilder()
.maximumSize(main == null ? 512 : main.getTerraConfig().getBiomeCache())
.build(
new CacheLoader<Vector2, TerraBiome>() {
@Override
public TerraBiome load(@NotNull Vector2 key) throws ExecutionException {
int x = FastMath.floorToInt(key.getX());
int z = FastMath.floorToInt(key.getZ());
x /= resolution;
z /= resolution;
int fdX = FastMath.floorDiv(x, pipeline.getSize());
int fdZ = FastMath.floorDiv(z, pipeline.getSize());
return holderCache.get(new Vector2(fdX, fdZ)).getBiome(x - fdX * pipeline.getSize(), z - fdZ * pipeline.getSize());
}
}
);
}
@Override
@@ -70,15 +71,17 @@ public class StandardBiomeProvider implements BiomeProvider {
public static final class StandardBiomeProviderBuilder implements BiomeProviderBuilder {
private final ExceptionalFunction<Long, BiomePipeline> pipelineBuilder;
private final TerraPlugin main;
public StandardBiomeProviderBuilder(ExceptionalFunction<Long, BiomePipeline> pipelineBuilder) {
public StandardBiomeProviderBuilder(ExceptionalFunction<Long, BiomePipeline> pipelineBuilder, TerraPlugin main) {
this.pipelineBuilder = pipelineBuilder;
this.main = main;
}
@Override
public StandardBiomeProvider build(long seed) {
try {
return new StandardBiomeProvider(pipelineBuilder.apply(seed));
return new StandardBiomeProvider(pipelineBuilder.apply(seed), main);
} catch(ConfigException e) {
throw new RuntimeException(e);
}