Skip to content

Commit 0ec68c6

Browse files
committed
micro-optimization
1 parent e4d2bcf commit 0ec68c6

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

common/src/main/kotlin/com/lambda/util/collections/Extensions.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@ inline fun <reified R, C : MutableCollection<in R>> Iterable<*>.filterPointer(
2020
predicate: (R) -> Boolean,
2121
) {
2222
var index = 0
23-
for (element in this) if (element is R && predicate(element)) {
24-
iterator(element, index++); destination?.add(element)
23+
for (element in this) {
24+
when {
25+
element is R && predicate(element) && destination != null -> {
26+
iterator(element, index++)
27+
destination.add(element)
28+
}
29+
30+
element is R && predicate(element) && destination == null -> {
31+
iterator(element, index++)
32+
}
33+
}
2534
}
2635
}

common/src/main/kotlin/com/lambda/util/world/WorldUtils.kt

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ import kotlin.math.ceil
4040
* @see <a href="https://www.ibm.com/docs/no/aix/7.2?topic=monitoring-garbage-collection-impacts-java-performance">IBM - Garbage Collection Impacts on Java Performance</a>
4141
* @see <a href="https://devdiaries.medium.com/gc-and-its-effect-on-java-performance-9cba51ffb196">Medium - GC and Its Effect on Java Performance</a>
4242
*
43+
* Many functions uses a branching approach to avoid trolling the speculative execution of the CPU.
44+
* @see <a href="https://en.wikipedia.org/wiki/Branch_predictor for more information.">Branch Predictor</a>
45+
* @see <a href="https://en.wikipedia.org/wiki/Speculative_execution">Speculative Execution</a>
4346
*/
4447
object WorldUtils {
4548
/**
@@ -163,13 +166,13 @@ object WorldUtils {
163166
}
164167

165168
/**
166-
* Returns all the position within the range where the predicate is true.
169+
* Returns all the blocks and positions within the range where the predicate is true.
167170
*
168171
* @param pos The position to search from.
169172
* @param rangeX The maximum distance to search for entities in the x-axis.
170173
* @param rangeY The maximum distance to search for entities in the y-axis.
171174
* @param rangeZ The maximum distance to search for entities in the z-axis.
172-
* @param pointer The mutable list to store the positions in.
175+
* @param pointer The mutable map to store the positions to blocks in.
173176
* @param predicate Predicate to filter the blocks.
174177
* @param iterator Iterator to perform operations on each block.
175178
*/
@@ -178,32 +181,38 @@ object WorldUtils {
178181
rangeX: Int,
179182
rangeY: Int,
180183
rangeZ: Int,
181-
pointer: MutableList<Block>? = null,
184+
pointer: MutableMap<BlockPos, Block>? = null,
182185
predicate: (BlockState, BlockPos) -> Boolean = { _, _ -> true },
183186
iterator: (BlockState, BlockPos, Int) -> Unit = { _, _, _ -> },
184187
) = searchBlocks(pos, Vec3i(rangeX, rangeY, rangeZ), pointer, predicate, iterator)
185188

186189
/**
187-
* Returns all the position within the range where the predicate is true.
190+
* Returns all the blocks and positions within the range where the predicate is true.
188191
*
189192
* @param pos The position to search from.
190193
* @param range The maximum distance to search for entities in each axis.
191-
* @param pointer The mutable list to store the positions in.
194+
* @param pointer The mutable map to store the positions to blocks in.
192195
* @param iterator Iterator to perform operations on each block.
193196
* @param predicate Predicate to filter the blocks.
194197
*/
195198
inline fun SafeContext.searchBlocks(
196199
pos: Vec3i,
197200
range: Vec3i,
198-
pointer: MutableList<Block>? = null,
201+
pointer: MutableMap<BlockPos, Block>? = null,
199202
predicate: (BlockState, BlockPos) -> Boolean = { _, _ -> true },
200203
iterator: (BlockState, BlockPos, Int) -> Unit = { _, _, _ -> },
201204
) {
202205
iteratePositions(pos, range) { blockPos, index ->
203206
val state = blockPos.blockState(world)
204-
if (predicate(state, blockPos)) {
205-
pointer?.add(state.block)
206-
iterator(state, blockPos, index)
207+
when {
208+
predicate(state, blockPos) && pointer != null -> {
209+
pointer[blockPos] = state.block
210+
iterator(state, blockPos, index)
211+
}
212+
213+
predicate(state, blockPos) && pointer == null -> {
214+
iterator(state, blockPos, index)
215+
}
207216
}
208217
}
209218
}
@@ -213,22 +222,28 @@ object WorldUtils {
213222
*
214223
* @param pos The position to search from.
215224
* @param range The maximum distance to search for fluids in each axis.
216-
* @param collector The mutable list to store the positions in.
225+
* @param pointer The mutable list to store the positions in.
217226
* @param iterator Iterator to perform operations on each fluid.
218227
* @param predicate Predicate to filter the fluids.
219228
*/
220229
inline fun <reified T : Fluid> SafeContext.searchFluids(
221230
pos: Vec3i,
222231
range: Vec3i,
223-
collector: MutableList<T>? = null,
232+
pointer: MutableMap<BlockPos, T>? = null,
224233
iterator: (FluidState, BlockPos, Int) -> Unit = { _, _, _ -> },
225234
predicate: (FluidState, BlockPos) -> Boolean = { _, _ -> true },
226235
) {
227236
iteratePositions(pos, range) { blockPos, index ->
228237
val state = world.getFluidState(blockPos)
229-
if (predicate(state, blockPos)) {
230-
collector?.add(state.fluid as? T ?: return@iteratePositions)
231-
iterator(state, blockPos, index)
238+
when {
239+
predicate(state, blockPos) && pointer != null -> {
240+
iterator(state, blockPos, index)
241+
pointer[blockPos] = state.fluid as T
242+
}
243+
244+
predicate(state, blockPos) && pointer == null -> {
245+
iterator(state, blockPos, index)
246+
}
232247
}
233248
}
234249
}

0 commit comments

Comments
 (0)