Skip to content
This repository has been archived by the owner on May 1, 2023. It is now read-only.

Commit

Permalink
fix: improve performance of tracker, remove redundant comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
peaches94 committed Jul 17, 2022
1 parent cbee00f commit 6202cf0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 34 deletions.
31 changes: 17 additions & 14 deletions patches/server/0003-feat-multithreaded-tracker.patch
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ that. this multithreaded tracker remains accurate, non-breaking and fast

diff --git a/src/main/java/host/bloom/tracker/MultithreadedTracker.java b/src/main/java/host/bloom/tracker/MultithreadedTracker.java
new file mode 100644
index 0000000000000000000000000000000000000000..e5f3cf027534fe87e8b7d5e9fd14604b2327c701
index 0000000000000000000000000000000000000000..31a9a1dbb68cdbc5dc8a83542ddc7e0467ee8fd3
--- /dev/null
+++ b/src/main/java/host/bloom/tracker/MultithreadedTracker.java
@@ -0,0 +1,125 @@
@@ -0,0 +1,128 @@
+package host.bloom.tracker;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
Expand Down Expand Up @@ -68,7 +68,7 @@ index 0000000000000000000000000000000000000000..e5f3cf027534fe87e8b7d5e9fd14604b
+
+ while (this.taskIndex.get() < this.entityTickingChunks.getListSize()) {
+ this.runMainThreadTasks();
+ this.handleTask(); // assist
+ this.handleTasks(5); // assist
+ }
+
+ while (this.finishedTasks.get() != parallelism) {
Expand All @@ -94,25 +94,28 @@ index 0000000000000000000000000000000000000000..e5f3cf027534fe87e8b7d5e9fd14604b
+
+ private void run() {
+ try {
+ while (handleTask()) ;
+ while (handleTasks(10));
+ } finally {
+ this.finishedTasks.incrementAndGet();
+ }
+ }
+
+ private boolean handleTask() {
+ private boolean handleTasks(int tasks) {
+ int index;
+ while ((index = this.taskIndex.getAndIncrement()) < this.entityTickingChunks.getListSize()) {
+ LevelChunk chunk = this.entityTickingChunks.rawGet(index);
+ if (chunk != null) {
+ try {
+ this.processChunk(chunk);
+ } catch (Throwable throwable) {
+ MinecraftServer.LOGGER.warn("Ticking tracker failed", throwable);
+ }
+ while ((index = this.taskIndex.getAndAdd(tasks)) < this.entityTickingChunks.getListSize()) {
+ for (int i = index; i < index + tasks && i < this.entityTickingChunks.getListSize(); i++) {
+ LevelChunk chunk = this.entityTickingChunks.rawGet(i);
+ if (chunk != null) {
+ try {
+ this.processChunk(chunk);
+ } catch (Throwable throwable) {
+ MinecraftServer.LOGGER.warn("Ticking tracker failed", throwable);
+ }
+
+ return true;
+ }
+ }
+
+ return true;
+ }
+
+ return false;
Expand Down
20 changes: 0 additions & 20 deletions patches/server/0005-feat-reduce-sensor-work.patch
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,3 @@ index 34db1bd524bb97fbbe0f86b088a2ac343e730f5e..1df1e912194c4d2b934859d999e6d09e

if (i % 2 != 0 && this.tickCount > 1) {
this.level.getProfiler().push("targetSelector");
diff --git a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java
index d8cf99a3014a4b8152ae819fa663c2fdf34dce57..6cce3def9c52cb365c7e831be42eceb1e0710cb4 100644
--- a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java
+++ b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java
@@ -18,7 +18,14 @@ public class NearestLivingEntitySensor<T extends LivingEntity> extends Sensor<T>
List<LivingEntity> list = world.getEntitiesOfClass(LivingEntity.class, aABB, (e) -> {
return e != entity && e.isAlive();
});
- list.sort(Comparator.comparingDouble(entity::distanceToSqr));
+ // petal start - use manual comparison because we don't need the accuracy of Double.compare
+ list.sort((e1, e2) -> {
+ double dist1 = entity.distanceToSqr(e1), dist2 = entity.distanceToSqr(e2);
+ if (dist1 < dist2) return -1;
+ if (dist1 > dist2) return 1;
+ return 0;
+ });
+ // petal end
Brain<?> brain = entity.getBrain();
brain.setMemory(MemoryModuleType.NEAREST_LIVING_ENTITIES, list);
brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, new NearestVisibleLivingEntities(entity, list));

0 comments on commit 6202cf0

Please sign in to comment.