Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

perf(ecs): bind live change matchers before entity iteration

yhh70e3caf57fb4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9e41c9c4b..3e560d5da 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Dense change queries bind their live sequence index once.** Added and Changed filters reuse component-specific matchers during entity iteration, avoiding repeated world and component lookups while retaining precise system boundaries, stable-frame skipping and visibility of later component replacements.
+
- **System change queries see additions and writes after earlier runs in the same frame.** Added and Changed filters use completed-system mutation sequences while explicit tick-based query instances keep their existing windows. Systems consume their own writebacks without retriggering themselves; compiled writers and asynchronous reads share the same boundaries. Stable queries skip candidate enumeration, and live sequence stamps reuse tick records and disappear with removed components.
- **Removal queries retain events across system runs in the same frame.** Removed component events use reader-owned sequence cursors, so removals after a system runs or after its last read remain available on its next run. Fresh readers start from registration, paused and async readers keep their claims, and fast readers skip retained history rather than repeatedly scanning a paused reader's backlog; explicit tick-based samplers keep their existing windows.
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index 06b8ba4af..a32212c2d 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -10064,6 +10064,7 @@ tabIndex: number | undefined
@internal resetIterationDepth: () => void
@internal resetQueryCacheStats: () => void
@internal resetQueryPool: () => void
+@internal resolveChangeSequenceMatcher: (component: AnyComponentDef, added: boolean) => (entity: Entity, after: number) => boolean
@internal resolveGetter: (component: AnyComponentDef, mode?: "borrowed" | "retained") => ((entity: Entity) => unknown) | null
@internal resolveHas: (component: AnyComponentDef) => ((entity: Entity) => boolean) | null
@internal resolveSetter: (component: AnyComponentDef) => ((entity: Entity, data: unknown) => void) | null
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index e12de68cc..e6a8a1a6f 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -10277,6 +10277,7 @@ tabIndex: number | undefined
@internal resetIterationDepth: () => void
@internal resetQueryCacheStats: () => void
@internal resetQueryPool: () => void
+@internal resolveChangeSequenceMatcher: (component: AnyComponentDef, added: boolean) => (entity: Entity, after: number) => boolean
@internal resolveGetter: (component: AnyComponentDef, mode?: "borrowed" | "retained") => ((entity: Entity) => unknown) | null
@internal resolveHas: (component: AnyComponentDef) => ((entity: Entity) => boolean) | null
@internal resolveSetter: (component: AnyComponentDef) => ((entity: Entity, data: unknown) => void) | null
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index 47f1007ad..a0fa0a665 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -10059,6 +10059,7 @@ tabIndex: number | undefined
@internal resetIterationDepth: () => void
@internal resetQueryCacheStats: () => void
@internal resetQueryPool: () => void
+@internal resolveChangeSequenceMatcher: (component: AnyComponentDef, added: boolean) => (entity: Entity, after: number) => boolean
@internal resolveGetter: (component: AnyComponentDef, mode?: "borrowed" | "retained") => ((entity: Entity) => unknown) | null
@internal resolveHas: (component: AnyComponentDef) => ((entity: Entity) => boolean) | null
@internal resolveSetter: (component: AnyComponentDef) => ((entity: Entity, data: unknown) => void) | null
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index 1fc1b83e4..d912b6ee8 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -10358,6 +10358,7 @@ tabIndex: number | undefined
@internal resetIterationDepth: () => void
@internal resetQueryCacheStats: () => void
@internal resetQueryPool: () => void
+@internal resolveChangeSequenceMatcher: (component: AnyComponentDef, added: boolean) => (entity: Entity, after: number) => boolean
@internal resolveGetter: (component: AnyComponentDef, mode?: "borrowed" | "retained") => ((entity: Entity) => unknown) | null
@internal resolveHas: (component: AnyComponentDef) => ((entity: Entity) => boolean) | null
@internal resolveSetter: (component: AnyComponentDef) => ((entity: Entity, data: unknown) => void) | null
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index 17c2334af..1c1534b77 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -10090,6 +10090,7 @@ tabIndex: number | undefined
@internal resetIterationDepth: () => void
@internal resetQueryCacheStats: () => void
@internal resetQueryPool: () => void
+@internal resolveChangeSequenceMatcher: (component: AnyComponentDef, added: boolean) => (entity: Entity, after: number) => boolean
@internal resolveGetter: (component: AnyComponentDef, mode?: "borrowed" | "retained") => ((entity: Entity) => unknown) | null
@internal resolveHas: (component: AnyComponentDef) => ((entity: Entity) => boolean) | null
@internal resolveSetter: (component: AnyComponentDef) => ((entity: Entity, data: unknown) => void) | null
diff --git a/sdk/src/ecs/ChangeTracker.ts b/sdk/src/ecs/ChangeTracker.ts
index dbda64fcb..02a939ff0 100644
--- a/sdk/src/ecs/ChangeTracker.ts
+++ b/sdk/src/ecs/ChangeTracker.ts
@@ -217,6 +217,12 @@ export class ChangeTracker {
getChangeSequence(): number { return this.changeSequence_; }
+ resolveChangeSequenceMatcher(component: AnyComponentDef, added: boolean): (entity: Entity, after: number) => boolean {
+ this.enableChangeSequenceTracking(component, added);
+ const rows = (added ? this.componentAddedSequences_ : this.componentChangedSequences_).get(component._id)!.rows;
+ return (entity, after) => ((rows.get(entity) as ChangeStamp | undefined)?.sequence ?? -1) > after;
+ }
+
isAddedAfterSequence(entity: Entity, component: AnyComponentDef, after: number): boolean {
return ((this.componentAddedSequences_.get(component._id)?.rows.get(entity) as ChangeStamp | undefined)?.sequence ?? -1) > after;
}
diff --git a/sdk/src/ecs/query.ts b/sdk/src/ecs/query.ts
index 40aed6043..ebeae03ae 100644
--- a/sdk/src/ecs/query.ts
+++ b/sdk/src/ecs/query.ts
@@ -407,6 +407,7 @@ export class QueryInstance<C extends readonly QueryArg[]> implements Iterable<Qu
private readonly depIds_: symbol[];
private lastRunTick_: number;
private lastChangeSequence_: number | null = null;
+ private sequenceMatchers_: Array<(entity: Entity, after: number) => boolean> | null = null;
private calls_ = 0;
private scanned_ = 0;
private filtered_ = 0;
@@ -472,8 +473,13 @@ export class QueryInstance<C extends readonly QueryArg[]> implements Iterable<Qu
resetChangeSequence(sequence: number): void {
if (!this.hasChangeFilters_()) return;
if (this.lastChangeSequence_ === null) {
- for (const f of this.descriptor_._addedFilters) this.world_.enableChangeSequenceTracking(f.component, true);
- for (const f of this.descriptor_._changedFilters) this.world_.enableChangeSequenceTracking(f.component, false);
+ this.sequenceMatchers_ = [];
+ for (const f of this.descriptor_._addedFilters) {
+ this.sequenceMatchers_.push(this.world_.resolveChangeSequenceMatcher(f.component, true));
+ }
+ for (const f of this.descriptor_._changedFilters) {
+ this.sequenceMatchers_.push(this.world_.resolveChangeSequenceMatcher(f.component, false));
+ }
}
this.lastChangeSequence_ = sequence;
}
@@ -535,14 +541,18 @@ export class QueryInstance<C extends readonly QueryArg[]> implements Iterable<Qu
if (_addedFilters.length === 0 && _changedFilters.length === 0) return true;
const tick = this.lastRunTick_;
const sequence = this.lastChangeSequence_;
+ if (sequence !== null) {
+ for (const matches of this.sequenceMatchers_!) {
+ if (!matches(entity, sequence)) return this.rejected_();
+ }
+ return true;
+ }
for (const f of _addedFilters) {
- const added = sequence === null ? this.world_.isAddedSince(entity, f.component, tick)
- : this.world_.isAddedAfterSequence(entity, f.component, sequence);
+ const added = this.world_.isAddedSince(entity, f.component, tick);
if (!added) return this.rejected_();
}
for (const f of _changedFilters) {
- const changed = sequence === null ? this.world_.isChangedSince(entity, f.component, tick)
- : this.world_.isChangedAfterSequence(entity, f.component, sequence);
+ const changed = this.world_.isChangedSince(entity, f.component, tick);
if (!changed) return this.rejected_();
}
return true;
diff --git a/sdk/src/ecs/world.ts b/sdk/src/ecs/world.ts
index d2ece2089..159cf6648 100644
--- a/sdk/src/ecs/world.ts
+++ b/sdk/src/ecs/world.ts
@@ -1370,6 +1370,11 @@ export class World {
/** @internal Latest opted-in component mutation sequence. */
getChangeSequence(): number { return this.changes_.getChangeSequence(); }
+ /** @internal Bind a live sequence index once for a system query's entity loop. */
+ resolveChangeSequenceMatcher(component: AnyComponentDef, added: boolean): (entity: Entity, after: number) => boolean {
+ return this.changes_.resolveChangeSequenceMatcher(component, added);
+ }
+
/** @internal Live additions after a completed system boundary. */
isAddedAfterSequence(entity: Entity, component: AnyComponentDef, after: number): boolean {
return this.changes_.isAddedAfterSequence(entity, component, after);
diff --git a/sdk/tests/change-query-boundaries.test.ts b/sdk/tests/change-query-boundaries.test.ts
index b6973dbb8..f80e57a73 100644
--- a/sdk/tests/change-query-boundaries.test.ts
+++ b/sdk/tests/change-query-boundaries.test.ts
@@ -195,4 +195,23 @@ describe('system change query boundaries', () => {
runner.run(reader);
expect(counts).toEqual([0, 1, 0, 1]);
});
+ it.each([Added, Changed])('keeps a bound %s index live after its last component is removed', filter => {
+ const batches: Entity[][] = [];
+ const reader = defineSystem([Query(filter(Marker))], query => {
+ const batch: Entity[] = [];
+ query.forEach(entity => batch.push(entity));
+ batches.push(batch);
+ });
+ runner.run(reader);
+ const entity = world.spawn();
+ world.insert(entity, Marker);
+ runner.run(reader);
+ world.remove(entity, Marker);
+ runner.run(reader);
+ expect(world.getStorageSizes().changes.sequenceRows).toBe(0);
+ world.insert(entity, Marker);
+ runner.run(reader);
+ runner.run(reader);
+ expect(batches).toEqual([[], [entity], [], [entity], []]);
+ });
});
diff --git a/sdk/tests/system.test.ts b/sdk/tests/system.test.ts
index 2dd2280c6..b800b4bde 100644
--- a/sdk/tests/system.test.ts
+++ b/sdk/tests/system.test.ts
@@ -60,6 +60,7 @@ function createMockWorld() {
getWorldTick: vi.fn(() => worldTick.value),
getChangeSequence: vi.fn(() => 0),
enableChangeSequenceTracking: vi.fn(),
+ resolveChangeSequenceMatcher: vi.fn(() => () => false),
isAddedAfterSequence: vi.fn(() => false),
isChangedAfterSequence: vi.fn(() => false),
anyAddedAfterSequence: vi.fn(() => false),
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index febb88499..2241de7bd 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';
* the reader recognises anyway.
*/
export const NOTED = {
+ 'perf(ecs): bind live change matchers before entity iteration':
+ { note: 'Dense change queries bind their live sequence index once.' },
'fix(ecs): track system change queries across same-frame writes':
{ note: 'System change queries see additions and writes after earlier runs in the same frame.' },
'fix(ecs): retain same-frame removals with sequence cursors':