Files
Shiroha/shiroha-server/minecraft-patches/features/0030-Reduce-allocation-in-PoiAccess.patch
T

111 lines
5.8 KiB
Diff
Raw Normal View History

2026-08-05 23:57:15 +08:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: NanaChiyo0721 <nanachiyo0721@163.com>
Date: Wed, 5 Aug 2026 23:53:48 +0800
Subject: [PATCH] Reduce allocation in PoiAccess
diff --git a/ca/spottedleaf/moonrise/patches/poi_lookup/PoiAccess.java b/ca/spottedleaf/moonrise/patches/poi_lookup/PoiAccess.java
index 85d80b3952a76b0ca7f67401a659cf35dd55b082..d4f95458d2e35aedb0fd5230fcd74b968437d8a5 100644
--- a/ca/spottedleaf/moonrise/patches/poi_lookup/PoiAccess.java
+++ b/ca/spottedleaf/moonrise/patches/poi_lookup/PoiAccess.java
@@ -89,6 +89,23 @@ public final class PoiAccess {
return (centerDiffX * centerDiffX) + (centerDiffY * centerDiffY) + (centerDiffZ * centerDiffZ);
}
+ // Shiroha start- Reduce allocation in PoiAccess
+ private static int getSectionIndex(
+ final int x,
+ final int y,
+ final int z,
+ final int lowerX,
+ final int lowerY,
+ final int lowerZ,
+ final int spanY,
+ final int spanZ
+ ) {
+ return (x - lowerX) * spanY * spanZ
+ + (y - lowerY) * spanZ
+ + (z - lowerZ);
+ }
+ // Shiroha end - Reduce allocation in PoiAccess
+
public static void findClosestPoiDataRecords(final PoiManager poiStorage,
final Predicate<Holder<PoiType>> villagePlaceType,
// position predicate must not modify chunk POI
@@ -116,9 +133,15 @@ public final class PoiAccess {
final int centerZ = sourcePosition.getZ() >> 4;
final long centerKey = CoordinateUtils.getChunkSectionKey(centerX, centerY, centerZ);
+ // Shiroha start - Reduce allocation in PoiAccess
+ final int spanX = upperX - lowerX + 1;
+ final int spanY = upperY - lowerY + 1;
+ final int spanZ = upperZ - lowerZ + 1;
+ // Shiroha end - Reduce allocation in PoiAccess
+
final LongArrayFIFOQueue queue = new LongArrayFIFOQueue();
- final LongOpenHashSet seen = new LongOpenHashSet();
- seen.add(centerKey);
+ final java.util.BitSet seen = new java.util.BitSet(spanX * spanY * spanZ); // Shiroha - Reduce allocation in PoiAccess
+ seen.set(getSectionIndex(centerX, centerY, centerZ, lowerX, lowerY, lowerZ, spanY, spanZ)); // Shiroha - Reduce allocation in PoiAccess
queue.enqueue(centerKey);
while (!queue.isEmpty()) {
@@ -159,8 +182,18 @@ public final class PoiAccess {
final int neighbourY = sectionY + dy;
final int neighbourZ = sectionZ + dz;
+ // Shiroha start - Reduce allocation in PoiAccess
+ if (neighbourX < lowerX || neighbourX > upperX
+ || neighbourY < lowerY || neighbourY > upperY
+ || neighbourZ < lowerZ || neighbourZ > upperZ) {
+ continue;
+ }
+
+ final int neighbourIndex = getSectionIndex(neighbourX, neighbourY, neighbourZ, lowerX, lowerY, lowerZ, spanY, spanZ);
+ // Shiroha end - Reduce allocation in PoiAccess
final long neighbourKey = CoordinateUtils.getChunkSectionKey(neighbourX, neighbourY, neighbourZ);
- if (seen.add(neighbourKey)) {
+ if (!seen.get(neighbourIndex)) { // Shiroha - Reduce allocation in PoiAccess
+ seen.set(neighbourIndex); // Shiroha - Reduce allocation in PoiAccess
queue.enqueue(neighbourKey);
}
}
@@ -359,9 +392,15 @@ public final class PoiAccess {
final int centerZ = sourcePosition.getZ() >> 4;
final long centerKey = CoordinateUtils.getChunkSectionKey(centerX, centerY, centerZ);
+ // Shiroha start - Reduce allocation in PoiAccess
+ final int spanX = upperX - lowerX + 1;
+ final int spanY = upperY - lowerY + 1;
+ final int spanZ = upperZ - lowerZ + 1;
+ // Shiroha end - Reduce allocation in PoiAccess
+
final LongArrayFIFOQueue queue = new LongArrayFIFOQueue();
- final LongOpenHashSet seen = new LongOpenHashSet();
- seen.add(centerKey);
+ final java.util.BitSet seen = new java.util.BitSet(spanX * spanY * spanZ); // Shiroha - Reduce allocation in PoiAccess
+ seen.set(getSectionIndex(centerX, centerY, centerZ, lowerX, lowerY, lowerZ, spanY, spanZ)); // Shiroha - Reduce allocation in PoiAccess
queue.enqueue(centerKey);
while (!queue.isEmpty()) {
@@ -403,8 +442,18 @@ public final class PoiAccess {
final int neighbourY = sectionY + dy;
final int neighbourZ = sectionZ + dz;
+ // Shiroha start - Reduce allocation in PoiAccess
+ if (neighbourX < lowerX || neighbourX > upperX
+ || neighbourY < lowerY || neighbourY > upperY
+ || neighbourZ < lowerZ || neighbourZ > upperZ) {
+ continue;
+ }
+
+ final int neighbourIndex = getSectionIndex(neighbourX, neighbourY, neighbourZ, lowerX, lowerY, lowerZ, spanY, spanZ);
+ // Shiroha end - Reduce allocation in PoiAccess
final long neighbourKey = CoordinateUtils.getChunkSectionKey(neighbourX, neighbourY, neighbourZ);
- if (seen.add(neighbourKey)) {
+ if (!seen.get(neighbourIndex)) { // Shiroha - Reduce allocation in PoiAccess
+ seen.set(neighbourIndex); // Shiroha - Reduce allocation in PoiAccess
queue.enqueue(neighbourKey);
}
}