From b692d5de923b5b51ffb3bc9dca76984ccb230155 Mon Sep 17 00:00:00 2001 From: Christian Treffs Date: Tue, 20 Aug 2019 17:00:56 +0200 Subject: [PATCH] Optimize Nexus storage --- Sources/FirebladeECS/Nexus.swift | 36 ++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/Sources/FirebladeECS/Nexus.swift b/Sources/FirebladeECS/Nexus.swift index a7e5337..746f49e 100644 --- a/Sources/FirebladeECS/Nexus.swift +++ b/Sources/FirebladeECS/Nexus.swift @@ -5,25 +5,29 @@ // Created by Christian Treffs on 09.10.17. // -public class Nexus { - public weak var delegate: NexusEventDelegate? +public final class Nexus { + public final weak var delegate: NexusEventDelegate? - /// - Index: index value matching entity identifier shifted to Int - /// - Value: each element is a entity instance - @usableFromInline var entityStorage: UnorderedSparseSet + /// Main entity storage. + /// Entities are tightly packed by EntityIdentifier. + @usableFromInline final var entityStorage: UnorderedSparseSet - /// - Key: component type identifier - /// - Value: each element is a component instance of the same type (uniform). New component instances are appended. - @usableFromInline internal var componentsByType: [ComponentIdentifier: ManagedContiguousArray] + /// - Key: ComponentIdentifier aka component type. + /// - Value: Array of component instances of same type (uniform). + /// New component instances are appended. + @usableFromInline final var componentsByType: [ComponentIdentifier: ManagedContiguousArray] - /// - Key: entity id as index - /// - Value: each element is a component identifier associated with this entity - @usableFromInline internal var componentIdsByEntity: [EntityIdentifier: Set] + /// - Key: EntityIdentifier aka entity index + /// - Value: Set of unique component types (ComponentIdentifier). + /// Each element is a component identifier associated with this entity. + @usableFromInline final var componentIdsByEntity: [EntityIdentifier: Set] - /// - Values: entity ids that are currently not used - @usableFromInline internal var freeEntities: ContiguousArray + /// Entity ids that are currently not used. + @usableFromInline final var freeEntities: ContiguousArray - @usableFromInline internal var familyMembersByTraits: [FamilyTraitSet: UnorderedSparseSet] + /// - Key: FamilyTraitSet aka component types that make up one distinct family. + /// - Value: Tightly packed EntityIdentifiers that represent the association of an entity to the family. + @usableFromInline final var familyMembersByTraits: [FamilyTraitSet: UnorderedSparseSet] public init() { entityStorage = UnorderedSparseSet() @@ -60,7 +64,7 @@ public class Nexus { // MARK: - Equatable extension Nexus: Equatable { - public static func == (lhs: Nexus, rhs: Nexus) -> Bool { + @inlinable public static func == (lhs: Nexus, rhs: Nexus) -> Bool { return lhs.entityStorage == rhs.entityStorage && lhs.componentIdsByEntity == rhs.componentIdsByEntity && lhs.freeEntities == rhs.freeEntities && @@ -84,6 +88,6 @@ extension Nexus { // MARK: - CustomDebugStringConvertible extension Nexus: CustomDebugStringConvertible { public var debugDescription: String { - return "Nexus" + return "" } }