Skip to content

Commit 4d1f9b1

Browse files
committed
Force pass array pointer to entity utils
Force developers to write high performance code
1 parent f450676 commit 4d1f9b1

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

common/src/main/kotlin/com/lambda/util/combat/Explosion.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ object Explosion {
4848
* @return The velocity of the entities.
4949
*/
5050
fun SafeContext.velocity(explosion: Explosion) =
51-
getFastEntities<LivingEntity>(explosion.position, explosion.power * 2.0)
51+
getFastEntities<LivingEntity>(explosion.position, explosion.power * 2.0, ArrayList())
5252
.associateWith { entity -> velocity(entity, explosion) }
5353

5454
/**

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ object EntityUtils {
2323
noinline predicate: (T) -> Boolean = { true },
2424
): T? {
2525
// Speculative execution trolling
26-
val entities =
27-
if (range > 64) getEntities(predicate)
26+
val entities = ArrayList<T>()
27+
if (range > 64) getEntities(entities, predicate)
2828
// I have an idea for optimization.
2929
//
3030
// Since the search operates linearly, eventually it will reach the midpoint.
@@ -33,7 +33,7 @@ object EntityUtils {
3333
// Theoretically, the closest entity should be within a cubic space of delta^3 blocks.
3434
// If there are no entities within this delta box, examine the outer box. (Although this is unlikely given the fact that the closest entity is within the delta box.)
3535
// The performance improvement is relative to the initial state.
36-
else getFastEntities(pos, range, predicate)
36+
else getFastEntities(pos, range, entities, predicate)
3737

3838
return entities.minByOrNull { it.squaredDistanceTo(pos) }
3939
}
@@ -69,30 +69,29 @@ object EntityUtils {
6969
inline fun <reified T : Entity> SafeContext.getFastEntities(
7070
pos: Vec3d,
7171
distance: Double,
72+
entitiesPtr: MutableList<T>,
7273
noinline predicate: (T) -> Boolean = { true },
7374
): List<T> {
7475
val chunks = ceil(distance / 16).toInt()
7576
val sectionX = pos.x.toInt() shr 4
7677
val sectionY = pos.y.toInt() shr 4
7778
val sectionZ = pos.z.toInt() shr 4
7879

79-
val entities = ArrayList<T>()
80-
8180
// Here we iterate over all sections within the specified distance and add all entities of type [T] to the list.
8281
// We do not have to worry about performance here, as the number of sections is very limited.
8382
// For example, if the player is on the edge of a section and the distance is 16, we only have to iterate over 9 sections.
8483
for (x in sectionX - chunks..sectionX + chunks) {
8584
for (y in sectionY - chunks..sectionY + chunks) {
8685
for (z in sectionZ - chunks..sectionZ + chunks) {
8786
val section = world.entityManager.cache.findTrackingSection(ChunkSectionPos.asLong(x, y, z)) ?: continue
88-
section.collection.filterIsInstanceTo(entities) { entity ->
87+
section.collection.filterIsInstanceTo(entitiesPtr) { entity ->
8988
entity != player && entity.squaredDistanceTo(pos) <= distance * distance && predicate(entity)
9089
}
9190
}
9291
}
9392
}
9493

95-
return entities
94+
return entitiesPtr
9695
}
9796

9897
/**
@@ -104,13 +103,12 @@ object EntityUtils {
104103
* @param predicate Optional predicate to filter entities.
105104
* @return A list of entities of type [T] within the specified distance from the position without the player.
106105
*/
107-
inline fun <reified T : Entity> SafeContext.getEntities(noinline predicate: (T) -> Boolean = { true }): List<T> {
108-
val entities = ArrayList<T>()
109-
110-
world.entities.filterIsInstanceTo(entities) { entity ->
106+
inline fun <reified T : Entity> SafeContext.getEntities(
107+
entitiesPtr: MutableList<T>,
108+
noinline predicate: (T) -> Boolean = { true }
109+
) {
110+
world.entities.filterIsInstanceTo(entitiesPtr) { entity ->
111111
entity != player && predicate(entity)
112112
}
113-
114-
return entities
115113
}
116114
}

0 commit comments

Comments
 (0)