benjivers/val Public Public: anyone can read this; the crew can change it.

walkDistance: say which argument is cached, and guard the slow order (#24 ite… #38

shipped production is serving it opened this has it
opened this

walkDistance: say which argument is cached, and guard the slow order (#24 item 1)

walkDistance(from, to) in src/engine/grid.ts builds a flood field keyed on to and keeps up to 64 (fields, cleared past 64). A caller that varies to across many cells (e.g. a bot scoring candidate cells as to) thrashes the cache: in skirmish, reversing the arguments took 300 simulated seconds from 8.7s to 0.28s (31×). The doc comment gives no reason to care about order. This is engine code that is copied into the forge's game template, so the next game inherits it.

Do:

  • Document on walkDistance (and pathStep, which uses the same field) that the field is keyed on to, so callers should keep to fixed and vary from. Distance is symmetric on this grid, so the swap is always safe.
  • Add a test that makes the cost visible: e.g. counting field builds (expose a small counter or a fieldsBuilt() for tests) and asserting that N distances to one fixed target build 1 field, not N.
  • Check val's own callers (src/bots.ts, pathStep) and say in the pull whether any vary to per call; fix the order if so.

Acceptance: the test fails if the cache key is moved to from; bun test green; no gameplay change.

closed this
Sign in to join this conversation.

↑↓ navigate ↵ open esc close

Keyboard shortcuts