From 3ae994e70798ea09b77826d6445c009ef6abc344 Mon Sep 17 00:00:00 2001 From: hayanesuru Date: Sun, 4 May 2025 23:19:53 +0800 Subject: [PATCH] remove box on SpscIntQueue#recv --- .../java/org/dreeam/leaf/async/ai/AsyncGoalExecutor.java | 6 ++++-- .../java/org/dreeam/leaf/async/ai/AsyncGoalThread.java | 6 ++++-- .../java/org/dreeam/leaf/util/queue/SpscIntQueue.java | 8 +++++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/leaf-server/src/main/java/org/dreeam/leaf/async/ai/AsyncGoalExecutor.java b/leaf-server/src/main/java/org/dreeam/leaf/async/ai/AsyncGoalExecutor.java index 646dc638..9a656f4d 100644 --- a/leaf-server/src/main/java/org/dreeam/leaf/async/ai/AsyncGoalExecutor.java +++ b/leaf-server/src/main/java/org/dreeam/leaf/async/ai/AsyncGoalExecutor.java @@ -8,6 +8,7 @@ import org.apache.logging.log4j.Logger; import org.dreeam.leaf.config.modules.async.AsyncTargetFinding; import org.dreeam.leaf.util.queue.SpscIntQueue; +import java.util.OptionalInt; import java.util.concurrent.locks.LockSupport; public class AsyncGoalExecutor { @@ -55,10 +56,11 @@ public class AsyncGoalExecutor { public final void midTick() { while (true) { - Integer id = this.wake.recv(); - if (id == null) { + OptionalInt result = this.wake.recv(); + if (result.isEmpty()) { break; } + int id = result.getAsInt(); if (poll(id)) { submit(id); } diff --git a/leaf-server/src/main/java/org/dreeam/leaf/async/ai/AsyncGoalThread.java b/leaf-server/src/main/java/org/dreeam/leaf/async/ai/AsyncGoalThread.java index 6dbf6474..df3e3a0e 100644 --- a/leaf-server/src/main/java/org/dreeam/leaf/async/ai/AsyncGoalThread.java +++ b/leaf-server/src/main/java/org/dreeam/leaf/async/ai/AsyncGoalThread.java @@ -4,6 +4,7 @@ import net.minecraft.Util; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerLevel; +import java.util.OptionalInt; import java.util.concurrent.locks.LockSupport; public class AsyncGoalThread extends Thread { @@ -22,10 +23,11 @@ public class AsyncGoalThread extends Thread { for (ServerLevel level : server.getAllLevels()) { var exec = level.asyncGoalExecutor; while (true) { - Integer id = exec.queue.recv(); - if (id == null) { + OptionalInt result = exec.queue.recv(); + if (result.isEmpty()) { break; } + int id = result.getAsInt(); retry = true; if (exec.wake(id)) { while (!exec.wake.send(id)) { diff --git a/leaf-server/src/main/java/org/dreeam/leaf/util/queue/SpscIntQueue.java b/leaf-server/src/main/java/org/dreeam/leaf/util/queue/SpscIntQueue.java index 7e2f243a..e880136d 100644 --- a/leaf-server/src/main/java/org/dreeam/leaf/util/queue/SpscIntQueue.java +++ b/leaf-server/src/main/java/org/dreeam/leaf/util/queue/SpscIntQueue.java @@ -2,6 +2,8 @@ package org.dreeam.leaf.util.queue; import org.jetbrains.annotations.Nullable; +import java.util.OptionalInt; + /// Lock-free Single Producer Single Consumer Queue public class SpscIntQueue { @@ -35,14 +37,14 @@ public class SpscIntQueue { } - public final @Nullable Integer recv() { + public final OptionalInt recv() { final int idx = consumerIdx.getOpaque(); int cachedIdx = producerCachedIdx.getPlain(); if (idx == cachedIdx) { cachedIdx = producerIdx.getAcquire(); producerCachedIdx.setPlain(cachedIdx); if (idx == cachedIdx) { - return null; + return OptionalInt.empty(); } } int e = data[idx]; @@ -51,7 +53,7 @@ public class SpscIntQueue { nextIdx = 0; } consumerIdx.setRelease(nextIdx); - return e; + return OptionalInt.of(e); } public final int size() {