NateTheGreatt / bitECS

Flexible, minimal, data-oriented ECS library for Typescript
Mozilla Public License 2.0
942 stars 84 forks source link

Is it necessary to make a query on every frame tick or are there any optimization tricks to avoid it if nothing changed? #117

Closed ProgrammingLife closed 1 year ago

ProgrammingLife commented 1 year ago

Let's say we have some system defined like this:

    const query = defineQuery( [ Scale, Position ] );
    return defineSystem( world => {
        const entities = query( world );
        const entitiesCount = entities.length;
        for ( let i = 0; i < entitiesCount; i++ ) {
            const eid = entities[ i ];
            ...
        }
    }

Is it necessary to call query on every frame tick if there are no changes since the last frame, and many other previous frames? Are there some optimization tricks to cache the output of that query and just reuse it? Maybe this can be done internally inside the query function in BitECS as it can track a list of queried components?

NateTheGreatt commented 1 year ago

Is it necessary to call query on every frame tick if there are no changes since the last frame, and many other previous frames?

When you say changes, do you mean add/removeComponent changes? Or a direct data manipulation component.data[eid] = 3 type of change?

Assuming you meant add/removeComponent, then you don't have to worry. Queries are cached and incrementally updated when components are added/removed. Calling the query simply passes back an array, it doesn't do any filtering.

ProgrammingLife commented 1 year ago

Wow, thanks! That's what I exactly meant! (add/removeComponent). You're doing very useful work with BitECS. Thanks!