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

未關閉
#168 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
5/5
預估耗時
一週以上
新手友好度
38/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
javascript

研究方向

Start with js/rules.js at legalMovesFrom, allLegalMoves and allLegalCaptures, then read the search and quiescence call sites in js/ai.js. Run npm test and use the provided Node benchmark steps to compare move generation with evaluation. Done means all acceptance criteria pass, including the generator cross-checks and the hard depth 4 timing target.

由索引模型根據 Issue 內容生成。

描述

help wanted performance severity: medium

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

主要語言
JavaScript
星號
4
分支
2
平均合併
22 小時 16 分鐘
30 天內合併 PR
9

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

TMHSDigital/Static_Chess 的其他 Issue

查看 TMHSDigital/Static_Chess 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。