Skip to content

Comments

Optimize query caching, system operations, and component indexing#7

Draft
Copilot wants to merge 4 commits intomasterfrom
copilot/improve-general-performance
Draft

Optimize query caching, system operations, and component indexing#7
Copilot wants to merge 4 commits intomasterfrom
copilot/improve-general-performance

Conversation

Copy link

Copilot AI commented Feb 18, 2026

Four targeted optimizations for hot paths in the ECS runtime:

Changes

  • Query key/mask caching: Memoize query keys (WeakMap) and masks (Map) to eliminate O(n log n) sorting on repeated queries. Queries now check cache first instead of regenerating.

  • System enable/disable: Replace O(n) indexOf with O(1) Map-based index lookup. Added _enabledSystemsIndex: Map<System, number> maintained during enable/disable operations.

  • Component gathering deduplication: Extract duplicated component collection logic (4 sites) into single _gatherComponents() helper function.

  • Component-to-entity index: Maintain Map<ComponentType, Set<Entity>> to skip irrelevant entities in single-component queries. pickOne() now iterates O(matching entities) instead of O(all entities).

Example

// Before: Query key regenerated with sort every call
function _getQueryKey(types: ComponentType[]): string {
    const ids = new Array<number>(length);
    for (let i = 0; i < length; i += 1) { ids[i] = types[i].Id; }
    ids.sort((a, b) => (a - b));
    return ids.join(",");
}

// After: Check cache first, regenerate only on miss
const cached = _queryKeyCache.get(types);
if (cached !== undefined) { return cached; }

All existing tests pass. Added performance benchmarks validating improvements (1000 queries < 100ms, 1000 system toggles < 50ms).


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits February 18, 2026 16:48
Co-authored-by: Byloth <14953974+Byloth@users.noreply.github.com>
Co-authored-by: Byloth <14953974+Byloth@users.noreply.github.com>
Co-authored-by: Byloth <14953974+Byloth@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve general performance for library Optimize query caching, system operations, and component indexing Feb 18, 2026
Copilot AI requested a review from Byloth February 18, 2026 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants