From 04c9d8ddfedc74e6e5bd399490410db83a7e3cc2 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 3 Oct 2022 18:16:01 +0700 Subject: [PATCH] Replace LinkedList with ArrayList in a lot of places --- .../mc/otm/android/AndroidResearchType.kt | 2 +- .../block/entity/MatteryWorkerBlockEntity.kt | 10 +++++++++- .../otm/block/entity/SynchronizedBlockEntity.kt | 4 ++-- .../ru/dbotthepony/mc/otm/core/TickList.kt | 9 ++++----- .../ru/dbotthepony/mc/otm/core/TimerQueue.kt | 6 ++---- .../mc/otm/network/FieldSynchronizer.kt | 17 +++++++++-------- 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/android/AndroidResearchType.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/android/AndroidResearchType.kt index ae8923dd7..10d6afeca 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/android/AndroidResearchType.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/android/AndroidResearchType.kt @@ -619,7 +619,7 @@ class AndroidResearchType( var iconText: Component? = null, ) { private val items = ArrayList>() - private val prerequisites = LinkedList() + private val prerequisites = ArrayList() private val blockers = ArrayList() private val features = ArrayList() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryWorkerBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryWorkerBlockEntity.kt index e08e04396..aacaa4206 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryWorkerBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryWorkerBlockEntity.kt @@ -105,8 +105,16 @@ abstract class MatteryWorkerBlockEntity( var throttleTicks = 0 protected set + protected open fun jobUpdated(oldJob: JobType?, newJob: JobType?) {} + var currentJob: JobType? = null - protected set + protected set(value) { + if (field != value) { + val old = field + field = value + jobUpdated(old, value) + } + } /** * Can be whatever you want, but [IdleReason] certainly contains all cases diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/SynchronizedBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/SynchronizedBlockEntity.kt index 2deeae9db..58c3372be 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/SynchronizedBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/SynchronizedBlockEntity.kt @@ -34,8 +34,8 @@ import java.util.LinkedList import java.util.WeakHashMap private data class Subscribers( - val blockEntities: LinkedList> = LinkedList(), - val players: LinkedList = LinkedList(), + val blockEntities: ArrayList> = ArrayList(0), + val players: ArrayList = ArrayList(1), val level: WeakReference, val chunkPos: Long, var changeset: Int = 0 diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/TickList.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/TickList.kt index d50029f83..8b1d635f9 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/TickList.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/TickList.kt @@ -1,15 +1,14 @@ package ru.dbotthepony.mc.otm.core import org.apache.logging.log4j.LogManager -import java.util.* import kotlin.ConcurrentModificationException class TickList { - private val conditional = LinkedList() - private val once = LinkedList() + private val conditional = ArrayDeque() + private val once = ArrayDeque() - private val conditionalValveTime = LinkedList() - private val onceValveTime = LinkedList() + private val conditionalValveTime = ArrayList() + private val onceValveTime = ArrayList() private var inTicker = false diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/TimerQueue.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/TimerQueue.kt index 8f357be6a..40e5ec0e7 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/TimerQueue.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/TimerQueue.kt @@ -1,10 +1,8 @@ package ru.dbotthepony.mc.otm.core -import java.util.LinkedList - class TimerQueue { private var ticks = 0 - private val list = LinkedList() + private val list = ArrayDeque() inner class Timer(val timerTicks: Int, val runnable: Runnable) { val ringAt = ticks + timerTicks @@ -54,7 +52,7 @@ class TimerQueue { ticks++ while (list.isNotEmpty()) { - val head = list.first + val head = list.first() if (head.ringAt <= ticks) { head.execute() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/network/FieldSynchronizer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/network/FieldSynchronizer.kt index 1251c302e..e081311d4 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/network/FieldSynchronizer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/network/FieldSynchronizer.kt @@ -3,6 +3,7 @@ package ru.dbotthepony.mc.otm.network import it.unimi.dsi.fastutil.io.FastByteArrayOutputStream import it.unimi.dsi.fastutil.objects.Reference2ObjectFunction import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap +import it.unimi.dsi.fastutil.objects.ReferenceArraySet import net.minecraft.world.item.ItemStack import ru.dbotthepony.mc.otm.core.* import java.io.DataInputStream @@ -73,8 +74,8 @@ class FieldSynchronizer(private val callback: Runnable, private val alwaysCallCa constructor() : this(Runnable {}, false) constructor(callback: Runnable) : this(callback, false) - private val fields = ArrayList>() - private val observers = LinkedList>() + private val fields = ArrayList>(0) + private val observers = ArrayList>(0) val isEmpty: Boolean get() = fields.isEmpty() val isNotEmpty: Boolean get() = fields.isNotEmpty() @@ -241,7 +242,7 @@ class FieldSynchronizer(private val callback: Runnable, private val alwaysCallCa ) } - private val endpoints = LinkedList>() + private val endpoints = ArrayList>() val defaultEndpoint = Endpoint() private var lastEndpointsCleanup = System.nanoTime() @@ -274,7 +275,7 @@ class FieldSynchronizer(private val callback: Runnable, private val alwaysCallCa inner class Endpoint { init { - endpoints.addLast(WeakReference(this)) + endpoints.add(WeakReference(this)) if (System.nanoTime() - lastEndpointsCleanup >= 60_000_000_000) { lastEndpointsCleanup = System.nanoTime() @@ -291,7 +292,9 @@ class FieldSynchronizer(private val callback: Runnable, private val alwaysCallCa } } - private val dirtyFields = LinkedList>() + private val dirtyFields = ReferenceArraySet>(0) + + // use LinkedList because it is ensured memory is freed on LinkedList#clear private val mapBacklogs = Reference2ObjectOpenHashMap, LinkedList Unit>>>() var unused: Boolean = false @@ -324,9 +327,7 @@ class FieldSynchronizer(private val callback: Runnable, private val alwaysCallCa return } - if (field !in dirtyFields) { - dirtyFields.addLast(field) - } + dirtyFields.add(field) } internal fun getMapBacklog(map: Map): LinkedList Unit>> {