How to build fast hierarchies for game objects using data oriented design
4 hours ago
- #data-oriented-design
- #ECS
- #game-development
- ECS with integrated hierarchies is becoming common in libraries like Flecs, Unreal Mass, Bevy, Jecs, and BitECS.
- Hierarchies should be part of the core ECS model, as relationships are fundamental in data models like relational databases.
- Usability requires support for both asset hierarchies (for design) and scene hierarchies (for runtime).
- Performance optimization uses data-oriented design, including arrays of structures (AoS) and structure of arrays (SoA).
- Asset hierarchies in Flecs use prefabs with inheritance for variations and instancing, enabling runtime creation and overrides.
- Fragmenting relationships (like IsA and ChildOf) group entities by archetype, improving performance for queries and rendering.
- Component sharing reduces memory usage and improves cache efficiency by deduplicating data, with per-instance overrides.
- Scene hierarchies face challenges with fragmenting (too many archetypes) vs. non-fragmenting (O(n) operations) approaches.
- Flecs uses a hybrid hierarchy: fragmenting for small hierarchies and non-fragmenting for large ones, optimizing performance.
- Examples show significant performance gains: from 10 FPS to 60 FPS in traffic scenes, and from 23 FPS to 90 FPS in city scenes.