Skip to content

Regression: legal-move generation still dominates search, so hard never gets past depth 3 #168

Open

Description

@TMHSDigital

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

  1. Time searchPosition at depth 3 and depth 4 on a middlegame position, using rules.js and ai.js in Node or in a jest test.
  2. Compare the cost of allLegalMoves with the cost of evaluatePosition.

Suggested fix

  1. Generate pseudo-legal moves from piece offsets and sliding rays instead of scanning all 64 squares.
  2. Check legality only where it can fail: king moves, pinned pieces (one ray scan from the king), en passant, and positions in check.
  3. Or defer the legality test until the search actually plays a move, and count a node with no legal child as mate or stalemate.
  4. Make and unmake moves on one board (or a flat 64-entry array) instead of copying it for each move.
  5. 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

  • allLegalMoves is 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

  1. added
    help wanted
    performance
    severity: medium
    on Nov 18, 2026
  2. TMHSDigital commented on Nov 21, 2026

    @TMHSDigital
    OwnerAuthor

    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.

    Commit Change Measured (Node 24, Ryzen 5 5600X)
    9dd4acf Generate moves from piece steps and lines, not all 64 squares (pseudoMoveTargets) allLegalMoves 567 → 217 µs, allLegalCaptures 120 → 25 µs, hard depth 3 1541 → 627 ms
    bbef7d0 Play a move out only when it can expose the king: king moves, en passant, pinned pieces, or any move while in check allLegalMoves ~300 → ~67 µs (A/B in the same session)
    635391d Look up each square's distance from the centre in evaluatePosition evaluatePosition 11–15 → 1.7–2.5 µs, hard depth 3 ~700–1150 → ~430–530 ms

    Benchmark position: r1bq1rk1/pp2bppp/2n1pn2/3p4/2PP4/2N1PN2/PP3PPP/R2QKB1R w KQ -.

    Acceptance criteria:

    • allLegalMoves at least 5 times faster: 567 → about 67 µs (about 8.5×).
    • Hard completes depth 4 within 1 s: partly met. A fresh depth-4 search went from 11.0 s to 2.9 s. In a timed search (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.
    • Rules tests and generator cross-checks pass. Two new cross-checks compare pseudoMoveTargets with the old square-by-square rule, and the pin-aware filter with playing out every candidate move.

    Tried, measured, and not kept:

    • Making and unmaking the move on the position's own board, instead of applyMove on a copy, for the legality test: no gain (217 → 229 µs, within noise).
    • Replacing the attackerAt closure in isSquareAttacked with 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, isSquareAttacked and 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedperformanceseverity: medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions