9
0
mirror of https://github.com/Samsuik/Sakura.git synced 2025-12-22 16:29:16 +00:00
Files
SakuraMC/patches/server/0087-Improve-paper-biome-cache-locking.patch
2024-12-02 22:12:07 +00:00

43 lines
2.6 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samsuik <kfian294ma4@gmail.com>
Date: Mon, 2 Dec 2024 21:46:31 +0000
Subject: [PATCH] Improve paper biome cache locking
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
index 962c950ca9c7e047a3aec215d4faa73676049d36..f9f2119f6d48305641c088f76e2ef878fc776fe0 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
@@ -368,13 +368,28 @@ 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
+ // Sakura start - improve paper biome cache locking
+ private static final java.util.Map<org.bukkit.block.Biome, net.minecraft.resources.ResourceKey<net.minecraft.world.level.biome.Biome>> BIOME_KEY_CACHE = new java.util.EnumMap<>(Biome.class); // Paper
+ private static final java.util.concurrent.locks.StampedLock BIOME_KEY_CACHE_LOCK = new java.util.concurrent.locks.StampedLock();
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(BIOME_KEY_CACHE.computeIfAbsent(bio, b -> ResourceKey.create(Registries.BIOME, CraftNamespacedKey.toMinecraft(b.getKey())))); // Paper - cache key
+ // NOTE: Could get rid of the map by splitting up this patch and storing a reference in Biome (under api)
+ long stamp = BIOME_KEY_CACHE_LOCK.tryOptimisticRead();
+ ResourceKey<net.minecraft.world.level.biome.Biome> biomeKey = null;
+ try {
+ biomeKey = BIOME_KEY_CACHE.get(bio);
+ } catch (Exception ignored) {
+ // precaution
+ }
+ if (biomeKey == null || !BIOME_KEY_CACHE_LOCK.validate(stamp)) {
+ stamp = BIOME_KEY_CACHE_LOCK.writeLock();
+ biomeKey = BIOME_KEY_CACHE.computeIfAbsent(bio, b -> ResourceKey.create(Registries.BIOME, CraftNamespacedKey.toMinecraft(b.getKey()))); // Paper - cache key
+ BIOME_KEY_CACHE_LOCK.unlockWrite(stamp);
+ }
+ return registry.getHolderOrThrow(biomeKey);
+ // Sakura end - improve paper biome cache locking
}
@Override