From 87e11bf7fdf48ecdf3e1cae383c368b9b61d7df9 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sat, 2 Apr 2022 18:10:13 -0700 Subject: [PATCH] Cache resource keys instead of trying to create them (#7643) --- patches/server/0573-Add-PaperRegistry.patch | 10 ++-- patches/server/0856-Add-GameEvent-tags.patch | 12 ++-- patches/server/0889-cache-resource-keys.patch | 57 +++++++++++++++++++ 3 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 patches/server/0889-cache-resource-keys.patch diff --git a/patches/server/0573-Add-PaperRegistry.patch b/patches/server/0573-Add-PaperRegistry.patch index ce5b0c9aa..57709833c 100644 --- a/patches/server/0573-Add-PaperRegistry.patch +++ b/patches/server/0573-Add-PaperRegistry.patch @@ -7,10 +7,10 @@ PaperRegistry is a server-backed impl of bukkit's Registry interface diff --git a/src/main/java/io/papermc/paper/registry/PaperRegistry.java b/src/main/java/io/papermc/paper/registry/PaperRegistry.java new file mode 100644 -index 0000000000000000000000000000000000000000..51cec316df8bc0c7d36e0b1dfdf8d9fae04e3606 +index 0000000000000000000000000000000000000000..8d1f3c4891870b4239df678dd1e52e9f4ef74b2c --- /dev/null +++ b/src/main/java/io/papermc/paper/registry/PaperRegistry.java -@@ -0,0 +1,145 @@ +@@ -0,0 +1,147 @@ +package io.papermc.paper.registry; + +import com.google.common.base.Preconditions; @@ -34,6 +34,7 @@ index 0000000000000000000000000000000000000000..51cec316df8bc0c7d36e0b1dfdf8d9fa +import java.util.Map; +import java.util.Objects; +import java.util.Optional; ++import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; + +@DefaultQualifier(NonNull.class) @@ -49,7 +50,8 @@ index 0000000000000000000000000000000000000000..51cec316df8bc0c7d36e0b1dfdf8d9fa + private boolean registered; + private final RegistryKey registryKey; + private final Supplier> registry; -+ private final Map cache = new HashMap<>(); ++ private final Map cache = new ConcurrentHashMap<>(); ++ private final Map> resourceKeyCache = new ConcurrentHashMap<>(); + + public PaperRegistry(RegistryKey registryKey) { + this.registryKey = registryKey; @@ -93,7 +95,7 @@ index 0000000000000000000000000000000000000000..51cec316df8bc0c7d36e0b1dfdf8d9fa + } + + public Holder getMinecraftHolder(API apiValue) { -+ return this.registry.get().getHolderOrThrow(ResourceKey.create(this.registryKey.resourceKey(), CraftNamespacedKey.toMinecraft(apiValue.getKey()))); ++ return this.registry.get().getHolderOrThrow(this.resourceKeyCache.computeIfAbsent(apiValue.getKey(), key -> ResourceKey.create(this.registryKey.resourceKey(), CraftNamespacedKey.toMinecraft(key)))); + } + + @Override diff --git a/patches/server/0856-Add-GameEvent-tags.patch b/patches/server/0856-Add-GameEvent-tags.patch index 58312e326..4f7d46d22 100644 --- a/patches/server/0856-Add-GameEvent-tags.patch +++ b/patches/server/0856-Add-GameEvent-tags.patch @@ -6,10 +6,10 @@ Subject: [PATCH] Add GameEvent tags diff --git a/src/main/java/io/papermc/paper/CraftGameEventTag.java b/src/main/java/io/papermc/paper/CraftGameEventTag.java new file mode 100644 -index 0000000000000000000000000000000000000000..d5d3c66afb408026513ac436eb1f4fdd381847bf +index 0000000000000000000000000000000000000000..cb78a3d4e21376ea24347187478525d5f0c24079 --- /dev/null +++ b/src/main/java/io/papermc/paper/CraftGameEventTag.java -@@ -0,0 +1,30 @@ +@@ -0,0 +1,34 @@ +package io.papermc.paper; + +import net.minecraft.core.Registry; @@ -20,6 +20,9 @@ index 0000000000000000000000000000000000000000..d5d3c66afb408026513ac436eb1f4fdd +import org.bukkit.craftbukkit.util.CraftNamespacedKey; +import org.jetbrains.annotations.NotNull; + ++import java.util.Collections; ++import java.util.IdentityHashMap; ++import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; @@ -30,9 +33,10 @@ index 0000000000000000000000000000000000000000..d5d3c66afb408026513ac436eb1f4fdd + super(registry, tag); + } + ++ private static final Map> KEY_CACHE = Collections.synchronizedMap(new IdentityHashMap<>()); + @Override -+ public boolean isTagged(@NotNull GameEvent item) { -+ return registry.getHolderOrThrow(ResourceKey.create(Registry.GAME_EVENT_REGISTRY, CraftNamespacedKey.toMinecraft(item.getKey()))).is(tag); ++ public boolean isTagged(@NotNull GameEvent gameEvent) { ++ return registry.getHolderOrThrow(KEY_CACHE.computeIfAbsent(gameEvent, event -> ResourceKey.create(Registry.GAME_EVENT_REGISTRY, CraftNamespacedKey.toMinecraft(event.getKey())))).is(tag); + } + + @Override diff --git a/patches/server/0889-cache-resource-keys.patch b/patches/server/0889-cache-resource-keys.patch new file mode 100644 index 000000000..7139beb01 --- /dev/null +++ b/patches/server/0889-cache-resource-keys.patch @@ -0,0 +1,57 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Jake Potrebic +Date: Sun, 20 Mar 2022 22:06:47 -0700 +Subject: [PATCH] cache resource keys + + +diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java +index a52e76c8870b1dd578c4332930c6f2fd3b31d471..ca9904ded51580425f06587b7eed72da7b95d2f1 100644 +--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java ++++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java +@@ -366,12 +366,13 @@ public class CraftBlock implements Block { + return (biome == null) ? Biome.CUSTOM : biome; + } + ++ private static final java.util.Map> BIOME_KEY_CACHE = Collections.synchronizedMap(new java.util.EnumMap<>(Biome.class)); // Paper + public static Holder biomeToBiomeBase(net.minecraft.core.Registry registry, Biome bio) { + if (bio == null || bio == Biome.CUSTOM) { + return null; + } + +- return registry.getHolderOrThrow(ResourceKey.create(net.minecraft.core.Registry.BIOME_REGISTRY, CraftNamespacedKey.toMinecraft(bio.getKey()))); ++ return registry.getHolderOrThrow(BIOME_KEY_CACHE.computeIfAbsent(bio, b -> ResourceKey.create(net.minecraft.core.Registry.BIOME_REGISTRY, CraftNamespacedKey.toMinecraft(b.getKey())))); // Paper - cache key + } + + @Override +diff --git a/src/main/java/org/bukkit/craftbukkit/tag/CraftEntityTag.java b/src/main/java/org/bukkit/craftbukkit/tag/CraftEntityTag.java +index 393bb27648501c6f25cf846e929f4c95b1a0b11f..979f82d589e378b9e52417d20b48d66f077f5dfd 100644 +--- a/src/main/java/org/bukkit/craftbukkit/tag/CraftEntityTag.java ++++ b/src/main/java/org/bukkit/craftbukkit/tag/CraftEntityTag.java +@@ -16,9 +16,10 @@ public class CraftEntityTag extends CraftTag>> KEY_CACHE = Collections.synchronizedMap(new java.util.EnumMap<>(EntityType.class)); // Paper + @Override + public boolean isTagged(EntityType entity) { +- return registry.getHolderOrThrow(ResourceKey.create(net.minecraft.core.Registry.ENTITY_TYPE_REGISTRY, CraftNamespacedKey.toMinecraft(entity.getKey()))).is(tag); ++ return registry.getHolderOrThrow(KEY_CACHE.computeIfAbsent(entity, type -> ResourceKey.create(net.minecraft.core.Registry.ENTITY_TYPE_REGISTRY, CraftNamespacedKey.toMinecraft(type.getKey())))).is(tag); // Paper - cache key + } + + @Override +diff --git a/src/main/java/org/bukkit/craftbukkit/tag/CraftFluidTag.java b/src/main/java/org/bukkit/craftbukkit/tag/CraftFluidTag.java +index cdd474e9b0363641839a66d3e61fec46c735879a..3611e3e9d33a0f1d82a78a27ea5c2c649225f564 100644 +--- a/src/main/java/org/bukkit/craftbukkit/tag/CraftFluidTag.java ++++ b/src/main/java/org/bukkit/craftbukkit/tag/CraftFluidTag.java +@@ -14,9 +14,10 @@ public class CraftFluidTag extends CraftTag> KEY_CACHE = Collections.synchronizedMap(new java.util.EnumMap<>(Fluid.class)); // Paper + @Override + public boolean isTagged(Fluid fluid) { +- return registry.getHolderOrThrow(net.minecraft.resources.ResourceKey.create(net.minecraft.core.Registry.FLUID_REGISTRY, org.bukkit.craftbukkit.util.CraftNamespacedKey.toMinecraft(fluid.getKey()))).is(tag); // Paper ++ return registry.getHolderOrThrow(KEY_CACHE.computeIfAbsent(fluid, f -> net.minecraft.resources.ResourceKey.create(net.minecraft.core.Registry.FLUID_REGISTRY, org.bukkit.craftbukkit.util.CraftNamespacedKey.toMinecraft(f.getKey())))).is(tag); // Paper - cache key + } + + @Override