From dcaaa5f62389858917996440c4f7f1b842ca2a11 Mon Sep 17 00:00:00 2001 From: NanaChiyo0721 Date: Wed, 29 Jul 2026 23:13:20 +0800 Subject: [PATCH] RegionizedTaskQueue queue TTL optimization --- ...izedTaskQueue-queue-TTL-optimization.patch | 143 ++++++++++++++++++ ...cket-processing-queue-with-mt-queue.patch} | 0 ...ync-protocol-switching-optimization.patch} | 0 ...0030-Improved-server-health-command.patch} | 0 ...iju-Entity-tick-and-removal-limiter.patch} | 0 ...ju-Vanilla-end-portal-teleportation.patch} | 0 ...-to-support-for-long-command-inputs.patch} | 0 ...0034-Add-config-for-server-mod-name.patch} | 4 +- ...=> 0035-Add-config-for-cpu-affinity.patch} | 0 ...Add-config-for-unsafe-teleportation.patch} | 0 ...037-Add-config-for-watchdog-timeout.patch} | 0 ...fig-to-disable-entity-tick-catchers.patch} | 0 ...39-Add-config-for-heightmap-warning.patch} | 0 ...atch => 0040-Add-config-gui-support.patch} | 0 ...e-data-command-to-be-enabled-config.patch} | 0 ...config-for-command-block-command-ex.patch} | 0 ...-config-to-modify-tripwire-behavior.patch} | 0 ... 0044-Add-config-for-item-multitask.patch} | 0 ...5-Add-config-to-enable-tick-command.patch} | 0 ...-for-whether-to-save-portal-tickets.patch} | 0 ...tch => 0047-Add-portal-rate-limiter.patch} | 6 +- ...us-bar.patch => 0048-Add-status-bar.patch} | 4 +- ...patch => 0049-Leaves-Vanilla-Hopper.patch} | 0 ...eaves-Lithium-Sleeping-Block-Entity.patch} | 0 ...ch => 0051-Petal-Reduce-sensor-work.patch} | 0 ...2-Purpur-Lobotomize-stuck-villagers.patch} | 0 ...ish-Reduce-projectile-chunk-loading.patch} | 0 ...le-goal-selector-during-inactive-ti.patch} | 0 ...Secure-seed-and-matter-seed-command.patch} | 0 ... => 0056-Secure-seed-V2-with-Blake3.patch} | 0 ...rain-maps-with-optimized-collection.patch} | 0 ...ss-creating-stats-json-bases-on-pla.patch} | 0 ...mize-PatchedDataComponentMap-equals.patch} | 0 ...r-checking-for-useless-move-packets.patch} | 0 ...ch => 0061-Leaf-fast-bit-radix-sort.patch} | 0 ...Configurable-vanilla-username-check.patch} | 2 +- ...63-Leaf-Lithium-faster-hash-palette.patch} | 0 ...cket-limit-Shiroha-Option-to-full-d.patch} | 0 ...ves-Configurable-collision-behavior.patch} | 0 ...066-Leaves-Optimized-dragon-respawn.patch} | 0 ...le-Variable-entity-wake-up-duration.patch} | 0 ...ttributes-with-optimized-collection.patch} | 0 ...kip-entity-move-if-movement-is-zero.patch} | 0 ...0-Gale-Store-mob-counts-in-an-array.patch} | 0 ...imize-random-calls-in-chunk-ticking.patch} | 0 ...uce-enderman-teleport-chunk-lookups.patch} | 0 ...> 0073-Gale-Cache-ShapePairKey-hash.patch} | 0 ...n-check-has-physics-before-same-veh.patch} | 0 ...ible-planar-movement-multiplication.patch} | 0 ...-Gale-Optimize-matching-item-checks.patch} | 0 ...a-and-Optional-allocation-in-Entity.patch} | 0 ... 0078-Gale-Reduce-array-allocations.patch} | 0 ...079-Some-optimizations-from-krypton.patch} | 0 ...h => 0080-Frustum-filtering-tracker.patch} | 0 ...imized-collections-for-WeightedList.patch} | 0 ...m-optimized-non-poi-block-searching.patch} | 0 56 files changed, 151 insertions(+), 8 deletions(-) create mode 100644 shiroha-server/minecraft-patches/features/0027-RegionizedTaskQueue-queue-TTL-optimization.patch rename shiroha-server/minecraft-patches/features/{0027-Replace-packet-processing-queue-with-mt-queue.patch => 0028-Replace-packet-processing-queue-with-mt-queue.patch} (100%) rename shiroha-server/minecraft-patches/features/{0028-Async-protocol-switching-optimization.patch => 0029-Async-protocol-switching-optimization.patch} (100%) rename shiroha-server/minecraft-patches/features/{0029-Improved-server-health-command.patch => 0030-Improved-server-health-command.patch} (100%) rename shiroha-server/minecraft-patches/features/{0030-Kaiiju-Entity-tick-and-removal-limiter.patch => 0031-Kaiiju-Entity-tick-and-removal-limiter.patch} (100%) rename shiroha-server/minecraft-patches/features/{0031-Kaiiju-Vanilla-end-portal-teleportation.patch => 0032-Kaiiju-Vanilla-end-portal-teleportation.patch} (100%) rename shiroha-server/minecraft-patches/features/{0032-Add-config-to-support-for-long-command-inputs.patch => 0033-Add-config-to-support-for-long-command-inputs.patch} (100%) rename shiroha-server/minecraft-patches/features/{0033-Add-config-for-server-mod-name.patch => 0034-Add-config-for-server-mod-name.patch} (84%) rename shiroha-server/minecraft-patches/features/{0034-Add-config-for-cpu-affinity.patch => 0035-Add-config-for-cpu-affinity.patch} (100%) rename shiroha-server/minecraft-patches/features/{0035-Add-config-for-unsafe-teleportation.patch => 0036-Add-config-for-unsafe-teleportation.patch} (100%) rename shiroha-server/minecraft-patches/features/{0036-Add-config-for-watchdog-timeout.patch => 0037-Add-config-for-watchdog-timeout.patch} (100%) rename shiroha-server/minecraft-patches/features/{0037-Add-config-to-disable-entity-tick-catchers.patch => 0038-Add-config-to-disable-entity-tick-catchers.patch} (100%) rename shiroha-server/minecraft-patches/features/{0038-Add-config-for-heightmap-warning.patch => 0039-Add-config-for-heightmap-warning.patch} (100%) rename shiroha-server/minecraft-patches/features/{0039-Add-config-gui-support.patch => 0040-Add-config-gui-support.patch} (100%) rename shiroha-server/minecraft-patches/features/{0040-Add-force-the-data-command-to-be-enabled-config.patch => 0041-Add-force-the-data-command-to-be-enabled-config.patch} (100%) rename shiroha-server/minecraft-patches/features/{0041-Add-experimental-config-for-command-block-command-ex.patch => 0042-Add-experimental-config-for-command-block-command-ex.patch} (100%) rename shiroha-server/minecraft-patches/features/{0042-Add-config-to-modify-tripwire-behavior.patch => 0043-Add-config-to-modify-tripwire-behavior.patch} (100%) rename shiroha-server/minecraft-patches/features/{0043-Add-config-for-item-multitask.patch => 0044-Add-config-for-item-multitask.patch} (100%) rename shiroha-server/minecraft-patches/features/{0044-Add-config-to-enable-tick-command.patch => 0045-Add-config-to-enable-tick-command.patch} (100%) rename shiroha-server/minecraft-patches/features/{0045-Add-config-for-whether-to-save-portal-tickets.patch => 0046-Add-config-for-whether-to-save-portal-tickets.patch} (100%) rename shiroha-server/minecraft-patches/features/{0046-Add-portal-rate-limiter.patch => 0047-Add-portal-rate-limiter.patch} (96%) rename shiroha-server/minecraft-patches/features/{0047-Add-status-bar.patch => 0048-Add-status-bar.patch} (99%) rename shiroha-server/minecraft-patches/features/{0048-Leaves-Vanilla-Hopper.patch => 0049-Leaves-Vanilla-Hopper.patch} (100%) rename shiroha-server/minecraft-patches/features/{0049-Leaves-Lithium-Sleeping-Block-Entity.patch => 0050-Leaves-Lithium-Sleeping-Block-Entity.patch} (100%) rename shiroha-server/minecraft-patches/features/{0050-Petal-Reduce-sensor-work.patch => 0051-Petal-Reduce-sensor-work.patch} (100%) rename shiroha-server/minecraft-patches/features/{0051-Purpur-Lobotomize-stuck-villagers.patch => 0052-Purpur-Lobotomize-stuck-villagers.patch} (100%) rename shiroha-server/minecraft-patches/features/{0052-Pufferfish-Reduce-projectile-chunk-loading.patch => 0053-Pufferfish-Reduce-projectile-chunk-loading.patch} (100%) rename shiroha-server/minecraft-patches/features/{0053-Pufferfish-Throttle-goal-selector-during-inactive-ti.patch => 0054-Pufferfish-Throttle-goal-selector-during-inactive-ti.patch} (100%) rename shiroha-server/minecraft-patches/features/{0054-Leaf-Secure-seed-and-matter-seed-command.patch => 0055-Leaf-Secure-seed-and-matter-seed-command.patch} (100%) rename shiroha-server/minecraft-patches/features/{0055-Secure-seed-V2-with-Blake3.patch => 0056-Secure-seed-V2-with-Blake3.patch} (100%) rename shiroha-server/minecraft-patches/features/{0056-Leaf-Replace-brain-maps-with-optimized-collection.patch => 0057-Leaf-Replace-brain-maps-with-optimized-collection.patch} (100%) rename shiroha-server/minecraft-patches/features/{0057-Leaf-Remove-useless-creating-stats-json-bases-on-pla.patch => 0058-Leaf-Remove-useless-creating-stats-json-bases-on-pla.patch} (100%) rename shiroha-server/minecraft-patches/features/{0058-Leaf-Optimize-PatchedDataComponentMap-equals.patch => 0059-Leaf-Optimize-PatchedDataComponentMap-equals.patch} (100%) rename shiroha-server/minecraft-patches/features/{0059-Leaf-Better-checking-for-useless-move-packets.patch => 0060-Leaf-Better-checking-for-useless-move-packets.patch} (100%) rename shiroha-server/minecraft-patches/features/{0060-Leaf-fast-bit-radix-sort.patch => 0061-Leaf-fast-bit-radix-sort.patch} (100%) rename shiroha-server/minecraft-patches/features/{0061-Leaf-Configurable-vanilla-username-check.patch => 0062-Leaf-Configurable-vanilla-username-check.patch} (99%) rename shiroha-server/minecraft-patches/features/{0062-Leaf-Lithium-faster-hash-palette.patch => 0063-Leaf-Lithium-faster-hash-palette.patch} (100%) rename shiroha-server/minecraft-patches/features/{0063-Leaves-Disable-packet-limit-Shiroha-Option-to-full-d.patch => 0064-Leaves-Disable-packet-limit-Shiroha-Option-to-full-d.patch} (100%) rename shiroha-server/minecraft-patches/features/{0064-Leaves-Configurable-collision-behavior.patch => 0065-Leaves-Configurable-collision-behavior.patch} (100%) rename shiroha-server/minecraft-patches/features/{0065-Leaves-Optimized-dragon-respawn.patch => 0066-Leaves-Optimized-dragon-respawn.patch} (100%) rename shiroha-server/minecraft-patches/features/{0066-Gale-Variable-entity-wake-up-duration.patch => 0067-Gale-Variable-entity-wake-up-duration.patch} (100%) rename shiroha-server/minecraft-patches/features/{0067-Gale-Replace-AI-attributes-with-optimized-collection.patch => 0068-Gale-Replace-AI-attributes-with-optimized-collection.patch} (100%) rename shiroha-server/minecraft-patches/features/{0068-Gale-Skip-entity-move-if-movement-is-zero.patch => 0069-Gale-Skip-entity-move-if-movement-is-zero.patch} (100%) rename shiroha-server/minecraft-patches/features/{0069-Gale-Store-mob-counts-in-an-array.patch => 0070-Gale-Store-mob-counts-in-an-array.patch} (100%) rename shiroha-server/minecraft-patches/features/{0070-Gale-Optimize-random-calls-in-chunk-ticking.patch => 0071-Gale-Optimize-random-calls-in-chunk-ticking.patch} (100%) rename shiroha-server/minecraft-patches/features/{0071-Gale-Reduce-enderman-teleport-chunk-lookups.patch => 0072-Gale-Reduce-enderman-teleport-chunk-lookups.patch} (100%) rename shiroha-server/minecraft-patches/features/{0072-Gale-Cache-ShapePairKey-hash.patch => 0073-Gale-Cache-ShapePairKey-hash.patch} (100%) rename shiroha-server/minecraft-patches/features/{0073-Gale-For-collision-check-has-physics-before-same-veh.patch => 0074-Gale-For-collision-check-has-physics-before-same-veh.patch} (100%) rename shiroha-server/minecraft-patches/features/{0074-Gale-Skip-negligible-planar-movement-multiplication.patch => 0075-Gale-Skip-negligible-planar-movement-multiplication.patch} (100%) rename shiroha-server/minecraft-patches/features/{0075-Gale-Optimize-matching-item-checks.patch => 0076-Gale-Optimize-matching-item-checks.patch} (100%) rename shiroha-server/minecraft-patches/features/{0076-Gale-Reduce-lambda-and-Optional-allocation-in-Entity.patch => 0077-Gale-Reduce-lambda-and-Optional-allocation-in-Entity.patch} (100%) rename shiroha-server/minecraft-patches/features/{0077-Gale-Reduce-array-allocations.patch => 0078-Gale-Reduce-array-allocations.patch} (100%) rename shiroha-server/minecraft-patches/features/{0078-Some-optimizations-from-krypton.patch => 0079-Some-optimizations-from-krypton.patch} (100%) rename shiroha-server/minecraft-patches/features/{0079-Frustum-filtering-tracker.patch => 0080-Frustum-filtering-tracker.patch} (100%) rename shiroha-server/minecraft-patches/features/{0080-Lithium-optimized-collections-for-WeightedList.patch => 0081-Lithium-optimized-collections-for-WeightedList.patch} (100%) rename shiroha-server/minecraft-patches/features/{0081-Lithium-optimized-non-poi-block-searching.patch => 0082-Lithium-optimized-non-poi-block-searching.patch} (100%) diff --git a/shiroha-server/minecraft-patches/features/0027-RegionizedTaskQueue-queue-TTL-optimization.patch b/shiroha-server/minecraft-patches/features/0027-RegionizedTaskQueue-queue-TTL-optimization.patch new file mode 100644 index 0000000..cce80f3 --- /dev/null +++ b/shiroha-server/minecraft-patches/features/0027-RegionizedTaskQueue-queue-TTL-optimization.patch @@ -0,0 +1,143 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: NanaChiyo0721 +Date: Wed, 29 Jul 2026 18:39:56 +0800 +Subject: [PATCH] RegionizedTaskQueue queue TTL optimization + + +diff --git a/io/papermc/paper/threadedregions/RegionizedTaskQueue.java b/io/papermc/paper/threadedregions/RegionizedTaskQueue.java +index 037697e6364c73057b1fbac354bd78d8bf35bc7b..0e88bc2776615f8e3e1e6a05c5ffb8f791d49917 100644 +--- a/io/papermc/paper/threadedregions/RegionizedTaskQueue.java ++++ b/io/papermc/paper/threadedregions/RegionizedTaskQueue.java +@@ -23,6 +23,7 @@ import java.util.concurrent.atomic.AtomicLong; + public final class RegionizedTaskQueue { + + private static final TicketType TASK_QUEUE_TICKET = ChunkSystemTicketType.create("task_queue_ticket", Long::compareTo); ++ private static final long QUEUE_MAX_TTL_TICKS = 5L; // Shiroha - RegionizedTaskQueue queue TTL optimization + + public PrioritisedExecutor.PrioritisedTask createChunkTask(final ServerLevel world, final int chunkX, final int chunkZ, + final Runnable run) { +@@ -155,6 +156,58 @@ public final class RegionizedTaskQueue { + } + } + ++ // Shiroha start - RegionizedTaskQueue queue TTL optimization ++ public void tickQueueReferenceTTL() { ++ final ThreadedRegionizer.ThreadedRegion currentRegion ++ = io.papermc.paper.threadedregions.TickRegionScheduler.getCurrentRegion(); ++ if (currentRegion == null) { ++ return; ++ } ++ ++ final ReferenceCountData[] toRemoveTicket = new ReferenceCountData[1]; ++ ++ for (ConcurrentChainedLong2ReferenceHashTable.TableEntry counterEntry : this.referenceCounters.entrySet()) { ++ final long coord = counterEntry.getKey(); ++ final ReferenceCountData counterData = counterEntry.getValue(); ++ ++ // only tick for our region ++ if (currentRegion == this.world.regioniser.getRegionAtUnsynchronised(CoordinateUtils.getChunkX(coord), CoordinateUtils.getChunkZ(coord))) { ++ long curr = counterData.referenceTTL.get(); ++ // successfully decreased ttl ++ if (curr == (curr = counterData.referenceTTL.compareAndExchange(curr, curr - 1))) { ++ if (counterData.referenceCount.get() != 0L) { ++ // still has reference, pump back ++ counterData.referenceTTL.set(QUEUE_MAX_TTL_TICKS); ++ continue; ++ } ++ ++ // dead ++ if (curr <= 0) { ++ // parsed from decrementReference ++ this.referenceCounters.computeIfPresent(coord, (final long keyInMap, final ReferenceCountData valueInMap) -> { ++ // might be increased again ++ if (valueInMap.referenceCount.get() != 0L) { ++ valueInMap.referenceTTL.set(QUEUE_MAX_TTL_TICKS); // still has reference, pump back ++ return valueInMap; // directly, the ttl was already charged in add logic ++ } ++ ++ // note: valueInMap may not be referenceCountData ++ toRemoveTicket[0] = valueInMap; ++ ++ return null; ++ }); ++ ++ if (toRemoveTicket[0] != null) { ++ this.removeTicket(coord, toRemoveTicket[0].id); ++ toRemoveTicket[0] = null; ++ } ++ } ++ } ++ } ++ } ++ } ++ // Shiroha end - RegionizedTaskQueue queue TTL optimization ++ + private void decrementReference(final ReferenceCountData referenceCountData, final long coord) { + if (!referenceCountData.decreaseReferenceCount()) { + return; +@@ -212,9 +265,10 @@ public final class RegionizedTaskQueue { + private final long id = ID_GENERATOR.getAndIncrement(); + + public final AtomicLong referenceCount = new AtomicLong(1L); ++ public final AtomicLong referenceTTL = new AtomicLong(QUEUE_MAX_TTL_TICKS); // Shiroha - RegionizedTaskQueue queue TTL optimization + public volatile boolean addedTicket; + +- // returns false if reference count is 0, otherwise increments ref count ++ // returns false if reference count or ttl is 0, otherwise increments ref count // Shiroha - RegionizedTaskQueue queue TTL optimization + public boolean addCount() { + int failures = 0; + for (long curr = this.referenceCount.get();;) { +@@ -227,6 +281,27 @@ public final class RegionizedTaskQueue { + } + + if (curr == (curr = this.referenceCount.compareAndExchange(curr, curr + 1L))) { ++ // Shiroha start - RegionizedTaskQueue queue TTL optimization ++ // now force charge back the ttl to max ++ int ttlFailures = 0; ++ for (long currTTL = this.referenceTTL.get();;) { ++ for (int i = 0; i < ttlFailures; i++) { ++ Thread.onSpinWait(); ++ } ++ ++ // add failed, rollback (ttl reached) ++ if (currTTL <= 0) { ++ this.referenceCount.decrementAndGet(); // revert the addition ++ return false; ++ } ++ ++ if (currTTL == (currTTL = this.referenceTTL.compareAndExchange(currTTL, QUEUE_MAX_TTL_TICKS))) { ++ break; ++ } ++ ++ ++ttlFailures; ++ } ++ // Shiroha end - RegionizedTaskQueue queue TTL optimization + return true; + } + +@@ -234,11 +309,11 @@ public final class RegionizedTaskQueue { + } + } + +- // returns true if new reference count is 0 ++ // returns true if new reference count and ttl is 0 // Shiroha - RegionizedTaskQueue queue TTL optimization + public boolean decreaseReferenceCount() { + final long res = this.referenceCount.decrementAndGet(); + if (res >= 0L) { +- return res == 0L; ++ return res == 0L && this.referenceTTL.get() <= 0L; // Shiroha - RegionizedTaskQueue queue TTL optimization + } else { + throw new IllegalStateException("Negative reference count"); + } +diff --git a/net/minecraft/server/MinecraftServer.java b/net/minecraft/server/MinecraftServer.java +index 3e62b4beb0e5ada82f83b0ebec35ca21a072a2e2..63fecdde56650fb056fa8de352abdf927b0549f8 100644 +--- a/net/minecraft/server/MinecraftServer.java ++++ b/net/minecraft/server/MinecraftServer.java +@@ -1737,6 +1737,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop localPlayers = new CopyOnWriteArrayList<>(); diff --git a/net/minecraft/server/MinecraftServer.java b/net/minecraft/server/MinecraftServer.java -index 6025675b7004c9700ddc26638761f76d64ecc026..5be3e493f1638cc0befe5530c58ea15467a3a7ac 100644 +index 3ecedf303384c172a5e2a19c9e6ac0c3a43cef74..ee7fce1e4e9b94b1f9c956c376fff0abee51b0ca 100644 --- a/net/minecraft/server/MinecraftServer.java +++ b/net/minecraft/server/MinecraftServer.java -@@ -1932,6 +1932,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop level + " " + level.dimension().identifier()); profiler.push("tick"); @@ -82,7 +82,7 @@ index 6025675b7004c9700ddc26638761f76d64ecc026..5be3e493f1638cc0befe5530c58ea154 try { foliaProfiler.startTimer(level.tickTimerId); try { // Folia - profiler -@@ -1946,6 +1949,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop