Added Chunk#setEnableCachePacket to improve memory usage when needed

This commit is contained in:
themode 2020-10-15 08:21:13 +02:00
parent 50326b676c
commit 74e37e681b
3 changed files with 36 additions and 5 deletions

View File

@ -65,6 +65,7 @@ public abstract class Chunk implements Viewable, DataContainer {
private boolean shouldGenerate; private boolean shouldGenerate;
// Packet cache // Packet cache
private volatile boolean enableCachePacket;
protected volatile boolean packetUpdated; protected volatile boolean packetUpdated;
private ByteBuf fullDataPacket; private ByteBuf fullDataPacket;
@ -83,6 +84,9 @@ public abstract class Chunk implements Viewable, DataContainer {
this.chunkZ = chunkZ; this.chunkZ = chunkZ;
this.shouldGenerate = shouldGenerate; this.shouldGenerate = shouldGenerate;
// true by default
this.enableCachePacket = true;
if (biomes != null && biomes.length == BIOME_COUNT) { if (biomes != null && biomes.length == BIOME_COUNT) {
this.biomes = biomes; this.biomes = biomes;
} else { } else {
@ -273,6 +277,30 @@ public abstract class Chunk implements Viewable, DataContainer {
return shouldGenerate; return shouldGenerate;
} }
/**
* Get if this chunk automatically cache the latest {@link ChunkDataPacket} version.
* <p>
* Retrieved with {@link #retrieveDataBuffer(Consumer)}.
*
* @return true if the chunk automatically cache the chunk packet
*/
public boolean enableCachePacket() {
return enableCachePacket;
}
/**
* Enable or disable the automatic {@link ChunkDataPacket} caching.
*
* @param enableCachePacket true to enable to chunk packet caching
*/
public synchronized void setEnableCachePacket(boolean enableCachePacket) {
this.enableCachePacket = enableCachePacket;
if (enableCachePacket && fullDataPacket != null) {
this.fullDataPacket.release();
this.fullDataPacket = null;
}
}
/** /**
* Get the cached data packet * Get the cached data packet
* <p> * <p>
@ -285,7 +313,7 @@ public abstract class Chunk implements Viewable, DataContainer {
} }
/** /**
* Set the cached {@link ChunkDataPacket} of this chunk * Set the cached {@link ChunkDataPacket} of this chunk.
* *
* @param fullDataPacket the new cached chunk packet * @param fullDataPacket the new cached chunk packet
*/ */
@ -313,7 +341,9 @@ public abstract class Chunk implements Viewable, DataContainer {
if (data == null || !packetUpdated) { if (data == null || !packetUpdated) {
// Packet has never been wrote or is outdated, write it // Packet has never been wrote or is outdated, write it
PacketWriterUtils.writeCallbackPacket(getFreshFullDataPacket(), packet -> { PacketWriterUtils.writeCallbackPacket(getFreshFullDataPacket(), packet -> {
setFullDataPacket(packet); if (enableCachePacket) {
setFullDataPacket(packet);
}
consumer.accept(packet); consumer.accept(packet);
}); });
} else { } else {
@ -504,7 +534,7 @@ public abstract class Chunk implements Viewable, DataContainer {
* Get the {@link ChunkDataPacket} to update a single chunk section * Get the {@link ChunkDataPacket} to update a single chunk section
* *
* @param section the chunk section to update * @param section the chunk section to update
* @return the {@link ChunkDataPacket} to update a single chunk sectionl * @return the {@link ChunkDataPacket} to update a single chunk section
*/ */
protected ChunkDataPacket getChunkSectionUpdatePacket(int section) { protected ChunkDataPacket getChunkSectionUpdatePacket(int section) {
ChunkDataPacket chunkDataPacket = getFreshPartialDataPacket(); ChunkDataPacket chunkDataPacket = getFreshPartialDataPacket();

View File

@ -38,8 +38,8 @@ public class DynamicChunk extends Chunk {
protected final short[] customBlocksId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]; protected final short[] customBlocksId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z];
// Used to get all blocks with data (no null) // Used to get all blocks with data (no null)
// Key is still chunk coord // Key is still chunk coordinates (see #getBlockIndex)
protected Int2ObjectMap<Data> blocksData = new Int2ObjectOpenHashMap<>(16 * 16); // Start with the size of a single row protected Int2ObjectMap<Data> blocksData = new Int2ObjectOpenHashMap<>();
// Contains CustomBlocks' block index which are updatable // Contains CustomBlocks' block index which are updatable
protected IntSet updatableBlocks = new IntOpenHashSet(); protected IntSet updatableBlocks = new IntOpenHashSet();

View File

@ -4,6 +4,7 @@ import net.minestom.server.utils.chunk.ChunkCallback;
/** /**
* Interface implemented to change the way chunks are loaded/saved. * Interface implemented to change the way chunks are loaded/saved.
* <p>
* See {@link MinestomBasicChunkLoader} for the default implementation used in {@link InstanceContainer}. * See {@link MinestomBasicChunkLoader} for the default implementation used in {@link InstanceContainer}.
*/ */
public interface IChunkLoader { public interface IChunkLoader {