Skip to content

perf: prune irrelevant actions, rehydrate plans and history lazily - #91

Merged
bsbodden merged 3 commits into
mainfrom
perf/relevance-pruning-and-lazy-plan
Nov 17, 2026
Merged

bsbodden merged 3 commits into
mainfrom
perf/relevance-pruning-and-lazy-plan

Conversation

@bsbodden

Copy link
Copy Markdown
Contributor

A performance pass over the hot paths, measured before and after on the same machine (macOS-26.6.2-x86_64-i386-64bit-Mach-O 3.13.14, Intel Core i7-9750H) with the repository's pytest-benchmark suite plus two new cases. Three commits:

  1. A and MCTS prune actions that cannot move the goal* (relevant_actions: a backward closure over the keys the goal reads and the precondition keys of actions that write relevant keys). Reachable but useless actions used to multiply the state space by every combination of the facts they set. Pruning is skipped when a relevant action has callable effects, which may read any key; MCTS keeps every action when none writes a goal key so its anytime fallback still returns a step. A* also skips copying the state for an action whose effects already hold.
  2. Plans and histories rehydrate lazily (LazyStates, LazyHistory): every graph node rehydrates the state, and rebuilding one PlanningState per plan step and one ActionResult per history entry on every node made a run quadratic in its length. Both sequences compare, index, slice and concatenate like the tuple and list they replace; Plan.expected_states and GoapState["execution_history"] are now typed as sequences (changelog, Changed).
  3. Cheaper PlanningState.apply and remembered planning keys.

Method: baseline run of tests/benchmarks/ on main (f91882e) with --benchmark-json, then the same suite on this branch; the two new cases were run on a detached worktree of f91882e. The two heavier distractor cases never finished a single plan() call within 300 s on the old code (one attempt each, hard alarm), so they have no before number: after, they take 1.2 ms and 6 ms. Means from pytest-benchmark; this laptop's run-to-run noise on the same code was up to about 30 percent on the loop benchmarks, so the table is for the large changes, not the single digits. Nothing above 50 microseconds got slower.

benchmark before ms after ms change
test_bench_astar_distractors[6-3-2] 62.083 0.317 -99.5%
test_astar_100 79.156 10.721 -86.5%
test_bench_loop_long[120] 1120.557 197.729 -82.4%
test_bench_astar_linear_depth[50] 14.032 2.982 -78.7%
test_bench_astar_branching[3-3] 0.468 0.102 -78.2%
test_astar_50 12.494 3.014 -75.9%
test_pipeline_100 86.135 24.864 -71.1%
test_bench_loop_long[60] 306.476 99.110 -67.7%
test_bench_astar_linear_depth[20] 2.026 0.699 -65.5%
test_bench_astar_branching[2-4] 0.222 0.078 -64.8%
test_bench_astar_branching[4-2] 0.334 0.125 -62.6%
test_bench_astar_branching[3-2] 0.209 0.098 -53.0%
test_bench_apply_single_effect[500] 0.071 0.034 -52.3%
test_pipeline_50 20.491 10.694 -47.8%
test_bench_astar_linear_depth[10] 0.538 0.293 -45.5%
test_bench_astar_linear_depth[2] 0.121 0.069 -42.3%
test_bench_astar_noise_pool[500] 1.171 0.679 -42.0%
test_bench_astar_noise_pool[100] 0.374 0.218 -41.7%
test_bench_astar_noise_pool[50] 0.244 0.156 -35.9%
test_goap_5_step 20.488 13.168 -35.7%
test_astar_10 0.459 0.308 -33.0%
test_bench_astar_noise_pool[10] 0.141 0.110 -22.0%
test_bench_astar_linear_depth[5] 0.216 0.171 -20.9%
test_bench_loop_20_steps 41.400 34.570 -16.5%
test_bench_astar_blacklist_fallback 0.217 0.183 -15.9%
test_bench_optimize_plans_with_constraints[3-2] 4.942 4.266 -13.7%
test_bench_from_dict[500] 0.063 0.055 -13.6%
test_bench_loop_10_steps 24.914 21.636 -13.2%
test_bench_optimize_plans_with_constraints[3-10] 5.156 4.533 -12.1%
test_bench_loop_multigoal_any 9.255 8.150 -11.9%
test_pipeline_10 5.900 5.257 -10.9%
test_bench_loop_multigoal_sequential[4] 23.295 20.762 -10.9%
test_bench_optimize_plans_no_constraints[5] 5.503 4.947 -10.1%
test_bench_loop_multigoal_sequential[2] 14.274 12.877 -9.8%
test_bench_loop_3_steps 11.467 10.474 -8.7%
test_bench_optimize_plans_with_constraints[5-5] 5.625 5.145 -8.5%
test_bench_optimize_plans_no_constraints[3] 4.754 4.603 -3.2%
test_bench_optimize_plans_no_constraints[1] 4.424 4.317 -2.4%

