fix mem leak

This commit is contained in:
dfsek
2021-01-03 19:43:01 -07:00
parent 86d42a03a0
commit c7d3e5294a
3 changed files with 23 additions and 23 deletions
@@ -38,13 +38,14 @@ public class PluginConfig implements ConfigTemplate {
@Value("cache.carver") @Value("cache.carver")
@Default @Default
private int carverCache = 512; private int carverCache = 512;
@Value("cache.structure") @Value("cache.structure")
@Default @Default
private int structureCache = 128; private int structureCache = 128;
@Value("cache.checks") @Value("cache.sampler")
@Default @Default
private int checkCache = 128; private int samplerCache = 512;
@Value("dump-default") @Value("dump-default")
@Default @Default
@@ -95,7 +96,7 @@ public class PluginConfig implements ConfigTemplate {
return structureCache; return structureCache;
} }
public int getCheckCache() { public int getSamplerCache() {
return checkCache; return samplerCache;
} }
} }
@@ -5,45 +5,42 @@ import com.dfsek.terra.api.platform.TerraPlugin;
import com.dfsek.terra.api.platform.world.World; import com.dfsek.terra.api.platform.world.World;
import net.jafama.FastMath; import net.jafama.FastMath;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
public class SamplerCache { public class SamplerCache {
private final Map<Long, Container> cache; private final Map<Long, Container> containerMap;
private final TerraPlugin main; private final TerraPlugin main;
public SamplerCache(TerraPlugin main) { public SamplerCache(TerraPlugin main) {
cache = new HashMap<>(); containerMap = new HashMap<>();
this.main = main; this.main = main;
} }
public Sampler get(World world, int x, int z) { public Sampler get(World world, int x, int z) {
return cache.computeIfAbsent(world.getSeed(), seed -> new Container(world, new LinkedHashMap<Long, Sampler>() { return containerMap.computeIfAbsent(world.getSeed(), seed -> new Container(world)).get(x, z);
@Override
protected boolean removeEldestEntry(Map.Entry<Long, Sampler> eldest) {
return size() > main.getTerraConfig().getCheckCache();
}
})).get(x, z);
} }
public Sampler getChunk(World world, int chunkX, int chunkZ) { public Sampler getChunk(World world, int chunkX, int chunkZ) {
return cache.computeIfAbsent(world.getSeed(), seed -> new Container(world, new LinkedHashMap<Long, Sampler>() { return containerMap.computeIfAbsent(world.getSeed(), seed -> new Container(world)).getChunk(chunkX, chunkZ);
@Override
protected boolean removeEldestEntry(Map.Entry<Long, Sampler> eldest) {
return size() > main.getTerraConfig().getCheckCache();
}
})).getChunk(chunkX, chunkZ);
} }
private class Container { private class Container {
private final World world; private final World world;
private final Map<Long, Sampler> cache; private final TerraWorld terraWorld;
private final Map<Long, Sampler> cache = Collections.synchronizedMap(new LinkedHashMap<Long, Sampler>() {
@Override
protected boolean removeEldestEntry(Map.Entry<Long, Sampler> eldest) {
return this.size() > main.getTerraConfig().getSamplerCache();
}
});
private Container(World world, Map<Long, Sampler> cache) { private Container(World world) {
this.world = world; this.world = world;
this.cache = cache; terraWorld = main.getWorld(world);
} }
public Sampler get(int x, int z) { public Sampler get(int x, int z) {
@@ -54,8 +51,9 @@ public class SamplerCache {
public Sampler getChunk(int cx, int cz) { public Sampler getChunk(int cx, int cz) {
long key = (((long) cx) << 32) | (cz & 0xffffffffL); long key = (((long) cx) << 32) | (cz & 0xffffffffL);
TerraWorld tw = main.getWorld(world); synchronized(cache) {
return cache.computeIfAbsent(key, k -> new Sampler(cx, cz, tw.getGrid(), world, tw.getConfig().getTemplate().getBaseBlend(), tw.getConfig().getTemplate().getElevationBlend())); return cache.computeIfAbsent(key, k -> new Sampler(cx, cz, terraWorld.getGrid(), world, terraWorld.getConfig().getTemplate().getBaseBlend(), terraWorld.getConfig().getTemplate().getElevationBlend()));
}
} }
} }
} }
@@ -6,5 +6,6 @@ biome-search-resolution: 4
cache: cache:
carver: 512 carver: 512
structure: 128 structure: 128
sampler: 512
master-disable: master-disable:
caves: false caves: false