Skip to content

Commit 5fd5a05

Browse files
committed
arraylist over linkedlist to avoid excessive collection copying
1 parent f3b4776 commit 5fd5a05

File tree

1 file changed

+9
-7
lines changed
  • common/src/main/kotlin/com/lambda/module/modules/player

1 file changed

+9
-7
lines changed

common/src/main/kotlin/com/lambda/module/modules/player/PacketMine.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import net.minecraft.util.math.Box
4848
import java.awt.Color
4949
import java.util.*
5050
import java.util.concurrent.ConcurrentLinkedQueue
51+
import kotlin.collections.ArrayList
5152

5253
object PacketMine : Module(
5354
"PacketMine",
@@ -84,11 +85,11 @@ object PacketMine : Module(
8485
private var itemDrops = 0
8586

8687
private val breakPositions = arrayOfNulls<BlockPos>(2)
87-
private val queuePositions = LinkedList<MutableCollection<BlockPos>>()
88+
private val queuePositions = ArrayList<MutableCollection<BlockPos>>()
8889
private val queueSorted
8990
get() = when (queueOrder) {
9091
QueueOrder.Standard -> queuePositions
91-
QueueOrder.Reversed -> queuePositions.reversed()
92+
QueueOrder.Reversed -> queuePositions.asReversed()
9293
}.flatten()
9394

9495
private var reBreakPos: BlockPos? = null
@@ -118,17 +119,18 @@ object PacketMine : Module(
118119
}
119120
if (positions.isEmpty()) return@listen
120121
val activeBreaking = if (queue) {
121-
queuePositions.addLast(positions)
122+
queuePositions.add(positions)
122123
breakPositions.toList() + queueSorted
123124
} else {
124125
queuePositions.clear()
125-
queuePositions.addLast(positions)
126+
queuePositions.add(positions)
126127
queuePositions.flatten() + if (breakConfig.doubleBreak) {
127128
breakPositions[1] ?: breakPositions[0]
128129
} else null
129130
}
130131
requestBreakManager(activeBreaking)
131132
attackedThisTick = true
133+
queuePositions.trimToSize()
132134
}
133135

134136
listen<TickEvent.Input.Post> {
@@ -213,7 +215,7 @@ object PacketMine : Module(
213215
}
214216
}
215217

216-
private fun LinkedList<MutableCollection<BlockPos>>.removePos(element: BlockPos): Boolean {
218+
private fun ArrayList<MutableCollection<BlockPos>>.removePos(element: BlockPos): Boolean {
217219
var anyRemoved = false
218220
removeIf {
219221
val removed = it.remove(element)
@@ -223,7 +225,7 @@ object PacketMine : Module(
223225
return anyRemoved
224226
}
225227

226-
private fun LinkedList<MutableCollection<BlockPos>>.retainAllPositions(positions: Collection<BreakContext>): Boolean {
228+
private fun ArrayList<MutableCollection<BlockPos>>.retainAllPositions(positions: Collection<BreakContext>): Boolean {
227229
var modified = false
228230
forEach {
229231
modified = modified or it.retainAll { pos ->
@@ -235,7 +237,7 @@ object PacketMine : Module(
235237
return modified
236238
}
237239

238-
private fun LinkedList<MutableCollection<BlockPos>>.any(predicate: (BlockPos) -> Boolean): Boolean {
240+
private fun ArrayList<MutableCollection<BlockPos>>.any(predicate: (BlockPos) -> Boolean): Boolean {
239241
if (isEmpty()) return false
240242
forEach { if (it.any(predicate)) return true }
241243
return false

0 commit comments

Comments
 (0)