Use arrays for iterateBodyPartHierarchy (micro-optimization)

This commit is contained in:
Butterscotch! committed 2026-10-08 11:42:01 -04:00
1 parent 83172ba2b3
commit 0225e8a55b
1 file changed
+4 -4
@@ -115,7 +115,7 @@ fun highestBodyParts(bodyParts: Set<BodyPart>): Set<BodyPart> {
return result
}
private fun buildHierarchy(root: BodyPart, onlyChildren: Boolean): List<Pair<BodyPart?, BodyPart>> {
private fun buildHierarchy(root: BodyPart, onlyChildren: Boolean): Array<Pair<BodyPart?, BodyPart>> {
val result = mutableListOf<Pair<BodyPart?, BodyPart>>()
fun visit(parentBone: BodyPart?, bone: BodyPart, skipSelf: Boolean) {
if (!skipSelf) result += parentBone to bone
@@ -123,16 +123,16 @@ private fun buildHierarchy(root: BodyPart, onlyChildren: Boolean): List<Pair<Bod
for (child in children) visit(bone, child, false)
}
visit(null, root, onlyChildren)
return result
return result.toTypedArray()
}
// The hierarchy is constant, so every traversal of it is too. Derived once for the same reason
// BODY_PART_PARENTS is: walking it lazily per call cost a continuation and a Pair per bone, and
// BoneYawFallbackProcessor asks for one per active bone per frame.
private val BODY_PART_HIERARCHIES: Array<List<Pair<BodyPart?, BodyPart>>> =
private val BODY_PART_HIERARCHIES: Array<Array<Pair<BodyPart?, BodyPart>>> =
Array(ALL_BODY_PARTS.size * 2) { index -> buildHierarchy(ALL_BODY_PARTS[index / 2], index % 2 == 1) }
fun iterateBodyPartHierarchy(
root: BodyPart = BodyPart.HEAD,
onlyChildren: Boolean = false,
): List<Pair<BodyPart?, BodyPart>> = BODY_PART_HIERARCHIES[root.ordinal * 2 + if (onlyChildren) 1 else 0]
): Array<Pair<BodyPart?, BodyPart>> = BODY_PART_HIERARCHIES[root.ordinal * 2 + if (onlyChildren) 1 else 0]