From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: violetc <58360096+s-yh-china@users.noreply.github.com> Date: Thu, 20 Jul 2023 15:03:28 +0800 Subject: [PATCH] Reduce array allocations This patch is Powered by Gale(https://github.com/GaleMC/Gale) diff --git a/src/main/java/com/destroystokyo/paper/util/maplist/EntityList.java b/src/main/java/com/destroystokyo/paper/util/maplist/EntityList.java index 0133ea6feb1ab88f021f66855669f58367e7420b..d5e5463e3054cc06bd6589a864ff2003b1dfb9e3 100644 --- a/src/main/java/com/destroystokyo/paper/util/maplist/EntityList.java +++ b/src/main/java/com/destroystokyo/paper/util/maplist/EntityList.java @@ -5,6 +5,7 @@ import net.minecraft.world.entity.Entity; import java.util.Arrays; import java.util.Iterator; import java.util.NoSuchElementException; +import top.leavesmc.leaves.util.ArrayConstants; // list with O(1) remove & contains /** @@ -17,9 +18,7 @@ public final class EntityList implements Iterable { this.entityToIndex.defaultReturnValue(Integer.MIN_VALUE); } - protected static final Entity[] EMPTY_LIST = new Entity[0]; - - protected Entity[] entities = EMPTY_LIST; + protected Entity[] entities = ArrayConstants.emptyEntityArray; // Leaves - reduce array allocations protected int count; public int size() { diff --git a/src/main/java/com/destroystokyo/paper/util/maplist/IBlockDataList.java b/src/main/java/com/destroystokyo/paper/util/maplist/IBlockDataList.java index 277cfd9d1e8fff5d9b5e534b75c3c5162d58b0b7..19d5f3e167d7c94d33fcedc6c787d86ad5fee770 100644 --- a/src/main/java/com/destroystokyo/paper/util/maplist/IBlockDataList.java +++ b/src/main/java/com/destroystokyo/paper/util/maplist/IBlockDataList.java @@ -6,6 +6,7 @@ import java.util.Arrays; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.chunk.GlobalPalette; +import top.leavesmc.leaves.util.ArrayConstants; /** * @author Spottedleaf @@ -20,9 +21,7 @@ public final class IBlockDataList { this.map.defaultReturnValue(Long.MAX_VALUE); } - private static final long[] EMPTY_LIST = new long[0]; - - private long[] byIndex = EMPTY_LIST; + private long[] byIndex = ArrayConstants.emptyLongArray; // Leaves - reduce array allocations private int size; public static int getLocationKey(final int x, final int y, final int z) { diff --git a/src/main/java/io/papermc/paper/command/subcommands/VersionCommand.java b/src/main/java/io/papermc/paper/command/subcommands/VersionCommand.java index ae60bd96b5284d54676d8e7e4dd5d170b526ec1e..ad33c51ea9f74d2afd39c9139a9114b0f4436400 100644 --- a/src/main/java/io/papermc/paper/command/subcommands/VersionCommand.java +++ b/src/main/java/io/papermc/paper/command/subcommands/VersionCommand.java @@ -7,6 +7,7 @@ import org.bukkit.command.CommandSender; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.framework.qual.DefaultQualifier; +import top.leavesmc.leaves.util.ArrayConstants; @DefaultQualifier(NonNull.class) public final class VersionCommand implements PaperSubcommand { @@ -14,7 +15,7 @@ public final class VersionCommand implements PaperSubcommand { public boolean execute(final CommandSender sender, final String subCommand, final String[] args) { final @Nullable Command ver = MinecraftServer.getServer().server.getCommandMap().getCommand("version"); if (ver != null) { - ver.execute(sender, "paper", new String[0]); + ver.execute(sender, "paper", ArrayConstants.emptyStringArray); // Leaves - reduce array allocations } return true; } diff --git a/src/main/java/io/papermc/paper/world/ChunkEntitySlices.java b/src/main/java/io/papermc/paper/world/ChunkEntitySlices.java index 7e8dc9e8f381abfdcce2746edc93122d623622d1..59403aa7b77adab763c60f3f7805c802a94dcd41 100644 --- a/src/main/java/io/papermc/paper/world/ChunkEntitySlices.java +++ b/src/main/java/io/papermc/paper/world/ChunkEntitySlices.java @@ -24,6 +24,7 @@ import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.function.Predicate; +import top.leavesmc.leaves.util.ArrayConstants; public final class ChunkEntitySlices { @@ -82,7 +83,7 @@ public final class ChunkEntitySlices { } } - return ret.toArray(new org.bukkit.entity.Entity[0]); + return ret.toArray(ArrayConstants.emptyBukkitEntityArray); // Leaves - reduce array allocations } public CompoundTag save() { @@ -303,7 +304,7 @@ public final class ChunkEntitySlices { protected static final class BasicEntityList { - protected static final Entity[] EMPTY = new Entity[0]; + // protected static final Entity[] EMPTY = new Entity[0]; // Leaves - reduce array allocations protected static final int DEFAULT_CAPACITY = 4; protected E[] storage; @@ -314,7 +315,7 @@ public final class ChunkEntitySlices { } public BasicEntityList(final int cap) { - this.storage = (E[])(cap <= 0 ? EMPTY : new Entity[cap]); + this.storage = (E[])(cap <= 0 ? ArrayConstants.emptyEntityArray : new Entity[cap]); // Leaves - reduce array allocations } public boolean isEmpty() { @@ -326,7 +327,7 @@ public final class ChunkEntitySlices { } private void resize() { - if (this.storage == EMPTY) { + if (this.storage == ArrayConstants.emptyEntityArray) { // Leaves - reduce array allocations this.storage = (E[])new Entity[DEFAULT_CAPACITY]; } else { this.storage = Arrays.copyOf(this.storage, this.storage.length * 2); diff --git a/src/main/java/net/minecraft/nbt/ByteArrayTag.java b/src/main/java/net/minecraft/nbt/ByteArrayTag.java index 06648f9751fd8a322d0809ffebf6a544596ee1a4..40e957a4364c8017072dcd81fcb7cf2c9415df38 100644 --- a/src/main/java/net/minecraft/nbt/ByteArrayTag.java +++ b/src/main/java/net/minecraft/nbt/ByteArrayTag.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.List; import org.apache.commons.lang3.ArrayUtils; +import top.leavesmc.leaves.util.ArrayConstants; public class ByteArrayTag extends CollectionTag { @@ -175,7 +176,7 @@ public class ByteArrayTag extends CollectionTag { } public void clear() { - this.data = new byte[0]; + this.data = ArrayConstants.emptyByteArray; // Leaves - reduce array allocations } @Override diff --git a/src/main/java/net/minecraft/nbt/CompoundTag.java b/src/main/java/net/minecraft/nbt/CompoundTag.java index e464ada187fd1f15efef29a0e5033aeb0c688059..31ae491fdac6e27a3569c8c421cc49e7eab5d4b5 100644 --- a/src/main/java/net/minecraft/nbt/CompoundTag.java +++ b/src/main/java/net/minecraft/nbt/CompoundTag.java @@ -17,6 +17,7 @@ import javax.annotation.Nullable; import net.minecraft.CrashReport; import net.minecraft.CrashReportCategory; import net.minecraft.ReportedException; +import top.leavesmc.leaves.util.ArrayConstants; public class CompoundTag implements Tag { public static final Codec CODEC = Codec.PASSTHROUGH.comapFlatMap((dynamic) -> { @@ -420,7 +421,7 @@ public class CompoundTag implements Tag { throw new ReportedException(this.createReport(key, ByteArrayTag.TYPE, var3)); } - return new byte[0]; + return ArrayConstants.emptyByteArray; // Leaves - reduce array allocations } public int[] getIntArray(String key) { @@ -432,7 +433,7 @@ public class CompoundTag implements Tag { throw new ReportedException(this.createReport(key, IntArrayTag.TYPE, var3)); } - return new int[0]; + return ArrayConstants.emptyIntArray; // Leaves - reduce array allocations } public long[] getLongArray(String key) { @@ -444,7 +445,7 @@ public class CompoundTag implements Tag { throw new ReportedException(this.createReport(key, LongArrayTag.TYPE, var3)); } - return new long[0]; + return ArrayConstants.emptyLongArray; // Leaves - reduce array allocations } public CompoundTag getCompound(String key) { diff --git a/src/main/java/net/minecraft/nbt/IntArrayTag.java b/src/main/java/net/minecraft/nbt/IntArrayTag.java index ff13d67151c50ea11a45117e524c7524e2b1a202..c25d23c9c2ed887ce9ae7f07cd8aa0d18dfc719c 100644 --- a/src/main/java/net/minecraft/nbt/IntArrayTag.java +++ b/src/main/java/net/minecraft/nbt/IntArrayTag.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.List; import org.apache.commons.lang3.ArrayUtils; +import top.leavesmc.leaves.util.ArrayConstants; public class IntArrayTag extends CollectionTag { @@ -186,7 +187,7 @@ public class IntArrayTag extends CollectionTag { } public void clear() { - this.data = new int[0]; + this.data = ArrayConstants.emptyIntArray; // Leaves - reduce array allocations } @Override diff --git a/src/main/java/net/minecraft/network/Connection.java b/src/main/java/net/minecraft/network/Connection.java index c6f773afc39e7c97d13d1977a7609fc46714709d..9cdf39bef3112ac0a522c8bc8cb1b2b5171272ea 100644 --- a/src/main/java/net/minecraft/network/Connection.java +++ b/src/main/java/net/minecraft/network/Connection.java @@ -57,6 +57,7 @@ import org.apache.commons.lang3.Validate; import org.slf4j.Logger; import org.slf4j.Marker; import org.slf4j.MarkerFactory; +import top.leavesmc.leaves.util.ArrayConstants; public class Connection extends SimpleChannelInboundHandler> { diff --git a/src/main/java/net/minecraft/server/level/ServerEntity.java b/src/main/java/net/minecraft/server/level/ServerEntity.java index b0517b9483a8cfd74997f89efdc0d8559b80ae4a..e7cfc98aa6dc19c5adad4c797ae7292a339986a6 100644 --- a/src/main/java/net/minecraft/server/level/ServerEntity.java +++ b/src/main/java/net/minecraft/server/level/ServerEntity.java @@ -348,7 +348,7 @@ public class ServerEntity { if (this.entity instanceof LivingEntity) { List> list = Lists.newArrayList(); - EquipmentSlot[] aenumitemslot = EquipmentSlot.values(); + EquipmentSlot[] aenumitemslot = EquipmentSlot.VALUES; // Leaves - reduce array allocations int i = aenumitemslot.length; for (int j = 0; j < i; ++j) { diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java index 49bed177db75c73f09dfd6b48dc88ae3432f5e84..4b3c4e8b404ee8fca48ceb333438a1ecb70c5cb3 100644 --- a/src/main/java/net/minecraft/server/level/ServerLevel.java +++ b/src/main/java/net/minecraft/server/level/ServerLevel.java @@ -178,6 +178,7 @@ import org.bukkit.event.world.GenericGameEvent; import org.bukkit.event.world.TimeSkipEvent; // CraftBukkit end import it.unimi.dsi.fastutil.ints.IntArrayList; // Paper +import top.leavesmc.leaves.util.ArrayConstants; public class ServerLevel extends Level implements WorldGenLevel { @@ -1070,7 +1071,7 @@ public class ServerLevel extends Level implements WorldGenLevel { BlockPos blockposition2 = blockposition.set(j + randomX, randomY, k + randomZ); BlockState iblockdata = com.destroystokyo.paper.util.maplist.IBlockDataList.getBlockDataFromRaw(raw); - iblockdata.randomTick(this, blockposition2, this.randomTickRandom); + iblockdata.randomTick(this, blockposition2.immutable(), this.randomTickRandom); // Leaves - reduce array allocations } // We drop the fluid tick since LAVA is ALREADY TICKED by the above method (See LiquidBlock). // TODO CHECK ON UPDATE (ping the Canadian) @@ -1376,7 +1377,7 @@ public class ServerLevel extends Level implements WorldGenLevel { public static List getCurrentlyTickingEntities() { Entity ticking = currentlyTickingEntity.get(); - List ret = java.util.Arrays.asList(ticking == null ? new Entity[0] : new Entity[] { ticking }); + List ret = java.util.Arrays.asList(ticking == null ? ArrayConstants.emptyEntityArray : new Entity[] { ticking }); // Leaves - reduce array allocations return ret; } diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java index c70597690364e044a0d4231eb2895cc5bf35820a..6950e1d1eef91141190db83b82debcd11dd220e8 100644 --- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java +++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java @@ -243,6 +243,7 @@ import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.SmithingInventory; // CraftBukkit end +import top.leavesmc.leaves.util.ArrayConstants; public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl implements ServerGamePacketListener, ServerPlayerConnection, TickablePacketListener { @@ -783,13 +784,13 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl // PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel()); // Paper - run this async // CraftBukkit start if (this.chatSpamTickCount.addAndGet(io.papermc.paper.configuration.GlobalConfiguration.get().spamLimiter.tabSpamIncrement) > io.papermc.paper.configuration.GlobalConfiguration.get().spamLimiter.tabSpamLimit && !this.server.getPlayerList().isOp(this.player.getGameProfile())) { // Paper start - split and make configurable - server.scheduleOnMain(() -> this.disconnect(Component.translatable("disconnect.spam", new Object[0]), org.bukkit.event.player.PlayerKickEvent.Cause.SPAM)); // Paper - kick event cause + server.scheduleOnMain(() -> this.disconnect(Component.translatable("disconnect.spam", ArrayConstants.emptyObjectArray), org.bukkit.event.player.PlayerKickEvent.Cause.SPAM)); // Paper - kick event cause // Leaves - reduce array allocations return; } // Paper start String str = packet.getCommand(); int index = -1; if (str.length() > 64 && ((index = str.indexOf(' ')) == -1 || index >= 64)) { - server.scheduleOnMain(() -> this.disconnect(Component.translatable("disconnect.spam", new Object[0]))); // Paper + server.scheduleOnMain(() -> this.disconnect(Component.translatable("disconnect.spam", ArrayConstants.emptyObjectArray))); // Paper - kick event cause // Leaves - reduce array allocations return; } // Paper end @@ -3202,7 +3203,7 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl // Paper start if (!org.bukkit.Bukkit.isPrimaryThread()) { if (this.recipeSpamPackets.addAndGet(io.papermc.paper.configuration.GlobalConfiguration.get().spamLimiter.recipeSpamIncrement) > io.papermc.paper.configuration.GlobalConfiguration.get().spamLimiter.recipeSpamLimit) { - this.server.scheduleOnMain(() -> this.disconnect(net.minecraft.network.chat.Component.translatable("disconnect.spam", new Object[0]), org.bukkit.event.player.PlayerKickEvent.Cause.SPAM)); // Paper - kick event cause + this.server.scheduleOnMain(() -> this.disconnect(net.minecraft.network.chat.Component.translatable("disconnect.spam", ArrayConstants.emptyObjectArray), org.bukkit.event.player.PlayerKickEvent.Cause.SPAM)); // Paper - kick event cause // Leaves - reduce array allocations return; } } diff --git a/src/main/java/net/minecraft/server/network/ServerLoginPacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerLoginPacketListenerImpl.java index 89b3184be952fd0803520dd0f717f3acfc3cb496..adf510f28ce9a9c83821e23e0e46dfc32659c6a7 100644 --- a/src/main/java/net/minecraft/server/network/ServerLoginPacketListenerImpl.java +++ b/src/main/java/net/minecraft/server/network/ServerLoginPacketListenerImpl.java @@ -44,6 +44,7 @@ import org.bukkit.craftbukkit.util.Waitable; import org.bukkit.event.player.AsyncPlayerPreLoginEvent; import org.bukkit.event.player.PlayerPreLoginEvent; // CraftBukkit end +import top.leavesmc.leaves.util.ArrayConstants; public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener, TickablePacketListener { @@ -138,8 +139,8 @@ public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener, @Override public void handleHello(ServerboundHelloPacket packet) { - Validate.validState(this.state == ServerLoginPacketListenerImpl.State.HELLO, "Unexpected hello packet", new Object[0]); - if (io.papermc.paper.configuration.GlobalConfiguration.get().proxies.isProxyOnlineMode() && io.papermc.paper.configuration.GlobalConfiguration.get().unsupportedSettings.performUsernameValidation && !this.iKnowThisMayNotBeTheBestIdeaButPleaseDisableUsernameValidation) Validate.validState(Player.isValidUsername(packet.name()), "Invalid characters in username", new Object[0]); // Paper - config username validation + Validate.validState(this.state == ServerLoginPacketListenerImpl.State.HELLO, "Unexpected hello packet", ArrayConstants.emptyObjectArray); // Leaves - reduce array allocations + if (io.papermc.paper.configuration.GlobalConfiguration.get().proxies.isProxyOnlineMode() && io.papermc.paper.configuration.GlobalConfiguration.get().unsupportedSettings.performUsernameValidation && !this.iKnowThisMayNotBeTheBestIdeaButPleaseDisableUsernameValidation) Validate.validState(Player.isValidUsername(packet.name()), "Invalid characters in username", ArrayConstants.emptyObjectArray); // Paper - config username validation // Leaves - reduce array allocations this.requestedUsername = packet.name(); GameProfile gameprofile = this.server.getSingleplayerProfile(); @@ -218,7 +219,7 @@ public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener, @Override public void handleKey(ServerboundKeyPacket packet) { - Validate.validState(this.state == ServerLoginPacketListenerImpl.State.KEY, "Unexpected key packet", new Object[0]); + Validate.validState(this.state == ServerLoginPacketListenerImpl.State.KEY, "Unexpected key packet", ArrayConstants.emptyObjectArray); // Leaves - reduce array allocations final String s; diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java index 2818f21777560f271379884e0cf3ef26bd99196a..cd962621ee0d8bd17926a90ce960c46f1ae5a8dd 100644 --- a/src/main/java/net/minecraft/server/players/PlayerList.java +++ b/src/main/java/net/minecraft/server/players/PlayerList.java @@ -122,6 +122,7 @@ import org.bukkit.event.player.PlayerSpawnChangeEvent; import top.leavesmc.leaves.bot.ServerBot; import top.leavesmc.leaves.util.ReturnPortalManager; // Leaves - return portal fix +import top.leavesmc.leaves.util.ArrayConstants; public abstract class PlayerList { @@ -734,7 +735,7 @@ public abstract class PlayerList { while (iterator.hasNext()) { entityplayer = (ServerPlayer) iterator.next(); this.save(entityplayer); // CraftBukkit - Force the player's inventory to be saved - entityplayer.connection.disconnect(Component.translatable("multiplayer.disconnect.duplicate_login", new Object[0]), org.bukkit.event.player.PlayerKickEvent.Cause.DUPLICATE_LOGIN); // Paper - kick event cause + entityplayer.connection.disconnect(Component.translatable("multiplayer.disconnect.duplicate_login", ArrayConstants.emptyObjectArray), org.bukkit.event.player.PlayerKickEvent.Cause.DUPLICATE_LOGIN); // Paper - kick event cause // Leaves - reduce array allocations } // Instead of kicking then returning, we need to store the kick reason diff --git a/src/main/java/net/minecraft/server/players/StoredUserList.java b/src/main/java/net/minecraft/server/players/StoredUserList.java index 665120a62525f56912263a3e1b6f12f6c3e15dec..7fd220838520bf0f971d6fbe6d9286592d590253 100644 --- a/src/main/java/net/minecraft/server/players/StoredUserList.java +++ b/src/main/java/net/minecraft/server/players/StoredUserList.java @@ -25,6 +25,7 @@ import javax.annotation.Nullable; import net.minecraft.Util; import net.minecraft.util.GsonHelper; import org.slf4j.Logger; +import top.leavesmc.leaves.util.ArrayConstants; public abstract class StoredUserList> { @@ -79,7 +80,7 @@ public abstract class StoredUserList> { } public String[] getUserList() { - return (String[]) this.map.keySet().toArray(new String[0]); + return (String[]) this.map.keySet().toArray(ArrayConstants.emptyStringArray); // Leaves - reduce array allocations } public boolean isEmpty() { diff --git a/src/main/java/net/minecraft/util/ZeroBitStorage.java b/src/main/java/net/minecraft/util/ZeroBitStorage.java index e23995acb97100830079677aab0896487a705416..53e81721507cfc8f88be0ca9565746d78b2a1686 100644 --- a/src/main/java/net/minecraft/util/ZeroBitStorage.java +++ b/src/main/java/net/minecraft/util/ZeroBitStorage.java @@ -4,9 +4,10 @@ import java.util.Arrays; import java.util.function.IntConsumer; import org.apache.commons.lang3.Validate; import net.minecraft.world.level.chunk.Palette; +import top.leavesmc.leaves.util.ArrayConstants; public class ZeroBitStorage implements BitStorage { - public static final long[] RAW = new long[0]; + public static final long[] RAW = ArrayConstants.emptyLongArray; // Leaves - reduce array allocations private final int size; public ZeroBitStorage(int size) { diff --git a/src/main/java/net/minecraft/world/entity/EquipmentSlot.java b/src/main/java/net/minecraft/world/entity/EquipmentSlot.java index 2e324276ea4cd9e528c6a3f9a9ba394b378fe075..8d949983a30f27a60982f6b406151408faacb18a 100644 --- a/src/main/java/net/minecraft/world/entity/EquipmentSlot.java +++ b/src/main/java/net/minecraft/world/entity/EquipmentSlot.java @@ -15,6 +15,7 @@ public enum EquipmentSlot implements StringRepresentable { private final int index; private final int filterFlag; private final String name; + public static final EquipmentSlot[] VALUES = EquipmentSlot.values(); // Leaves - reduce array allocations private EquipmentSlot(EquipmentSlot.Type type, int entityId, int armorStandId, String name) { this.type = type; diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java index c5be586d14751991c95626adb76d7a99612f7daa..2b34c1b58bc0f25462834bdc2e79c6936553729b 100644 --- a/src/main/java/net/minecraft/world/entity/LivingEntity.java +++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java @@ -3143,7 +3143,7 @@ public abstract class LivingEntity extends Entity implements Attackable { @Nullable private Map collectEquipmentChanges() { Map map = null; - EquipmentSlot[] aenumitemslot = EquipmentSlot.values(); + EquipmentSlot[] aenumitemslot = EquipmentSlot.VALUES; // Leaves - reduce array allocations int i = aenumitemslot.length; for (int j = 0; j < i; ++j) { diff --git a/src/main/java/net/minecraft/world/entity/Mob.java b/src/main/java/net/minecraft/world/entity/Mob.java index 339782b10101189fdf70d41330d2e793713d1544..a4193814174821614e8c47d7662d567a352102bc 100644 --- a/src/main/java/net/minecraft/world/entity/Mob.java +++ b/src/main/java/net/minecraft/world/entity/Mob.java @@ -1082,7 +1082,7 @@ public abstract class Mob extends LivingEntity implements Targeting { @Override protected void dropCustomDeathLoot(DamageSource source, int lootingMultiplier, boolean allowDrops) { super.dropCustomDeathLoot(source, lootingMultiplier, allowDrops); - EquipmentSlot[] aenumitemslot = EquipmentSlot.values(); + EquipmentSlot[] aenumitemslot = EquipmentSlot.VALUES; // Leaves - reduce array allocations int j = aenumitemslot.length; for (int k = 0; k < j; ++k) { @@ -1144,7 +1144,7 @@ public abstract class Mob extends LivingEntity implements Targeting { } boolean flag = true; - EquipmentSlot[] aenumitemslot = EquipmentSlot.values(); + EquipmentSlot[] aenumitemslot = EquipmentSlot.VALUES; // Leaves - reduce array allocations int j = aenumitemslot.length; for (int k = 0; k < j; ++k) { @@ -1231,7 +1231,7 @@ public abstract class Mob extends LivingEntity implements Targeting { float f = localDifficulty.getSpecialMultiplier(); this.enchantSpawnedWeapon(random, f); - EquipmentSlot[] aenumitemslot = EquipmentSlot.values(); + EquipmentSlot[] aenumitemslot = EquipmentSlot.VALUES; // Leaves - reduce array allocations int i = aenumitemslot.length; for (int j = 0; j < i; ++j) { @@ -1450,7 +1450,7 @@ public abstract class Mob extends LivingEntity implements Targeting { t0.setInvulnerable(this.isInvulnerable()); if (flag) { t0.setCanPickUpLoot(this.canPickUpLoot()); - EquipmentSlot[] aenumitemslot = EquipmentSlot.values(); + EquipmentSlot[] aenumitemslot = EquipmentSlot.VALUES; // Leaves - reduce array allocations int i = aenumitemslot.length; for (int j = 0; j < i; ++j) { diff --git a/src/main/java/net/minecraft/world/entity/monster/ZombieVillager.java b/src/main/java/net/minecraft/world/entity/monster/ZombieVillager.java index 94396ad1a3c280787d36c6c18256d10340ace488..ae0de0d0cb970a10a8a612cf34810a3613ac6c73 100644 --- a/src/main/java/net/minecraft/world/entity/monster/ZombieVillager.java +++ b/src/main/java/net/minecraft/world/entity/monster/ZombieVillager.java @@ -237,7 +237,7 @@ public class ZombieVillager extends Zombie implements VillagerDataHolder { return; } // CraftBukkit end - EquipmentSlot[] aenumitemslot = EquipmentSlot.values(); + EquipmentSlot[] aenumitemslot = EquipmentSlot.VALUES; // Leaves - reduce array allocations int i = aenumitemslot.length; for (int j = 0; j < i; ++j) { diff --git a/src/main/java/net/minecraft/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java index fdfbca595e5bff701bf6d9dd7990d67fbb16795f..c85ae98b7d50e7f08035417d2f4d43099c02041c 100644 --- a/src/main/java/net/minecraft/world/item/ItemStack.java +++ b/src/main/java/net/minecraft/world/item/ItemStack.java @@ -1052,7 +1052,7 @@ public final class ItemStack { int k; if (ItemStack.shouldShowInTooltip(i, ItemStack.TooltipPart.MODIFIERS)) { - EquipmentSlot[] aenumitemslot = EquipmentSlot.values(); + EquipmentSlot[] aenumitemslot = EquipmentSlot.VALUES; // Leaves - reduce array allocations k = aenumitemslot.length; diff --git a/src/main/java/net/minecraft/world/item/crafting/ShapedRecipe.java b/src/main/java/net/minecraft/world/item/crafting/ShapedRecipe.java index d772cf80fa3831e1c79d601ea09a073da089e2c5..4699f5fd794a7ef1f430e801bd35f1c62452aa8e 100644 --- a/src/main/java/net/minecraft/world/item/crafting/ShapedRecipe.java +++ b/src/main/java/net/minecraft/world/item/crafting/ShapedRecipe.java @@ -16,6 +16,7 @@ import org.bukkit.craftbukkit.inventory.CraftRecipe; import org.bukkit.craftbukkit.inventory.CraftShapedRecipe; import org.bukkit.inventory.RecipeChoice; // CraftBukkit end +import top.leavesmc.leaves.util.ArrayConstants; public class ShapedRecipe extends io.papermc.paper.inventory.recipe.RecipeBookExactChoiceRecipe implements CraftingRecipe { // Paper - improve exact recipe choices diff --git a/src/main/java/net/minecraft/world/item/enchantment/Enchantments.java b/src/main/java/net/minecraft/world/item/enchantment/Enchantments.java index 6c2e8049c2197ddc912c1a0fc99c87beae81e25b..be13baa7b51c4ad377b50d8ca8f7cfad3c870390 100644 --- a/src/main/java/net/minecraft/world/item/enchantment/Enchantments.java +++ b/src/main/java/net/minecraft/world/item/enchantment/Enchantments.java @@ -43,8 +43,10 @@ public class Enchantments { public static final Enchantment MULTISHOT = register("multishot", new MultiShotEnchantment(Enchantment.Rarity.RARE, EquipmentSlot.MAINHAND)); public static final Enchantment QUICK_CHARGE = register("quick_charge", new QuickChargeEnchantment(Enchantment.Rarity.UNCOMMON, EquipmentSlot.MAINHAND)); public static final Enchantment PIERCING = register("piercing", new ArrowPiercingEnchantment(Enchantment.Rarity.COMMON, EquipmentSlot.MAINHAND)); - public static final Enchantment MENDING = register("mending", new MendingEnchantment(Enchantment.Rarity.RARE, EquipmentSlot.values())); - public static final Enchantment VANISHING_CURSE = register("vanishing_curse", new VanishingCurseEnchantment(Enchantment.Rarity.VERY_RARE, EquipmentSlot.values())); + // Leaves start - reduce array allocations + public static final Enchantment MENDING = Enchantments.register("mending", new MendingEnchantment(Enchantment.Rarity.RARE, EquipmentSlot.VALUES)); + public static final Enchantment VANISHING_CURSE = Enchantments.register("vanishing_curse", new VanishingCurseEnchantment(Enchantment.Rarity.VERY_RARE, EquipmentSlot.VALUES)); + // Leaves end - reduce array allocations private static Enchantment register(String name, Enchantment enchantment) { return Registry.register(BuiltInRegistries.ENCHANTMENT, name, enchantment); diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java index 614c37b11f7820bedc576fce74829c1950ed7756..302f0f5c3a8137e1eb1b90a25e92e82e308c02c4 100644 --- a/src/main/java/net/minecraft/world/level/Level.java +++ b/src/main/java/net/minecraft/world/level/Level.java @@ -97,6 +97,7 @@ import org.bukkit.craftbukkit.util.CraftSpawnCategory; import org.bukkit.entity.SpawnCategory; import org.bukkit.event.block.BlockPhysicsEvent; // CraftBukkit end +import top.leavesmc.leaves.util.ArrayConstants; public abstract class Level implements LevelAccessor, AutoCloseable { @@ -1848,7 +1849,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable { public org.bukkit.entity.Entity[] getChunkEntities(int chunkX, int chunkZ) { io.papermc.paper.world.ChunkEntitySlices slices = ((ServerLevel)this).getEntityLookup().getChunk(chunkX, chunkZ); if (slices == null) { - return new org.bukkit.entity.Entity[0]; + return ArrayConstants.emptyBukkitEntityArray; // Leaves - reduce array allocations } return slices.getChunkEntities(); } diff --git a/src/main/java/net/minecraft/world/level/block/ComposterBlock.java b/src/main/java/net/minecraft/world/level/block/ComposterBlock.java index de8b5cab7925c1c46478952ed2cb2bd40f9a9c48..3ea1c2a9a3e6dfb65d95b5ed92d21f401a1c0cd2 100644 --- a/src/main/java/net/minecraft/world/level/block/ComposterBlock.java +++ b/src/main/java/net/minecraft/world/level/block/ComposterBlock.java @@ -44,6 +44,7 @@ import net.minecraft.world.phys.shapes.VoxelShape; import org.bukkit.craftbukkit.inventory.CraftBlockInventoryHolder; import org.bukkit.craftbukkit.util.DummyGeneratorAccess; // CraftBukkit end +import top.leavesmc.leaves.util.ArrayConstants; public class ComposterBlock extends Block implements WorldlyContainerHolder { @@ -422,7 +423,7 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder { @Override public int[] getSlotsForFace(Direction side) { - return side == Direction.DOWN ? new int[]{0} : new int[0]; + return side == Direction.DOWN ? ArrayConstants.zeroSingletonIntArray : ArrayConstants.emptyIntArray; // Leaves - reduce array allocations } @Override @@ -471,7 +472,7 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder { @Override public int[] getSlotsForFace(Direction side) { - return side == Direction.UP ? new int[]{0} : new int[0]; + return side == Direction.UP ? ArrayConstants.zeroSingletonIntArray : ArrayConstants.emptyIntArray; // Leaves - reduce array allocations } @Override @@ -513,7 +514,7 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder { @Override public int[] getSlotsForFace(Direction side) { - return new int[0]; + return ArrayConstants.emptyIntArray; // Leaves - reduce array allocations } @Override diff --git a/src/main/java/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java index eeed0ccbf0fc04e12dea5e8602f67d862fbd3600..0caf6040c2da342971c37a9af5d83f3263909361 100644 --- a/src/main/java/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java +++ b/src/main/java/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java @@ -58,6 +58,7 @@ import org.bukkit.event.inventory.FurnaceSmeltEvent; import org.bukkit.event.inventory.FurnaceStartSmeltEvent; import org.bukkit.inventory.CookingRecipe; // CraftBukkit end +import top.leavesmc.leaves.util.ArrayConstants; public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntity implements WorldlyContainer, RecipeCraftingHolder, StackedContentsCompatible { @@ -65,7 +66,7 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit protected static final int SLOT_FUEL = 1; protected static final int SLOT_RESULT = 2; public static final int DATA_LIT_TIME = 0; - private static final int[] SLOTS_FOR_UP = new int[]{0}; + private static final int[] SLOTS_FOR_UP = ArrayConstants.zeroSingletonIntArray; // Leaves - reduce array allocations private static final int[] SLOTS_FOR_DOWN = new int[]{2, 1}; private static final int[] SLOTS_FOR_SIDES = new int[]{1}; public static final int DATA_LIT_DURATION = 1; diff --git a/src/main/java/net/minecraft/world/level/storage/PlayerDataStorage.java b/src/main/java/net/minecraft/world/level/storage/PlayerDataStorage.java index b3a90d6ef0e17c236e0b3c46e2d0012671afdaa7..f273d6ab29d8c2a36a4a00ef341d2a8e7ce3e8c3 100644 --- a/src/main/java/net/minecraft/world/level/storage/PlayerDataStorage.java +++ b/src/main/java/net/minecraft/world/level/storage/PlayerDataStorage.java @@ -12,7 +12,6 @@ import net.minecraft.nbt.NbtAccounter; import net.minecraft.nbt.NbtIo; import net.minecraft.nbt.NbtUtils; import net.minecraft.server.level.ServerPlayer; -import net.minecraft.util.datafix.DataFixTypes; import net.minecraft.world.entity.player.Player; import org.slf4j.Logger; @@ -21,6 +20,7 @@ import java.io.FileInputStream; import java.io.InputStream; import org.bukkit.craftbukkit.entity.CraftPlayer; // CraftBukkit end +import top.leavesmc.leaves.util.ArrayConstants; public class PlayerDataStorage { @@ -124,7 +124,7 @@ public class PlayerDataStorage { String[] astring = this.playerDir.list(); if (astring == null) { - astring = new String[0]; + astring = ArrayConstants.emptyStringArray; // Leaves - reduce array allocations } for (int i = 0; i < astring.length; ++i) { diff --git a/src/main/java/org/bukkit/craftbukkit/CraftEquipmentSlot.java b/src/main/java/org/bukkit/craftbukkit/CraftEquipmentSlot.java index 402a238cf502003a232bb95473bd13e59e067fab..6095f5c9298558c77a788c1c9f9ef1f32825b37c 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftEquipmentSlot.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftEquipmentSlot.java @@ -5,8 +5,10 @@ import org.bukkit.inventory.EquipmentSlot; public class CraftEquipmentSlot { - private static final net.minecraft.world.entity.EquipmentSlot[] slots = new net.minecraft.world.entity.EquipmentSlot[EquipmentSlot.values().length]; - private static final EquipmentSlot[] enums = new EquipmentSlot[net.minecraft.world.entity.EquipmentSlot.values().length]; + // Leaves start - reduce array allocations + private static final net.minecraft.world.entity.EquipmentSlot[] slots = net.minecraft.world.entity.EquipmentSlot.VALUES; + private static final EquipmentSlot[] enums = new EquipmentSlot[net.minecraft.world.entity.EquipmentSlot.VALUES.length]; + // Leaves end - reduce array allocations static { set(EquipmentSlot.HAND, net.minecraft.world.entity.EquipmentSlot.MAINHAND); diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftEntityEquipment.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftEntityEquipment.java index 6827979a5b270ced53b46ecb9eff548727dadb81..1b84078152d0585ec51288e585754be176f2d7c2 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftEntityEquipment.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftEntityEquipment.java @@ -165,7 +165,7 @@ public class CraftEntityEquipment implements EntityEquipment { @Override public void clear() { - for (net.minecraft.world.entity.EquipmentSlot slot : net.minecraft.world.entity.EquipmentSlot.values()) { + for (net.minecraft.world.entity.EquipmentSlot slot : net.minecraft.world.entity.EquipmentSlot.VALUES) { // Leaves - reduce array allocations this.setEquipment(slot, null, false); } } diff --git a/src/main/java/org/bukkit/craftbukkit/util/WeakCollection.java b/src/main/java/org/bukkit/craftbukkit/util/WeakCollection.java index b25dc23b81687dd4d4e70b3615ffb91f8c03c68b..9435ea991a429c1d81d4cba63231e846e3bd2b7e 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/WeakCollection.java +++ b/src/main/java/org/bukkit/craftbukkit/util/WeakCollection.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; +import top.leavesmc.leaves.util.ArrayConstants; public final class WeakCollection implements Collection { static final Object NO_VALUE = new Object(); @@ -164,7 +165,7 @@ public final class WeakCollection implements Collection { @Override public Object[] toArray() { - return this.toArray(new Object[0]); + return this.toArray(ArrayConstants.emptyObjectArray); // Leaves - reduce array allocations } @Override diff --git a/src/main/java/top/leavesmc/leaves/util/ArrayConstants.java b/src/main/java/top/leavesmc/leaves/util/ArrayConstants.java new file mode 100644 index 0000000000000000000000000000000000000000..0523062a825bd36f335f1fa6e1440eaaf400fd58 --- /dev/null +++ b/src/main/java/top/leavesmc/leaves/util/ArrayConstants.java @@ -0,0 +1,21 @@ +package top.leavesmc.leaves.util; + +import net.minecraft.server.level.ServerLevel; + +// Powered by Gale(https://github.com/GaleMC/Gale) + +public class ArrayConstants { + + private ArrayConstants() {} + + public static final Object[] emptyObjectArray = new Object[0]; + public static final int[] emptyIntArray = new int[0]; + public static final int[] zeroSingletonIntArray = new int[]{0}; + public static final byte[] emptyByteArray = new byte[0]; + public static final String[] emptyStringArray = new String[0]; + public static final long[] emptyLongArray = new long[0]; + public static final org.bukkit.entity.Entity[] emptyBukkitEntityArray = new org.bukkit.entity.Entity[0]; + public static final net.minecraft.world.entity.Entity[] emptyEntityArray = new net.minecraft.world.entity.Entity[0]; + public static final ServerLevel[] emptyServerLevelArray = new ServerLevel[0]; + +}