Repository navigation
Description
Summary
#86 was closed, but legal-move generation is still the main cost of the engine search. It costs about 40 times as much as evaluation. As a result, the "hard" level never finishes a search deeper than its base depth of 3, and its maxDepth: 8 cannot be reached.
Where
js/rules.js:243-270(legalMovesFrom)js/rules.js:302-360(allLegalMoves,allLegalCaptures)js/ai.js:16(hard: { depth: 3, timeMs: 1000, maxDepth: 8 })js/ai.js:118,js/ai.js:201(search and quiescence call sites)
Details
For every piece, legalMovesFrom tests all 64 target squares with isPseudoLegalMove and isPathObstructed. For each candidate move it copies the whole board (applyMove) and runs isInCheck. The search calls this at every interior node, and quiescence calls allLegalCaptures, which also runs one applyMove per capture.
Measured in Node on a middlegame position:
| Operation | Cost |
|---|---|
allLegalMoves |
685 µs per call |
allLegalCaptures |
173 µs per call |
evaluatePosition |
17.5 µs per call |
| Depth-3 search | 1.1 s, 10.7k nodes |
| Depth-4 search | 16.9 s, 115k nodes |
Impact
hard is allowed about 1 s, or at most 3 s on the clock (searchBudget), so iterative deepening never completes depth 4. The worker then spends the rest of its allowance on a depth-4 search that is thrown away. Hard plays at its fixed depth, and every computer move and every hint keeps a core busy for the full allowance. On phones, depth 3 alone takes longer than the 1 s allowance. The same constant also makes review scoring (see the issue on review steps) and the no-worker fallback slow.
Steps to reproduce
- Time
searchPositionat depth 3 and depth 4 on a middlegame position, usingrules.jsandai.jsin Node or in a jest test. - Compare the cost of
allLegalMoveswith the cost ofevaluatePosition.
Suggested fix
- Generate pseudo-legal moves from piece offsets and sliding rays instead of scanning all 64 squares.
- Check legality only where it can fail: king moves, pinned pieces (one ray scan from the king), en passant, and positions in check.
- Or defer the legality test until the search actually plays a move, and count a node with no legal child as mate or stalemate.
- Make and unmake moves on one board (or a flat 64-entry array) instead of copying it for each move.
- Then add move ordering (MVV-LVA, killer moves).
Keep the public functions in rules.js as wrappers so callers and tests don't change. The existing generator cross-check tests ("quick generators agree") act as a perft-style safety net.
Acceptance criteria
-
allLegalMovesis at least 5 times faster on the benchmark position. - Hard completes depth 4 within its 1 s allowance on desktop.
- All rules tests and generator cross-checks pass unchanged.
Getting started
- Relevant files:
js/rules.js,js/ai.js,tests/unit/rules.test.js,tests/unit/ai-strength.test.js - How to run tests:
npm test
Regression of #86.
Filed by repo-audit/full-audit at 01c6a35. Severity: medium. Effort: M. audit-key: performance|js/rules.js|legalMovesFrom
Activity
perf(rules): generate moves from piece steps, not all 64 squares (#168)
perf(rules): play out only the moves that can expose the king (#168)
perf(ai): look up each square's distance from the centre (#168)
TMHSDigital commentedon Nov 21, 2026
Progress from an optimize pass. Three commits on
main, each measured on its own, and each with identical behavior: the same move lists in the same order, the same perft counts, and the same engine choices at depths 1 to 3 on eight test positions.pseudoMoveTargets)allLegalMoves567 → 217 µs,allLegalCaptures120 → 25 µs, hard depth 3 1541 → 627 msallLegalMoves~300 → ~67 µs (A/B in the same session)evaluatePositionevaluatePosition11–15 → 1.7–2.5 µs, hard depth 3 ~700–1150 → ~430–530 msBenchmark position:
r1bq1rk1/pp2bppp/2n1pn2/3p4/2PP4/2N1PN2/PP3PPP/R2QKB1R w KQ -.Acceptance criteria:
allLegalMovesat least 5 times faster: 567 → about 67 µs (about 8.5×).timed: true), hard now finishes depth 4 from the start position and from Kiwipete (600–1000 ms), but not from the benchmark middlegame, where it still stops at depth 3.pseudoMoveTargetswith the old square-by-square rule, and the pin-aware filter with playing out every candidate move.Tried, measured, and not kept:
applyMoveon a copy, for the legality test: no gain (217 → 229 µs, within noise).attackerAtclosure inisSquareAttackedwith a top-level helper: consistently slower in an interleaved A/B (about 250–300 → 330–460 µs).What is left: the cost is now spread across move generation,
isSquareAttackedand quiescence, with no single hot spot. Getting the middlegame to depth 4 probably needs better move ordering (killer moves, history), which changes which moves the engine picks, or a flat-board make/unmake design. Leaving this open for that.