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
parent 0ad6840c58
commit 1f665d8c9f
3 changed files with 67 additions and 4 deletions

View File

@@ -34,6 +34,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+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..00000000000000000000000000000000
+ private boolean registered;
+ private final RegistryKey<API, MINECRAFT> registryKey;
+ 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) {
+ this.registryKey = registryKey;
@@ -93,7 +95,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ 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