mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-28 03:57:50 +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,7 +973,9 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
|
||||
|
||||
@NotNull
|
||||
private Set<Entity> getEntitiesInChunk(long index) {
|
||||
return chunkEntities.computeIfAbsent(index, i -> new CopyOnWriteArraySet<>());
|
||||
synchronized (chunkEntities) {
|
||||
return chunkEntities.computeIfAbsent(index, i -> new CopyOnWriteArraySet<>());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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,8 +451,10 @@ public class InstanceContainer extends Instance {
|
||||
@Override
|
||||
public Chunk getChunk(int chunkX, int chunkZ) {
|
||||
final long index = ChunkUtils.getChunkIndex(chunkX, chunkZ);
|
||||
final Chunk chunk = chunks.get(index);
|
||||
return ChunkUtils.isLoaded(chunk) ? chunk : null;
|
||||
synchronized (chunks) {
|
||||
final Chunk chunk = chunks.get(index);
|
||||
return ChunkUtils.isLoaded(chunk) ? chunk : null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -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,14 +645,16 @@ public class InstanceContainer extends Instance {
|
||||
copiedInstance.srcInstance = this;
|
||||
copiedInstance.lastBlockChangeTime = lastBlockChangeTime;
|
||||
|
||||
for (Chunk chunk : chunks.values()) {
|
||||
final int chunkX = chunk.getChunkX();
|
||||
final int chunkZ = chunk.getChunkZ();
|
||||
synchronized (chunks) {
|
||||
for (Chunk chunk : chunks.values()) {
|
||||
final int chunkX = chunk.getChunkX();
|
||||
final int chunkZ = chunk.getChunkZ();
|
||||
|
||||
final Chunk copiedChunk = chunk.copy(chunkX, chunkZ);
|
||||
final Chunk copiedChunk = chunk.copy(chunkX, chunkZ);
|
||||
|
||||
copiedInstance.cacheChunk(copiedChunk);
|
||||
UPDATE_MANAGER.signalChunkLoad(copiedInstance, chunkX, chunkZ);
|
||||
copiedInstance.cacheChunk(copiedChunk);
|
||||
UPDATE_MANAGER.signalChunkLoad(copiedInstance, chunkX, chunkZ);
|
||||
}
|
||||
}
|
||||
|
||||
return copiedInstance;
|
||||
@ -693,7 +701,9 @@ public class InstanceContainer extends Instance {
|
||||
*/
|
||||
public void cacheChunk(@NotNull Chunk chunk) {
|
||||
final long index = ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ());
|
||||
this.chunks.put(index, chunk);
|
||||
synchronized (chunks) {
|
||||
this.chunks.put(index, chunk);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -823,8 +833,12 @@ public class InstanceContainer extends Instance {
|
||||
});
|
||||
|
||||
// Clear cache
|
||||
this.chunks.remove(index);
|
||||
this.chunkEntities.remove(index);
|
||||
synchronized (chunks) {
|
||||
this.chunks.remove(index);
|
||||
}
|
||||
synchronized (chunkEntities) {
|
||||
this.chunkEntities.remove(index);
|
||||
}
|
||||
|
||||
chunk.unload();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user