From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Paul Sauve 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 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/net/minecraft/world/level/GameRules.java +++ b/src/main/java/net/minecraft/world/level/GameRules.java @@ -0,0 +0,0 @@ public class GameRules { worldserver.setDefaultSpawnPos(worldserver.getSharedSpawnPos(), worldserver.getSharedSpawnAngle()); })); private final Map, GameRules.Value> rules; + private final GameRules.Value[] gameruleArray; // Paper - Perf: Use array for gamerule storage private static > GameRules.Key register(String name, GameRules.Category category, GameRules.Type type) { GameRules.Key gamerules_gamerulekey = new GameRules.Key<>(name, category); @@ -0,0 +0,0 @@ public class GameRules { } public GameRules() { - this.rules = (Map) GameRules.GAME_RULE_TYPES.entrySet().stream().collect(ImmutableMap.toImmutableMap(Entry::getKey, (entry) -> { + // Paper start - Perf: Use array for gamerule storage + this((Map) GameRules.GAME_RULE_TYPES.entrySet().stream().collect(ImmutableMap.toImmutableMap(Entry::getKey, (entry) -> { return ((GameRules.Type) entry.getValue()).createRule(); - })); + }))); + // Paper end - Perf: Use array for gamerule storage } private GameRules(Map, GameRules.Value> rules) { this.rules = rules; + + // Paper start - Perf: Use array for gamerule storage + int arraySize = rules.keySet().stream().mapToInt(key -> key.gameRuleIndex).max().orElse(-1) + 1; + GameRules.Value[] values = new GameRules.Value[arraySize]; + + for (Entry, GameRules.Value> entry : rules.entrySet()) { + values[entry.getKey().gameRuleIndex] = entry.getValue(); + } + + this.gameruleArray = values; + // Paper end - Perf: Use array for gamerule storage } public > T getRule(GameRules.Key key) { - return (T) this.rules.get(key); // CraftBukkit - decompile error + return key == null ? null : (T) this.gameruleArray[key.gameRuleIndex]; // Paper - Perf: Use array for gamerule storage } public CompoundTag createTag() { @@ -0,0 +0,0 @@ public class GameRules { } public static final class Key> { + // Paper start - Perf: Use array for gamerule storage + private static int lastGameRuleIndex = 0; + public final int gameRuleIndex = lastGameRuleIndex++; + // Paper end - Perf: Use array for gamerule storage final String id; private final GameRules.Category category;