Skip to content

Raw table iteration to improve query iteration speed by bypassing change ticks #21861

@chengts95

Description

@chengts95

What problem does this solve or what need does it fill?

Bevy’s query system does not automatically allow AVX2 optimization, and the changed tick writes can consume significant memory bandwidth. While this may not impact 60fps graphics programs significantly, it severely affects performance in hot loops and physical/game logic simulations that rely on iterative algorithms or time acceleration (e.g., KSP, HoI4, Minecraft, and RimWorld — even though RimWorld is 2D, it still faces significant performance challenges, unlike the 3D games).

In these games and applications, even with a 60fps graphical frame rate, the computational workload per frame can be much higher due to simulation steps, time acceleration, and compression. These applications, which rely on high-performance ECS and iterative calculations, are the ones most in need of optimization. Unfortunately, the current Bevy architecture prevents the full exploitation of ECS capabilities, making it difficult to optimize these demanding use cases effectively.

Notably, the ECS from C and C++, such as Flecs and EnTT have no such problem. Meanwhile, an ECS should not only serve for creating 60fps game so we should consider fix the issues to promote a generic Data-Oriented Programming paradigm.

Currently, bevy_ecs has the unsafe API from query state, but it should be trivial to make it a safe/unsafe API in standard Query. Also, the changed tick problem should be carefully addressed.

What solution would you like?

Add the Flecs style table iteration API to Query. The Changed Tick can also be moved to the table level. Meanwhile, the best way to detect changes is still using observer or messagebus. It may be also possible to enable specific components' changed detection from runtime plugin.

What alternative(s) have you considered?

Use Query state and manually use unsafe codes to iterate. Manage change ticks by ourselves or branch the bevy_ecs for different demands.

Additional context

A SIMD parallel performance test is shown here to demonstrate the necessarity.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-PerformanceA change motivated by improving speed, memory usage or compile timesD-ComplexQuite challenging from either a design or technical perspective. Ask for help!S-Needs-DesignThis issue requires design work to think about how it would best be accomplished

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions