Skip to content

perf: remove per-tick allocations from sim hot paths #162

@LightAxe

Description

@LightAxe

Finding

Several src/sim hot paths allocate Maps/Sets/arrays/objects/strings on every tick. The most visible offender is combat:

  • src/sim/combat.ts:40-68 allocates a Map, tile arrays, sorted Array.from(bucket.keys()), and a Set during the per-tick combat sweep.
  • src/sim/combat.ts:170-190 allocates another Map, per-colony arrays, and a sorted key array inside tile resolution.

Additional recurring tick allocations:

  • src/sim/tick.ts:1046-1053 allocates and sorts eligible every colony task-assignment pass.
  • src/sim/tick.ts:1058-1063 allocates a need object every colony pass.
  • src/sim/tick.ts:1194-1200 calls gridKey.split(':') during pheromone decay.

Impact

This violates the no-per-tick-allocation discipline in the project instructions and makes sim scaling harder to reason about. Combat is especially costly because the sweep runs on every sim tick, including steady-state/no-combat ticks.

Suggested direction

Replace these with reusable scratch structures or typed-array buckets owned by the sim loop. For pheromones, avoid parsing metadata from string keys on every tick; keep the pheromone type available structurally or cache parsed metadata with the grid.

Add an allocation-oriented regression/profiling check for a stable world over many ticks so this does not creep back in as Phase 3 complexity grows.

Review provenance

Confirmed by an independent fresh-context review agent during the 2026-05-28 adversarial codebase review of origin/main (16e4201).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions