9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-19 14:59:32 +00:00
Files
LeavesMC/patches/server/0080-Reduce-array-allocations.patch
2024-10-18 19:19:26 +08:00

574 lines
32 KiB
Diff

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/ca/spottedleaf/moonrise/common/list/EntityList.java b/src/main/java/ca/spottedleaf/moonrise/common/list/EntityList.java
index ba68998f6ef57b24c72fd833bd7de440de9501cc..a8e93e61beb51cc061b489ed4ad70e8fe6dfb8b9 100644
--- a/src/main/java/ca/spottedleaf/moonrise/common/list/EntityList.java
+++ b/src/main/java/ca/spottedleaf/moonrise/common/list/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 org.leavesmc.leaves.util.ArrayConstants;
// list with O(1) remove & contains
@@ -18,9 +19,7 @@ public final class EntityList implements Iterable<Entity> {
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/ca/spottedleaf/moonrise/common/list/IBlockDataList.java b/src/main/java/ca/spottedleaf/moonrise/common/list/IBlockDataList.java
index fcfbca333234c09f7c056bbfcd9ac8860b20a8db..18503ef068924d4be4d40390af8db70d26fd9c7d 100644
--- a/src/main/java/ca/spottedleaf/moonrise/common/list/IBlockDataList.java
+++ b/src/main/java/ca/spottedleaf/moonrise/common/list/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 org.leavesmc.leaves.util.ArrayConstants;
public final class IBlockDataList {
@@ -17,9 +18,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/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java
index 5c7f2471a0b15ac2e714527296ad2aa7291999eb..56ca2488154bd5f064c0a97bf778dc07da4a0662 100644
--- a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java
+++ b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java
@@ -27,6 +27,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
import org.bukkit.event.entity.EntityRemoveEvent;
+import org.leavesmc.leaves.util.ArrayConstants;
public final class ChunkEntitySlices {
@@ -408,7 +409,7 @@ public final class ChunkEntitySlices {
private static final class BasicEntityList<E extends Entity> {
- private static final Entity[] EMPTY = new Entity[0];
+ // protected static final Entity[] EMPTY = new Entity[0]; // Leaves - reduce array allocations
private static final int DEFAULT_CAPACITY = 4;
private E[] storage;
@@ -419,7 +420,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() {
@@ -431,7 +432,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/io/papermc/paper/command/subcommands/VersionCommand.java b/src/main/java/io/papermc/paper/command/subcommands/VersionCommand.java
index ae60bd96b5284d54676d8e7e4dd5d170b526ec1e..0c474b1eb4dbef547890b7db5fcf9c13c86092a2 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 org.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/net/minecraft/nbt/ByteArrayTag.java b/src/main/java/net/minecraft/nbt/ByteArrayTag.java
index 06648f9751fd8a322d0809ffebf6a544596ee1a4..d6761bdb37619b91f147ff7a9197b730b90bd6cb 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 org.leavesmc.leaves.util.ArrayConstants;
public class ByteArrayTag extends CollectionTag<ByteTag> {
@@ -175,7 +176,7 @@ public class ByteArrayTag extends CollectionTag<ByteTag> {
}
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 d7bb00a946346dff0b0269cbd65276e146a63fb0..3f6545f3d2e421b9d2778adb113781b1d516fa0c 100644
--- a/src/main/java/net/minecraft/nbt/CompoundTag.java
+++ b/src/main/java/net/minecraft/nbt/CompoundTag.java
@@ -18,6 +18,7 @@ import javax.annotation.Nullable;
import net.minecraft.CrashReport;
import net.minecraft.CrashReportCategory;
import net.minecraft.ReportedException;
+import org.leavesmc.leaves.util.ArrayConstants;
public class CompoundTag implements Tag {
public static final Codec<CompoundTag> CODEC = Codec.PASSTHROUGH
@@ -409,7 +410,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) {
@@ -421,7 +422,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) {
@@ -433,7 +434,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..8c3ee4c9aa22bcc46f2dc3a5bc35bdde5fae3e64 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 org.leavesmc.leaves.util.ArrayConstants;
public class IntArrayTag extends CollectionTag<IntTag> {
@@ -186,7 +187,7 @@ public class IntArrayTag extends CollectionTag<IntTag> {
}
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 b19298aeaa0dacfe1afb1e672bd30b0c069ec968..8b6304160a6a3ca4c30f1ab75802a9d318d7d7c6 100644
--- a/src/main/java/net/minecraft/network/Connection.java
+++ b/src/main/java/net/minecraft/network/Connection.java
@@ -66,6 +66,7 @@ import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
+import org.leavesmc.leaves.util.ArrayConstants;
public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
diff --git a/src/main/java/net/minecraft/server/level/ServerEntity.java b/src/main/java/net/minecraft/server/level/ServerEntity.java
index 0846e11c82295ccf93683f877962ec7804dad677..b747b3858befe48996ea707cf126f6f8496258fd 100644
--- a/src/main/java/net/minecraft/server/level/ServerEntity.java
+++ b/src/main/java/net/minecraft/server/level/ServerEntity.java
@@ -369,7 +369,7 @@ public class ServerEntity {
if (this.entity instanceof LivingEntity) {
List<Pair<EquipmentSlot, ItemStack>> 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 f494ea2671f219ce02c53ad725cc41646558a4c4..fc7ce6a031c50d270a979ec57e05cec0de7426c2 100644
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -181,6 +181,7 @@ import org.bukkit.event.server.MapInitializeEvent;
import org.bukkit.event.weather.LightningStrikeEvent;
import org.bukkit.event.world.TimeSkipEvent;
// CraftBukkit end
+import org.leavesmc.leaves.util.ArrayConstants;
public class ServerLevel extends Level implements WorldGenLevel, ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemServerLevel, ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemLevelReader { // Paper - rewrite chunk system
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index bc3b8de51def54996cfe8e7a612f382df0b060e1..17b5d93e4d822d0b815594c18884f5349abbef19 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -249,6 +249,7 @@ import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.SmithingInventory;
// CraftBukkit end
+import org.leavesmc.leaves.util.ArrayConstants;
public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl implements ServerGamePacketListener, ServerPlayerConnection, TickablePacketListener {
@@ -806,7 +807,7 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
// Paper start
final int index;
if (packet.getCommand().length() > 64 && ((index = packet.getCommand().indexOf(' ')) == -1 || index >= 64)) {
- this.disconnectAsync(Component.translatable("disconnect.spam"), org.bukkit.event.player.PlayerKickEvent.Cause.SPAM); // Paper - add proper async disconnect
+ this.disconnectAsync(Component.translatable("disconnect.spam", ArrayConstants.emptyObjectArray), org.bukkit.event.player.PlayerKickEvent.Cause.SPAM); // Paper - add proper async disconnect // Leaves - reduce array allocations
return;
}
// Paper end
diff --git a/src/main/java/net/minecraft/server/network/ServerLoginPacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerLoginPacketListenerImpl.java
index 40638b439966619e9c70a18a32abd95b2178fe9f..153e918f1059bb09ad074eb06bbdbbdc6f673b05 100644
--- a/src/main/java/net/minecraft/server/network/ServerLoginPacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerLoginPacketListenerImpl.java
@@ -51,6 +51,7 @@ import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.craftbukkit.util.Waitable;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerPreLoginEvent;
+import org.leavesmc.leaves.util.ArrayConstants;
public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener, TickablePacketListener, CraftPlayer.TransferCookieConnection {
@@ -172,12 +173,12 @@ public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener,
@Override
public void handleHello(ServerboundHelloPacket packet) {
- Validate.validState(this.state == ServerLoginPacketListenerImpl.State.HELLO, "Unexpected hello packet", new Object[0]);
+ Validate.validState(this.state == ServerLoginPacketListenerImpl.State.HELLO, "Unexpected hello packet", ArrayConstants.emptyObjectArray); // Leaves - reduce array allocations
// Paper start - Validate usernames
if (io.papermc.paper.configuration.GlobalConfiguration.get().proxies.isProxyOnlineMode()
&& io.papermc.paper.configuration.GlobalConfiguration.get().unsupportedSettings.performUsernameValidation
&& !this.iKnowThisMayNotBeTheBestIdeaButPleaseDisableUsernameValidation) {
- Validate.validState(StringUtil.isReasonablePlayerName(packet.name()), "Invalid characters in username", new Object[0]);
+ Validate.validState(StringUtil.isReasonablePlayerName(packet.name()), "Invalid characters in username", ArrayConstants.emptyObjectArray); // Leaves - reduce array allocations
}
// Paper end - Validate usernames
this.requestedUsername = packet.name();
@@ -275,7 +276,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 e62a27431bb2872bdf7dcc34366249dbfe8add17..3322ee4e6720c8b06e6d6684cbac95c2d14babb1 100644
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
@@ -950,13 +950,9 @@ public abstract class PlayerList {
final ResourceKey<Level> OVERWORLD = Level.OVERWORLD;
final ResourceKey<Level> THE_NETHER = Level.NETHER;
if (!((fromDim != OVERWORLD || toDim != THE_NETHER) && (fromDim != THE_NETHER || toDim != OVERWORLD))) {
- BlockPos lastPos = entityplayer1.lastPos;
- if (lastPos != null) {
- net.minecraft.BlockUtil.FoundRectangle fromPortal = ReturnPortalManager.findPortalAt(entityplayer1, fromDim, lastPos);
- BlockPos toPos = entityplayer1.blockPosition();
- if (fromPortal != null) {
- ReturnPortalManager.storeReturnPortal(entityplayer1, toDim, toPos, fromPortal);
- }
+ BlockPos fromPortal = org.leavesmc.leaves.util.ReturnPortalManager.findPortalAt(entityplayer1, fromDim, entityplayer1.lastPos);
+ if (fromPortal != null) {
+ org.leavesmc.leaves.util.ReturnPortalManager.storeReturnPortal(entityplayer1, toDim, entityplayer1.blockPosition(), fromPortal);
}
}
}
diff --git a/src/main/java/net/minecraft/server/players/StoredUserList.java b/src/main/java/net/minecraft/server/players/StoredUserList.java
index c038da20b76c0b7b1c18471b20be01e849d29f3a..603007a376dc76c46d34f265283dda693cbf6c88 100644
--- a/src/main/java/net/minecraft/server/players/StoredUserList.java
+++ b/src/main/java/net/minecraft/server/players/StoredUserList.java
@@ -24,6 +24,7 @@ import javax.annotation.Nullable;
import net.minecraft.Util;
import net.minecraft.util.GsonHelper;
import org.slf4j.Logger;
+import org.leavesmc.leaves.util.ArrayConstants;
public abstract class StoredUserList<K, V extends StoredUserEntry<K>> {
@@ -76,7 +77,7 @@ public abstract class StoredUserList<K, V extends StoredUserEntry<K>> {
}
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 93d1416cf2eaeb10e24c69a28a1d9e576ca7dbdc..72d908b88bd21a777a26ed3a510c422912bf9e56 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 org.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 2fa2a4eef21e786f738f36616c3160defa95bce8..36c98f611f043ebadffd1b110ba3002b29731a52 100644
--- a/src/main/java/net/minecraft/world/entity/EquipmentSlot.java
+++ b/src/main/java/net/minecraft/world/entity/EquipmentSlot.java
@@ -19,6 +19,7 @@ public enum EquipmentSlot implements StringRepresentable {
private final int countLimit;
private final int filterFlag;
private final String name;
+ public static final EquipmentSlot[] VALUES = EquipmentSlot.values(); // Leaves - reduce array allocations
private EquipmentSlot(final EquipmentSlot.Type type, final int entityId, final int maxCount, final int armorStandId, final 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 567ceb290d91ce6291202e03496d455503ba59c5..28b6689421b311c5aa975c1f9e3deca9ce76d688 100644
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
@@ -3280,7 +3280,7 @@ public abstract class LivingEntity extends Entity implements Attackable {
@Nullable
private Map<EquipmentSlot, ItemStack> collectEquipmentChanges() {
Map<EquipmentSlot, ItemStack> 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 32a039bc4a99cb9b5db5c3a0e596f1b67a3c8738..69d37babce0b8d38d24abe9664bc0a8be8982820 100644
--- a/src/main/java/net/minecraft/world/entity/Mob.java
+++ b/src/main/java/net/minecraft/world/entity/Mob.java
@@ -1154,7 +1154,7 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
@Override
protected void dropCustomDeathLoot(ServerLevel world, DamageSource source, boolean causedByPlayer) {
super.dropCustomDeathLoot(world, source, causedByPlayer);
- EquipmentSlot[] aenumitemslot = EquipmentSlot.values();
+ EquipmentSlot[] aenumitemslot = EquipmentSlot.VALUES; // Leaves - reduce array allocations
int i = aenumitemslot.length;
for (int j = 0; j < i; ++j) {
@@ -1284,7 +1284,7 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
}
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) {
@@ -1369,7 +1369,7 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
protected void populateDefaultEquipmentEnchantments(ServerLevelAccessor world, RandomSource random, DifficultyInstance localDifficulty) {
this.enchantSpawnedWeapon(world, random, localDifficulty);
- EquipmentSlot[] aenumitemslot = EquipmentSlot.values();
+ EquipmentSlot[] aenumitemslot = EquipmentSlot.VALUES; // Leaves - reduce array allocations
int i = aenumitemslot.length;
for (int j = 0; j < i; ++j) {
@@ -1574,7 +1574,7 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
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/item/crafting/ShapedRecipe.java b/src/main/java/net/minecraft/world/item/crafting/ShapedRecipe.java
index 1bf54b0142fe41b29b21c8b97d3f52bb24a36a92..f19ace4f9027260ded3ee37c1bda91c56d2a086c 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 org.leavesmc.leaves.util.ArrayConstants;
public class ShapedRecipe extends io.papermc.paper.inventory.recipe.RecipeBookExactChoiceRecipe<CraftingInput> implements CraftingRecipe { // Paper - improve exact recipe choices
diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
index 9a6a2acec1bf11282d1451f5d5178e41fab91b39..5452a15b8a082337c9e53a004e93d8058f6a0474 100644
--- a/src/main/java/net/minecraft/world/level/Level.java
+++ b/src/main/java/net/minecraft/world/level/Level.java
@@ -102,6 +102,7 @@ import org.bukkit.craftbukkit.util.CraftSpawnCategory;
import org.bukkit.entity.SpawnCategory;
import org.bukkit.event.block.BlockPhysicsEvent;
// CraftBukkit end
+import org.leavesmc.leaves.util.ArrayConstants;
public abstract class Level implements LevelAccessor, AutoCloseable, ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemLevel, ca.spottedleaf.moonrise.patches.chunk_system.world.ChunkSystemEntityGetter, ca.spottedleaf.moonrise.patches.collisions.world.CollisionLevel { // Paper - rewrite chunk system // Paper - optimise collisions
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 d3d12f9114173f4971f95d7ef895a4374705bd3f..d9e28a241f7fc9c6828dc3b169f56bad08db5b04 100644
--- a/src/main/java/net/minecraft/world/level/block/ComposterBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/ComposterBlock.java
@@ -46,6 +46,7 @@ import net.minecraft.world.phys.shapes.VoxelShape;
import org.bukkit.craftbukkit.inventory.CraftBlockInventoryHolder;
import org.bukkit.craftbukkit.util.DummyGeneratorAccess;
// CraftBukkit end
+import org.leavesmc.leaves.util.ArrayConstants;
public class ComposterBlock extends Block implements WorldlyContainerHolder {
@@ -430,7 +431,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
@@ -479,7 +480,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
@@ -521,7 +522,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 7092a4d4a583f4e01cc02bca17f3bd1bd32677a0..32622ebdd9c5949ad995875d29e121a49e98b698 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
@@ -60,6 +60,7 @@ import org.bukkit.event.inventory.FurnaceSmeltEvent;
import org.bukkit.event.inventory.FurnaceStartSmeltEvent;
import org.bukkit.inventory.CookingRecipe;
// CraftBukkit end
+import org.leavesmc.leaves.util.ArrayConstants;
public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntity implements WorldlyContainer, RecipeCraftingHolder, StackedContentsCompatible {
@@ -67,7 +68,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/org/bukkit/craftbukkit/CraftEquipmentSlot.java b/src/main/java/org/bukkit/craftbukkit/CraftEquipmentSlot.java
index ae86c45c1d49c7646c721991910592091e7333f8..f3dce7156d518193fe27a69f5792800b72742632 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftEquipmentSlot.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftEquipmentSlot.java
@@ -7,8 +7,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 fdcc414f4fa246082ad0732133c870d915ae3084..556247696cde0d31cbb70907648d2970acf81153 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..8fdadf58054b2475f2023f76824af7bbe1303383 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 org.leavesmc.leaves.util.ArrayConstants;
public final class WeakCollection<T> implements Collection<T> {
static final Object NO_VALUE = new Object();
@@ -164,7 +165,7 @@ public final class WeakCollection<T> implements Collection<T> {
@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/org/leavesmc/leaves/util/ArrayConstants.java b/src/main/java/org/leavesmc/leaves/util/ArrayConstants.java
new file mode 100644
index 0000000000000000000000000000000000000000..99d6484ca682d3edb6461157dc57322a0ff8584a
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/util/ArrayConstants.java
@@ -0,0 +1,21 @@
+package org.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];
+
+}