mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-28 12:07:42 +01:00
Reduced the number of garbage objects
This commit is contained in:
parent
0a5349226e
commit
5966cdaa47
@ -38,6 +38,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
|
||||
/**
|
||||
* A chunk is a part of an {@link Instance}, limited by a size of 16x256x16 blocks and subdivided in 16 sections of 16 blocks height.
|
||||
* Should contains all the blocks located at those positions and manage their tick updates.
|
||||
* Be aware that implementations do not need to be thread-safe.
|
||||
* <p>
|
||||
* Chunks can be serialized using {@link #getSerializedData()} and deserialized back with {@link #readChunk(BinaryReader, ChunkCallback)},
|
||||
* allowing you to implement your own storage solution if needed.
|
||||
|
@ -30,6 +30,8 @@ import java.util.Set;
|
||||
|
||||
/**
|
||||
* Represents a {@link Chunk} which store each individual block in memory.
|
||||
* <p>
|
||||
* WARNING: not thread-safe
|
||||
*/
|
||||
public class DynamicChunk extends Chunk {
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
package net.minestom.server.instance;
|
||||
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.UpdateManager;
|
||||
import net.minestom.server.data.Data;
|
||||
@ -79,7 +81,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
|
||||
protected final Set<ObjectEntity> objectEntities = new CopyOnWriteArraySet<>();
|
||||
protected final Set<ExperienceOrb> experienceOrbs = new CopyOnWriteArraySet<>();
|
||||
// Entities per chunk
|
||||
protected final Map<Long, Set<Entity>> chunkEntities = new ConcurrentHashMap<>();
|
||||
protected final Long2ObjectMap<Set<Entity>> chunkEntities = new Long2ObjectOpenHashMap<>();
|
||||
|
||||
// the uuid of this instance
|
||||
protected UUID uniqueId;
|
||||
@ -971,8 +973,10 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
|
||||
|
||||
@NotNull
|
||||
private Set<Entity> getEntitiesInChunk(long index) {
|
||||
synchronized (chunkEntities) {
|
||||
return chunkEntities.computeIfAbsent(index, i -> new CopyOnWriteArraySet<>());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules a block update at a given {@link BlockPosition}.
|
||||
|
@ -1,5 +1,7 @@
|
||||
package net.minestom.server.instance;
|
||||
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.data.Data;
|
||||
import net.minestom.server.data.SerializableData;
|
||||
@ -33,7 +35,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
@ -56,7 +57,7 @@ public class InstanceContainer extends Instance {
|
||||
// the chunk generator used, can be null
|
||||
private ChunkGenerator chunkGenerator;
|
||||
// (chunk index -> chunk) map, contains all the chunks in the instance
|
||||
private final ConcurrentHashMap<Long, Chunk> chunks = new ConcurrentHashMap<>();
|
||||
private final Long2ObjectMap<Chunk> chunks = new Long2ObjectOpenHashMap<>();
|
||||
// contains all the chunks to remove during the next instance tick
|
||||
protected final Set<Chunk> scheduledChunksToRemove = new HashSet<>();
|
||||
|
||||
@ -450,9 +451,11 @@ public class InstanceContainer extends Instance {
|
||||
@Override
|
||||
public Chunk getChunk(int chunkX, int chunkZ) {
|
||||
final long index = ChunkUtils.getChunkIndex(chunkX, chunkZ);
|
||||
synchronized (chunks) {
|
||||
final Chunk chunk = chunks.get(index);
|
||||
return ChunkUtils.isLoaded(chunk) ? chunk : null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the instance ({@link #getUniqueId()} {@link #getData()}) and call {@link #saveChunksToStorage(Runnable)}.
|
||||
@ -492,7 +495,10 @@ public class InstanceContainer extends Instance {
|
||||
|
||||
@Override
|
||||
public void saveChunksToStorage(@Nullable Runnable callback) {
|
||||
this.chunkLoader.saveChunks(chunks.values(), callback);
|
||||
synchronized (chunks) {
|
||||
Collection<Chunk> chunksCollection = chunks.values();
|
||||
this.chunkLoader.saveChunks(chunksCollection, callback);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -639,6 +645,7 @@ public class InstanceContainer extends Instance {
|
||||
copiedInstance.srcInstance = this;
|
||||
copiedInstance.lastBlockChangeTime = lastBlockChangeTime;
|
||||
|
||||
synchronized (chunks) {
|
||||
for (Chunk chunk : chunks.values()) {
|
||||
final int chunkX = chunk.getChunkX();
|
||||
final int chunkZ = chunk.getChunkZ();
|
||||
@ -648,6 +655,7 @@ public class InstanceContainer extends Instance {
|
||||
copiedInstance.cacheChunk(copiedChunk);
|
||||
UPDATE_MANAGER.signalChunkLoad(copiedInstance, chunkX, chunkZ);
|
||||
}
|
||||
}
|
||||
|
||||
return copiedInstance;
|
||||
}
|
||||
@ -693,8 +701,10 @@ public class InstanceContainer extends Instance {
|
||||
*/
|
||||
public void cacheChunk(@NotNull Chunk chunk) {
|
||||
final long index = ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ());
|
||||
synchronized (chunks) {
|
||||
this.chunks.put(index, chunk);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChunkGenerator getChunkGenerator() {
|
||||
@ -823,8 +833,12 @@ public class InstanceContainer extends Instance {
|
||||
});
|
||||
|
||||
// Clear cache
|
||||
synchronized (chunks) {
|
||||
this.chunks.remove(index);
|
||||
}
|
||||
synchronized (chunkEntities) {
|
||||
this.chunkEntities.remove(index);
|
||||
}
|
||||
|
||||
chunk.unload();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user