Full suite: 2171 passed, 2 skipped; make lint clean. Changelog updated under Unreleased.

…plies

A* and MCTS drop, before searching, every action whose effects touch no
goal-relevant key: the goal's keys and, by backward closure, the
precondition keys of actions that write relevant keys. Reachable but
useless actions used to multiply the state space by every combination of
the facts they set; a chain of 6 layers with 2 such actions per layer
explored 377 nodes in 93 ms and now 7 nodes in 0.4 ms, and shapes that
did not finish within 10 minutes plan in milliseconds. Pruning is skipped
when a relevant action has callable effects, which may read any key, and
MCTS keeps every action when none writes a goal key so its anytime
fallback still makes progress. A* also no longer copies the state for an
action whose effects already hold. Adds the distractor benchmark.
Every graph node rehydrates the state from the channel; rebuilding one
PlanningState per plan step and one ActionResult per history entry on
every node made a run quadratic in its length. Plan.from_dict and
history_from_state now return LazyStates and LazyHistory, which build
each entry the first time it is read and compare, index, slice and
concatenate like the tuple and list they replace. In the benchmark suite
a 60-step run went from 306 ms to 99 ms and a 120-step run from 1121 ms
to 198 ms. Adds the long-loop benchmark.
PlanningState.apply builds the new frozenset from the cached view without
re-probing every value for hashability (values are validated when actions
and states are constructed; an unhashable one still falls back to the
dropping constructor and its warning). The union of precondition and
effect keys is remembered per action list instead of being recomputed by
every node on every step. Changelog for the performance pass.
@github-actions github-actions Bot added the maintenance CI/build/tooling maintenance label Nov 17, 2026
@github-actions

Copy link
Copy Markdown

✅ Code Quality Report

Metric Score Status
MFCQI Score 0.8117120802170346 ✅ Good
Test Coverage 94% ✅ Good
Security Issues 0 ✅ Clean

Quality Gates

  • MFCQI Score: ✅ Minimum 0.75 (Current: 0.8117120802170346)
  • Test Coverage: ✅ Minimum 80% (Current: 94%)
  • Security: ✅ Zero vulnerabilities (Current: 0)

🎉 All quality gates passed! This PR maintains high code quality standards.

📊 View detailed MFCQI breakdown

Run uv run python -m mfcqi analyze langgoap locally to see detailed metrics breakdown.

@bsbodden
bsbodden merged commit a08938a into main Nov 17, 2026
20 checks passed
@bsbodden
bsbodden deleted the perf/relevance-pruning-and-lazy-plan branch November 17, 2026 11:51
@bsbodden bsbodden added the enhancement New feature or request label Nov 17, 2026
bsbodden added a commit that referenced this pull request Nov 17, 2026
The interrupt-inside-actions fix (#89) and the performance pass (#91):
goal-relevance pruning in A* and MCTS, lazy rehydration of plans and
histories, a cheaper state apply and remembered planning keys. The
jupyter image installs the 0.3 line.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request maintenance CI/build/tooling maintenance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant