[AnimalShogi] precompute the CAN_MOVE table once instead of per _can_move call#1324
Open
gweber wants to merge 1 commit into
Open
[AnimalShogi] precompute the CAN_MOVE table once instead of per _can_move call#1324gweber wants to merge 1 commit into
gweber wants to merge 1 commit into
Conversation
…move call _can_move rebuilt the full (5, 12, 12) CAN_MOVE table via a triple-nested vmap on every call, only to index a single entry. The table depends solely on movement geometry, so lift it to a module-level constant (as the Zobrist tables already are). _can_move runs ~1700x per _legal_action_mask (132 move checks + 132x12 in _is_checked), so the rebuild dominated. ~1.57x faster _legal_action_mask, ~1.2x faster compile. Output identical: legal_action_mask matches the previous implementation across 250 states; tests/test_animal_shogi.py passes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
_can_moverebuilt the full(5, 12, 12)CAN_MOVEtable — via a triple-nestedvmap— on every call, only to index a single entry:The table depends only on movement geometry, so this lifts it to a module-level constant (as the Zobrist tables already are).
_can_moveruns ~1700× per_legal_action_mask(132 move checks + 132×12 in_is_checked), so the rebuild dominated.Behaviour & perf
Pure refactor, no behavioural change —
legal_action_maskmatches the previous implementation across 250 states, andtests/test_animal_shogi.pypasses (5/5). Measured ~1.57× faster_legal_action_mask(1.42 → 0.91 ms, B=200, CPU) and ~1.2× faster compile. (XLA does not fully constant-fold the per-call rebuild, so this is a real runtime win, not only a tracing/clarity one.)