Skip to content

perf: cache archetype transitions to avoid the per-operation linear scan#31

Merged
ragoune merged 1 commit into
masterfrom
perf/archetype-transition-graph
Jun 28, 2026
Merged

perf: cache archetype transitions to avoid the per-operation linear scan#31
ragoune merged 1 commit into
masterfrom
perf/archetype-transition-graph

Conversation

@ragoune

@ragoune ragoune commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Each archetype now caches its add/remove edges to neighbour archetypes. A single-component transition (Add/RemoveComponent, Add/RemoveTag) resolves the destination archetype via an O(1) edge lookup instead of rebuilding an id slice and scanning every archetype; the scan remains as a fallback to populate the edge and for multi-component operations.

This also fixes a latent corruption: removeComponent and RemoveTag held a *archetype taken before getArchetypeForComponentsIds, which can create a new archetype and reallocate world.archetypes (past the 1024 preallocated cap), leaving the pointer stale. The destination is now resolved first and both archetypes are re-fetched by index.

Each archetype now caches its add/remove edges to neighbour archetypes. A single-component transition (Add/RemoveComponent, Add/RemoveTag) resolves the destination archetype via an O(1) edge lookup instead of rebuilding an id slice and scanning every archetype; the scan remains as a fallback to populate the edge and for multi-component operations.
This also fixes a latent corruption: removeComponent and RemoveTag held a *archetype taken before getArchetypeForComponentsIds, which can create a new archetype and reallocate world.archetypes (past the 1024 preallocated cap), leaving the pointer stale. The destination is now resolved first and both archetypes are re-fetched by index.
@codecov

codecov Bot commented Jun 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
archetype.go 90.00% <100.00%> (-2.16%) ⬇️
component.go 65.82% <100.00%> (-0.24%) ⬇️
tag.go 86.66% <100.00%> (-1.57%) ⬇️
world.go 78.81% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ragoune ragoune merged commit 0206fac into master Jun 28, 2026
4 checks passed
@ragoune ragoune deleted the perf/archetype-transition-graph branch June 28, 2026 10:51
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.

1 participant