Begin optimizing netty.

This commit is contained in:
Eoghanmc22 2020-11-18 13:39:06 -05:00
parent f30330c59b
commit 618fd2b536
7 changed files with 91 additions and 14 deletions

@ -1 +1 @@
Subproject commit 472b3c041c58bd0a4397fb9144b1035682022cbe
Subproject commit 33abb2dc5dd350f05c8efbeb80835edf01056ff2

View File

@ -517,11 +517,6 @@ public class Player extends LivingEntity implements CommandSender {
sendPacketToViewers(updatePacket);
}
} else {
// Player did not move since last time
EntityMovementPacket entityMovementPacket = new EntityMovementPacket();
entityMovementPacket.entityId = getEntityId();
sendPacketToViewers(entityMovementPacket);
}
// Update sync data

View File

@ -1,7 +1,6 @@
package net.minestom.server.network.packet.server.play;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import net.minestom.server.MinecraftServer;
import net.minestom.server.data.Data;
@ -11,6 +10,7 @@ import net.minestom.server.instance.palette.PaletteStorage;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.BufUtils;
import net.minestom.server.utils.Utils;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.chunk.ChunkUtils;
@ -47,7 +47,7 @@ public class ChunkDataPacket implements ServerPacket {
writer.writeBoolean(fullChunk);
int mask = 0;
ByteBuf blocks = Unpooled.buffer(MAX_BUFFER_SIZE);
ByteBuf blocks = BufUtils.getBuffer(MAX_BUFFER_SIZE);
for (byte i = 0; i < CHUNK_SECTION_COUNT; i++) {
if (fullChunk || (sections.length == CHUNK_SECTION_COUNT && sections[i] != 0)) {
final long[] section = paletteStorage.getSectionBlocks()[i];
@ -93,6 +93,7 @@ public class ChunkDataPacket implements ServerPacket {
// Data
writer.writeVarInt(blocks.writerIndex());
writer.getBuffer().writeBytes(blocks);
blocks.release();
// Block entities
writer.writeVarInt(blockEntities.size());

View File

@ -0,0 +1,34 @@
package net.minestom.server.utils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
public class BufUtils {
private static final PooledByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT;
public static ByteBuf getBuffer() {
return alloc.heapBuffer();
}
public static ByteBuf getBuffer(boolean io) {
return io ? alloc.ioBuffer() : alloc.heapBuffer();
}
public static ByteBuf getBuffer(int initialCapacity) {
return alloc.heapBuffer(initialCapacity);
}
public static ByteBuf getBuffer(boolean io, int initialCapacity) {
return io ? alloc.ioBuffer(initialCapacity) : alloc.heapBuffer(initialCapacity);
}
public static ByteBuf getBuffer(int initialCapacity, int maxCapacity) {
return alloc.heapBuffer(initialCapacity, maxCapacity);
}
public static ByteBuf getBuffer(boolean io, int initialCapacity, int maxCapacity) {
return io ? alloc.ioBuffer(initialCapacity, maxCapacity) : alloc.heapBuffer(initialCapacity, maxCapacity);
}
}

View File

@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
@ -85,9 +86,12 @@ public final class PacketUtils {
*/
@NotNull
private static ByteBuf getPacketBuffer(@NotNull ServerPacket packet) {
BinaryWriter writer = new BinaryWriter();
BinaryWriter writer;
if (packet.getId() == ServerPacketIdentifier.CHUNK_DATA)
writer = new BinaryWriter(BufUtils.getBuffer(true, 40_000));
else
writer = new BinaryWriter(BufUtils.getBuffer(true));
packet.write(writer);
return writer.getBuffer();
}

View File

@ -4,11 +4,13 @@ import demo.blocks.BurningTorchBlock;
import demo.blocks.StoneBlock;
import demo.blocks.UpdatableBlockDemo;
import demo.commands.*;
import io.netty.util.ResourceLeakDetector;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandManager;
import net.minestom.server.extras.optifine.OptifineSupport;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.instance.block.rule.vanilla.RedstonePlacementRule;
import net.minestom.server.network.netty.NettyServer;
import net.minestom.server.storage.StorageManager;
import net.minestom.server.storage.systems.FileStorageSystem;
import net.minestom.server.utils.time.TimeUnit;
@ -18,8 +20,14 @@ import net.minestom.server.utils.time.UpdateOption;
public class Main {
public static void main(String[] args) {
ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.PARANOID);
MinecraftServer minecraftServer = MinecraftServer.init();
final NettyServer nettyServer = MinecraftServer.getNettyServer();
nettyServer.setWriteLimit(500_000);
nettyServer.setWriteLimit(500_000);
BlockManager blockManager = MinecraftServer.getBlockManager();
blockManager.registerCustomBlock(new StoneBlock());
blockManager.registerCustomBlock(new UpdatableBlockDemo());
@ -36,9 +44,7 @@ public class Main {
commandManager.register(new DimensionCommand());
commandManager.register(new ShutdownCommand());
commandManager.register(new TeleportCommand());
commandManager.register(new ReloadExtensionCommand());
commandManager.register(new UnloadExtensionCommand());
commandManager.register(new LoadExtensionCommand());
commandManager.register(new PlayersCommand());
commandManager.setUnknownCommandCallback((sender, command) -> sender.sendMessage("unknown command"));

View File

@ -0,0 +1,37 @@
package demo.commands;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.Arguments;
import net.minestom.server.command.builder.Command;
import net.minestom.server.entity.Player;
import java.util.Collection;
import java.util.stream.Collectors;
public class PlayersCommand extends Command {
public PlayersCommand() {
super("players");
setDefaultExecutor(this::usage);
}
private void usage(CommandSender sender, Arguments arguments) {
final Collection<Player> players = MinecraftServer.getConnectionManager().getOnlinePlayers();
final int playerCount = players.size();
sender.sendMessage(String.valueOf(playerCount));
sender.sendMessage("");
final int limit = 15;
if (playerCount <= limit) {
for (final Player player : players) {
sender.sendMessage(player.getUsername());
}
} else {
for (final Player player : players.stream().limit(limit).collect(Collectors.toList())) {
sender.sendMessage(player.getUsername());
}
sender.sendMessage("...");
}
}
}