cached regions in ram should not expand without bound

This commit is contained in:
Leijurv 2018-10-15 15:19:10 -07:00
parent 2f7259714a
commit 3cac37d1a5
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
3 changed files with 61 additions and 3 deletions

View File

@ -319,6 +319,21 @@ public final class CachedRegion implements ICachedRegion {
}
}
public synchronized final CachedChunk mostRecentlyModified() {
CachedChunk recent = null;
for (int x = 0; x < 32; x++) {
for (int z = 0; z < 32; z++) {
if (this.chunks[x][z] == null) {
continue;
}
if (recent == null || this.chunks[x][z].cacheTimestamp > recent.cacheTimestamp) {
recent = this.chunks[x][z];
}
}
}
return recent;
}
/**
* @return The region x coordinate
*/

View File

@ -147,6 +147,7 @@ public final class CachedWorld implements ICachedWorld, Helper {
region.removeExpired();
}
}); // even if we aren't saving to disk, still delete expired old chunks from RAM
prune();
return;
}
long start = System.nanoTime() / 1000000L;
@ -157,6 +158,47 @@ public final class CachedWorld implements ICachedWorld, Helper {
});
long now = System.nanoTime() / 1000000L;
System.out.println("World save took " + (now - start) + "ms");
prune();
}
/**
* Delete regions that are too far from the player
*/
private synchronized void prune() {
BlockPos pruneCenter = guessPosition();
for (CachedRegion region : allRegions()) {
int distX = (region.getX() * 512 + 256) - pruneCenter.getX();
int distZ = (region.getZ() * 512 + 256) - pruneCenter.getZ();
double dist = Math.sqrt(distX * distX + distZ * distZ);
if (dist > 1024) {
logDebug("Deleting cached region " + region.getX() + "," + region.getZ() + " from ram");
cachedRegions.remove(getRegionID(region.getX(), region.getZ()));
}
}
}
/**
* If we are still in this world and dimension, return player feet, otherwise return most recently modified chunk
*/
private BlockPos guessPosition() {
WorldData data = WorldProvider.INSTANCE.getCurrentWorld();
if (data != null && data.getCachedWorld() == this) {
return playerFeet();
}
CachedChunk mostRecentlyModified = null;
for (CachedRegion region : allRegions()) {
CachedChunk ch = region.mostRecentlyModified();
if (ch == null) {
continue;
}
if (mostRecentlyModified == null || mostRecentlyModified.cacheTimestamp < ch.cacheTimestamp) {
mostRecentlyModified = ch;
}
}
if (mostRecentlyModified == null) {
return new BlockPos(0, 0, 0);
}
return new BlockPos(mostRecentlyModified.x * 16 + 8, 0, mostRecentlyModified.z * 16 + 8);
}
private synchronized List<CachedRegion> allRegions() {

View File

@ -21,7 +21,10 @@ import baritone.Baritone;
import baritone.api.pathing.movement.ActionCosts;
import baritone.api.utils.*;
import baritone.pathing.movement.MovementState.MovementTarget;
import baritone.utils.*;
import baritone.utils.BlockStateInterface;
import baritone.utils.Helper;
import baritone.utils.InputOverrideHandler;
import baritone.utils.ToolSet;
import net.minecraft.block.*;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.IBlockState;
@ -35,8 +38,6 @@ import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.chunk.EmptyChunk;
import java.util.Optional;
/**
* Static helpers for cost calculation
*