Cache resource keys instead of trying to create them (#7643)

This commit is contained in:
Jake Potrebic 2022-04-02 18:10:13 -07:00 committed by GitHub
parent ea2c81e4b9
commit 87e11bf7fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 8 deletions

View File

@ -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 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 new file mode 100644
index 0000000000000000000000000000000000000000..51cec316df8bc0c7d36e0b1dfdf8d9fae04e3606 index 0000000000000000000000000000000000000000..8d1f3c4891870b4239df678dd1e52e9f4ef74b2c
--- /dev/null --- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/PaperRegistry.java +++ b/src/main/java/io/papermc/paper/registry/PaperRegistry.java
@@ -0,0 +1,145 @@ @@ -0,0 +1,147 @@
+package io.papermc.paper.registry; +package io.papermc.paper.registry;
+ +
+import com.google.common.base.Preconditions; +import com.google.common.base.Preconditions;
@ -34,6 +34,7 @@ index 0000000000000000000000000000000000000000..51cec316df8bc0c7d36e0b1dfdf8d9fa
+import java.util.Map; +import java.util.Map;
+import java.util.Objects; +import java.util.Objects;
+import java.util.Optional; +import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Supplier; +import java.util.function.Supplier;
+ +
+@DefaultQualifier(NonNull.class) +@DefaultQualifier(NonNull.class)
@ -49,7 +50,8 @@ index 0000000000000000000000000000000000000000..51cec316df8bc0c7d36e0b1dfdf8d9fa
+ private boolean registered; + private boolean registered;
+ private final RegistryKey<API, MINECRAFT> registryKey; + private final RegistryKey<API, MINECRAFT> registryKey;
+ private final Supplier<Registry<MINECRAFT>> registry; + private final Supplier<Registry<MINECRAFT>> registry;
+ private final Map<NamespacedKey, API> cache = new HashMap<>(); + private final Map<NamespacedKey, API> cache = new ConcurrentHashMap<>();
+ private final Map<NamespacedKey, ResourceKey<MINECRAFT>> resourceKeyCache = new ConcurrentHashMap<>();
+ +
+ public PaperRegistry(RegistryKey<API, MINECRAFT> registryKey) { + public PaperRegistry(RegistryKey<API, MINECRAFT> registryKey) {
+ this.registryKey = registryKey; + this.registryKey = registryKey;
@ -93,7 +95,7 @@ index 0000000000000000000000000000000000000000..51cec316df8bc0c7d36e0b1dfdf8d9fa
+ } + }
+ +
+ public Holder<MINECRAFT> getMinecraftHolder(API apiValue) { + public Holder<MINECRAFT> 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 + @Override

View File

@ -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 diff --git a/src/main/java/io/papermc/paper/CraftGameEventTag.java b/src/main/java/io/papermc/paper/CraftGameEventTag.java
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..d5d3c66afb408026513ac436eb1f4fdd381847bf index 0000000000000000000000000000000000000000..cb78a3d4e21376ea24347187478525d5f0c24079
--- /dev/null --- /dev/null
+++ b/src/main/java/io/papermc/paper/CraftGameEventTag.java +++ b/src/main/java/io/papermc/paper/CraftGameEventTag.java
@@ -0,0 +1,30 @@ @@ -0,0 +1,34 @@
+package io.papermc.paper; +package io.papermc.paper;
+ +
+import net.minecraft.core.Registry; +import net.minecraft.core.Registry;
@ -20,6 +20,9 @@ index 0000000000000000000000000000000000000000..d5d3c66afb408026513ac436eb1f4fdd
+import org.bukkit.craftbukkit.util.CraftNamespacedKey; +import org.bukkit.craftbukkit.util.CraftNamespacedKey;
+import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.NotNull;
+ +
+import java.util.Collections;
+import java.util.IdentityHashMap;
+import java.util.Map;
+import java.util.Objects; +import java.util.Objects;
+import java.util.Set; +import java.util.Set;
+import java.util.stream.Collectors; +import java.util.stream.Collectors;
@ -30,9 +33,10 @@ index 0000000000000000000000000000000000000000..d5d3c66afb408026513ac436eb1f4fdd
+ super(registry, tag); + super(registry, tag);
+ } + }
+ +
+ private static final Map<GameEvent, ResourceKey<net.minecraft.world.level.gameevent.GameEvent>> KEY_CACHE = Collections.synchronizedMap(new IdentityHashMap<>());
+ @Override + @Override
+ public boolean isTagged(@NotNull GameEvent item) { + public boolean isTagged(@NotNull GameEvent gameEvent) {
+ return registry.getHolderOrThrow(ResourceKey.create(Registry.GAME_EVENT_REGISTRY, CraftNamespacedKey.toMinecraft(item.getKey()))).is(tag); + return registry.getHolderOrThrow(KEY_CACHE.computeIfAbsent(gameEvent, event -> ResourceKey.create(Registry.GAME_EVENT_REGISTRY, CraftNamespacedKey.toMinecraft(event.getKey())))).is(tag);
+ } + }
+ +
+ @Override + @Override

View File

@ -0,0 +1,57 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
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<org.bukkit.block.Biome, net.minecraft.resources.ResourceKey<net.minecraft.world.level.biome.Biome>> BIOME_KEY_CACHE = Collections.synchronizedMap(new java.util.EnumMap<>(Biome.class)); // Paper
public static Holder<net.minecraft.world.level.biome.Biome> biomeToBiomeBase(net.minecraft.core.Registry<net.minecraft.world.level.biome.Biome> 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<net.minecraft.world.entity.EntityTy
super(registry, tag);
}
+ private static final java.util.Map<org.bukkit.entity.EntityType, net.minecraft.resources.ResourceKey<net.minecraft.world.entity.EntityType<?>>> 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<net.minecraft.world.level.material.F
super(registry, tag);
}
+ private static final java.util.Map<Fluid, net.minecraft.resources.ResourceKey<net.minecraft.world.level.material.Fluid>> 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