From 884caba0487a9184ca8d40ef691d23c0e9ada1fa Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 16 May 2024 17:48:45 +0700 Subject: [PATCH] Use PriorityQueue over AVLTreeSet with hacks --- .../ru/dbotthepony/mc/otm/core/util/TickList.kt | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/TickList.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/TickList.kt index d3d006ba2..c6532ba4e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/TickList.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/TickList.kt @@ -4,6 +4,7 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap import it.unimi.dsi.fastutil.objects.ObjectAVLTreeSet import org.apache.logging.log4j.LogManager import ru.dbotthepony.mc.otm.core.addSorted +import java.util.PriorityQueue import java.util.concurrent.atomic.AtomicInteger class TickList : ITickable { @@ -17,8 +18,7 @@ class TickList : ITickable { private val alwaysQueued = ArrayList() private val toRemoveFromAlways = ArrayList() - private val timers = ObjectAVLTreeSet() - private val nextTimerID = AtomicInteger() + private val timers = PriorityQueue() private val namedTimers = Object2ObjectOpenHashMap(0) private var shouldTick = false @@ -32,7 +32,6 @@ class TickList : ITickable { inner class Timer(val timerTicks: Int, val runnable: Runnable) : Comparable { val ringAt = ticks + timerTicks - val timerID = nextTimerID.incrementAndGet() var finished = false private set @@ -43,10 +42,7 @@ class TickList : ITickable { } override fun compareTo(other: Timer): Int { - var c = ringAt.compareTo(other.ringAt) - if (c != 0) return c - c = timerID.compareTo(other.timerID) - return c + return ringAt.compareTo(other.ringAt) } fun execute() { @@ -230,7 +226,7 @@ class TickList : ITickable { } while (timers.isNotEmpty()) { - val head = timers.first() + val head = timers.peek() if (head.ringAt <= ticks) { head.execute()