From 6b3e80b62977857d2c1170e619151d03f52236f6 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 18 Mar 2023 08:21:12 +0700 Subject: [PATCH] Make tick list even faster --- .../dbotthepony/mc/otm/core/util/TickList.kt | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) 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 703e349e8..492a1d766 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 @@ -19,6 +19,8 @@ class TickList : ITickable { var ticks = 0 private set + private var nothingToDo = true + inner class Timer(val timerTicks: Int, val runnable: Runnable) { val ringAt = ticks + timerTicks @@ -26,6 +28,8 @@ class TickList : ITickable { private set init { + nothingToDo = false + if (timers.isEmpty()) { timers.addLast(this) } else { @@ -68,6 +72,7 @@ class TickList : ITickable { conditionalValveTime.add(ticker) } else { conditional.addFirst(ticker) + nothingToDo = false } } @@ -85,6 +90,7 @@ class TickList : ITickable { onceValveTime.add(ticker) } else { once.addFirst(ticker) + nothingToDo = false } } @@ -102,6 +108,7 @@ class TickList : ITickable { alwaysValveTime.add(ticker) } else { always.add(ticker) + nothingToDo = false } } @@ -131,6 +138,10 @@ class TickList : ITickable { IConditionalTickable.wrap(tickerCondition, ticker), condition, reason) override fun tick() { + if (nothingToDo) { + return + } + if (inTicker) { throw ConcurrentModificationException("Already ticking") } @@ -139,6 +150,15 @@ class TickList : ITickable { inTicker = true try { + var nothingToDo = true + val conditional = conditional + val once = once + val always = always + val alwaysValveTime = alwaysValveTime + val conditionalValveTime = conditionalValveTime + val onceValveTime = onceValveTime + val timers = timers + if (conditional.isNotEmpty()) { val iterator = conditional.iterator() @@ -147,6 +167,7 @@ class TickList : ITickable { iterator.remove() } else { ticker.tick() + nothingToDo = false } } } @@ -163,11 +184,14 @@ class TickList : ITickable { for (ticker in always) { ticker.tick() } + + nothingToDo = false } if (alwaysValveTime.isNotEmpty()) { always.addAll(alwaysValveTime) alwaysValveTime.clear() + nothingToDo = false } if (conditionalValveTime.isNotEmpty()) { @@ -176,6 +200,7 @@ class TickList : ITickable { } conditionalValveTime.clear() + nothingToDo = false } if (onceValveTime.isNotEmpty()) { @@ -184,9 +209,11 @@ class TickList : ITickable { } onceValveTime.clear() + nothingToDo = false } while (timers.isNotEmpty()) { + nothingToDo = false val head = timers.first() if (head.ringAt <= ticks) { @@ -196,6 +223,8 @@ class TickList : ITickable { break } } + + this.nothingToDo = nothingToDo } finally { inTicker = false }