2023-04-06 14:18:45 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Paul Sauve <paul@technove.co>
Date: Sun, 9 May 2021 16:49:49 -0500
Subject: [PATCH] Use array for gamerule storage
diff --git a/src/main/java/net/minecraft/world/level/GameRules.java b/src/main/java/net/minecraft/world/level/GameRules.java
2024-01-21 19:37:09 +01:00
index 0112ef51815c4fab38b95d5e917d335eeaaa21cd..c246981987017a2f86c5d632929356855e2b5714 100644
2023-04-06 14:18:45 +02:00
--- a/src/main/java/net/minecraft/world/level/GameRules.java
+++ b/src/main/java/net/minecraft/world/level/GameRules.java
2023-12-06 17:21:56 +01:00
@@ -114,6 +114,7 @@ public class GameRules {
2023-04-06 14:18:45 +02:00
public static final GameRules.Key<GameRules.BooleanValue> RULE_DO_VINES_SPREAD = GameRules.register("doVinesSpread", GameRules.Category.UPDATES, GameRules.BooleanValue.create(true));
2023-09-22 18:11:35 +02:00
public static final GameRules.Key<GameRules.BooleanValue> RULE_ENDER_PEARLS_VANISH_ON_DEATH = GameRules.register("enderPearlsVanishOnDeath", GameRules.Category.PLAYER, GameRules.BooleanValue.create(true));
2023-04-06 14:18:45 +02:00
private final Map<GameRules.Key<?>, GameRules.Value<?>> rules;
2024-01-15 12:38:39 +01:00
+ private final GameRules.Value<?>[] gameruleArray; // Paper - Perf: Use array for gamerule storage
2023-04-06 14:18:45 +02:00
private static <T extends GameRules.Value<T>> GameRules.Key<T> register(String name, GameRules.Category category, GameRules.Type<T> type) {
GameRules.Key<T> gamerules_gamerulekey = new GameRules.Key<>(name, category);
2023-12-06 17:21:56 +01:00
@@ -132,17 +133,30 @@ public class GameRules {
2023-04-06 14:18:45 +02:00
}
public GameRules() {
- this.rules = (Map) GameRules.GAME_RULE_TYPES.entrySet().stream().collect(ImmutableMap.toImmutableMap(Entry::getKey, (entry) -> {
2024-01-15 12:38:39 +01:00
+ // Paper start - Perf: Use array for gamerule storage
2023-04-06 14:18:45 +02:00
+ this((Map) GameRules.GAME_RULE_TYPES.entrySet().stream().collect(ImmutableMap.toImmutableMap(Entry::getKey, (entry) -> {
return ((GameRules.Type) entry.getValue()).createRule();
- }));
+ })));
2024-01-15 12:38:39 +01:00
+ // Paper end - Perf: Use array for gamerule storage
2023-04-06 14:18:45 +02:00
}
private GameRules(Map<GameRules.Key<?>, GameRules.Value<?>> rules) {
this.rules = rules;
+
2024-01-15 12:38:39 +01:00
+ // Paper start - Perf: Use array for gamerule storage
2023-04-06 14:18:45 +02:00
+ int arraySize = rules.keySet().stream().mapToInt(key -> key.gameRuleIndex).max().orElse(-1) + 1;
+ GameRules.Value<?>[] values = new GameRules.Value[arraySize];
+
+ for (Entry<GameRules.Key<?>, GameRules.Value<?>> entry : rules.entrySet()) {
+ values[entry.getKey().gameRuleIndex] = entry.getValue();
+ }
+
+ this.gameruleArray = values;
2024-01-15 12:38:39 +01:00
+ // Paper end - Perf: Use array for gamerule storage
2023-04-06 14:18:45 +02:00
}
public <T extends GameRules.Value<T>> T getRule(GameRules.Key<T> key) {
- return (T) this.rules.get(key); // CraftBukkit - decompile error
2024-01-15 12:38:39 +01:00
+ return key == null ? null : (T) this.gameruleArray[key.gameRuleIndex]; // Paper - Perf: Use array for gamerule storage
2023-04-06 14:18:45 +02:00
}
public CompoundTag createTag() {
2023-12-06 17:21:56 +01:00
@@ -201,6 +215,10 @@ public class GameRules {
2023-04-06 14:18:45 +02:00
}
public static final class Key<T extends GameRules.Value<T>> {
2024-01-15 12:38:39 +01:00
+ // Paper start - Perf: Use array for gamerule storage
2023-04-06 14:18:45 +02:00
+ private static int lastGameRuleIndex = 0;
+ public final int gameRuleIndex = lastGameRuleIndex++;
2024-01-15 12:38:39 +01:00
+ // Paper end - Perf: Use array for gamerule storage
2023-04-06 14:18:45 +02:00
final String id;
private final GameRules.Category category;