What is the performance cost of skip and take for paginating Bevy ECS queries?
23.5K reputation · 06 Jan 2025, 18:17 UTC
Pagination in Bevy ECS
Bevy ECS uses archetype-based storage where queries iterate through matching entities. Unlike relational databases, the Query API does not provide native LIMIT or OFFSET functionality. To implement pagination for large datasets, developers typically chain .skip(n).take(m) onto the iterator returned by iter() or iter_mut().
Iterator Behavior and Stability
Because Bevy does not guarantee a stable iteration order across archetype changes or engine runs, pagination often requires an external index or an explicit sorting step to ensure consistent pages. However, the primary concern is the efficiency of the skipping mechanism when dealing with thousands of entities.
It is unclear whether the standard iterator adapters in the current Bevy version short-circuit archetype traversal or if skip(n) always incurs a linear O(n) cost by visiting every entity until the offset is reached.
- Does
skip(n)on aQueryIteravoid processing entities in archetypes that are entirely skipped? - What is the recommended architectural pattern for paginating large entity sets without incurring linear iteration costs?