NateTheGreatt/bitECS

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

ProgrammingLife opened this issue · 2 comments

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?

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.

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