From 04799b298bdff8965ffbb12909b575d5f9511a5a Mon Sep 17 00:00:00 2001 From: Danielle Voznyy Date: Fri, 8 Dec 2023 23:32:46 -0500 Subject: [PATCH] Archetype: Store relations as EntityType instead of List --- .../benchmarks/instantiation/ArchetypeBenchmark.kt | 13 ------------- .../geary/datatypes/maps/Family2ObjectArrayMap.kt | 4 ++-- .../geary/engine/archetypes/Archetype.kt | 13 ++++++------- .../operations/ArchetypeReadOperations.kt | 6 +++--- 4 files changed, 11 insertions(+), 25 deletions(-) delete mode 100644 geary-benchmarks/src/main/kotlin/com/mineinabyss/geary/benchmarks/instantiation/ArchetypeBenchmark.kt diff --git a/geary-benchmarks/src/main/kotlin/com/mineinabyss/geary/benchmarks/instantiation/ArchetypeBenchmark.kt b/geary-benchmarks/src/main/kotlin/com/mineinabyss/geary/benchmarks/instantiation/ArchetypeBenchmark.kt deleted file mode 100644 index 3e04b1b73..000000000 --- a/geary-benchmarks/src/main/kotlin/com/mineinabyss/geary/benchmarks/instantiation/ArchetypeBenchmark.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.mineinabyss.geary.benchmarks.instantiation - -import com.mineinabyss.geary.datatypes.EntityType -import com.mineinabyss.geary.engine.archetypes.Archetype -import org.openjdk.jmh.annotations.Scope -import org.openjdk.jmh.annotations.State - -@State(Scope.Benchmark) -class ArchetypeBenchmark { - var arch: Archetype = Archetype(EntityType(listOf(1uL, 1uL shl 10, 1uL shl 32 or 1uL)), 0) - val type = EntityType(listOf(1uL, 1uL shl 10, 1uL shl 32 or 1uL)) - -} diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/maps/Family2ObjectArrayMap.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/maps/Family2ObjectArrayMap.kt index c49e665b9..4ba9219aa 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/maps/Family2ObjectArrayMap.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/maps/Family2ObjectArrayMap.kt @@ -1,10 +1,10 @@ package com.mineinabyss.geary.datatypes.maps -import com.mineinabyss.geary.modules.geary import com.mineinabyss.geary.datatypes.* import com.mineinabyss.geary.datatypes.family.Family import com.mineinabyss.geary.helpers.hasRelationKind import com.mineinabyss.geary.helpers.hasRelationTarget +import com.mineinabyss.geary.modules.geary /** * A map of [ComponentId]s to Arrays of objects with the ability to make fast queries based on component IDs. @@ -32,7 +32,7 @@ internal class Family2ObjectArrayMap { // See componentMap definition for relations if (id.isRelation()) { - val relation = id.toRelation()!! + val relation = Relation.of(id) set(Relation.of(relation.kind, geary.components.any).id) set(Relation.of(geary.components.any, relation.target).id) } diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/Archetype.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/Archetype.kt index d9790aa6f..fb3a09b0a 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/Archetype.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/Archetype.kt @@ -16,7 +16,7 @@ import com.mineinabyss.geary.systems.accessors.RelationWithData * An example use case: If a query matches an archetype, it will also match all entities inside which * gives a large performance boost to system iteration. */ -data class Archetype( +data class Archetype internal constructor( val type: EntityType, val id: Int ) { @@ -24,7 +24,6 @@ data class Archetype( private val archetypeProvider get() = archetypes.archetypeProvider private val eventRunner get() = archetypes.eventRunner - val entities: Sequence get() = ids.getEntities() /** The entity ids in this archetype. Indices are the same as [componentData]'s sub-lists. */ @@ -47,15 +46,15 @@ data class Archetype( /** Edges to other archetypes where a single component has been removed. */ internal val componentRemoveEdges = CompId2ArchetypeMap() - internal val relations = type.inner.mapNotNull { it.toRelation() } - internal val relationsWithData = relations.filter { it.id.holdsData() } + internal val relations: EntityType = type.filter { it.isRelation() } + internal val relationsWithData: EntityType = relations.filter { it.holdsData() } fun getRelationsByTarget(target: EntityId): List { - return relations.filter { it.target.toLong() == target.toLong() } + return relations.filter { Relation.of(it).target.toLong() == target.toLong() }.map { Relation.of(it) } } fun getRelationsByKind(kind: ComponentId): List { - return relations.filter { it.kind.toLong() == kind.toLong() } + return relations.filter { Relation.of(it).kind.toLong() == kind.toLong() }.map { Relation.of(it) } } /** The amount of entities stored in this archetype. */ @@ -342,7 +341,7 @@ data class Archetype( specificKind && specificTarget -> listOf(Relation.of(kind, target)) specificTarget -> getRelationsByTarget(target) specificKind -> getRelationsByKind(kind) - else -> relations + else -> relations.map { Relation.of(it) } }.run { //TODO this technically doesnt need to run when specificKind is set if (kind.hasRole(HOLDS_DATA)) filter { it.hasRole(HOLDS_DATA) } else this }.run { diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/operations/ArchetypeReadOperations.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/operations/ArchetypeReadOperations.kt index 8f5bb1a25..0dd5af32f 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/operations/ArchetypeReadOperations.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/operations/ArchetypeReadOperations.kt @@ -17,9 +17,9 @@ class ArchetypeReadOperations : EntityReadOperations { override fun getComponentsFor(entity: Entity): Array { val (archetype, row) = records[entity] return archetype.getComponents(row).also { array -> - for (relation in archetype.relationsWithData) { - val i = archetype.indexOf(relation.id) - array[i] = RelationWithData(array[i], null, relation) + archetype.relationsWithData.forEach { relation -> + val i = archetype.indexOf(relation) + array[i] = RelationWithData(array[i], null, Relation.of(relation)) } } }