Skip to content

perf: run replace_promise only when a recursion promise actually escaped - #1564

Merged
Jammy2211 merged 1 commit into
mainfrom
claude/ci-test-timing-epic-ke2lul
Sep 6, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
claude/ci-test-timing-epic-ke2lul

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Closes #1563. Phase 8c (library leg) of the ci-timing-fast-tests epic (PyAutoMind draft/feature/pyautoheart/ci_timing_fast_tests_epic.md), from the phase-8 user-workspace smoke diagnosis (autolens_workspace#536): print(model.info) cost 0.5–1.3 s on nearly every modeling script in every workspace, and 6 s of the 10 s of the shapelets modeling script.

Summary

  • autofit/mapper/prior_model/recursion.py: RecursionPromise gains a used flag (__slots__), set when DynamicRecursionCache hands the placeholder back to a caller; replace_promise — the recursive setattr walk over every object reachable from the result — now runs only when the promise escaped. For a non-recursive model it never does, so the walk was a provably no-op traversal paid once per decorated call over a graph that grows with the model.
  • Tests compare the two implementations against each other via a force_traversal fixture plus a traversal counter: a genuinely self-referential Collection (the case the cache exists for — traversal still runs, graph and model.info intact) and a flat model (traversal count 0, model.info identical).

Measured

autolens_workspace scripts/multi_galaxy/features/advanced/shapelets/modeling.py under the smoke profile, warm: 10.58 s → 6.73 s; entire stdout including print(model.info) byte-identical.

Test Plan

  • python3 -m pytest -q -n auto — 2421 passed, 25 skipped
  • lib-tests.yml on the PR (both Python legs)

🤖 Generated with Claude Code

https://claude.ai/code/session_0151gQm9fk3XGLi5f18Urdba


Generated by Claude Code

`DynamicRecursionCache` hands out a `RecursionPromise` for the duration of a
decorated call and then calls `replace_promise` unconditionally, which
recursively `setattr`s its way through every object reachable from the result
looking for that promise. The only way to obtain the promise is a cache hit in
the wrapper, so when the graph being walked is not cyclic no object can hold
one and the traversal is provably a no-op -- paid once per decorated call, over
a graph that grows with the model, by both decorated functions
(`path_instances_of_class`, `AbstractPriorModel.from_instance`).

`RecursionPromise` now records whether it ever escaped and the wrapper walks
only when it did. `__slots__` earns its place twice: it keeps the flag off the
instance `__dict__`, and it makes `replace_promise` take its `AttributeError`
branch immediately on a promise that *is* live.

Measured on `autolens_workspace
scripts/multi_galaxy/features/advanced/shapelets/modeling.py` under the smoke
profile, warm second run: 10.58 s -> 6.73 s, with the script's whole stdout --
`print(model.info)` included -- byte-identical. `print(model.info)` appears in
nearly every modeling script in every workspace.

The regression tests compare the two implementations against each other rather
than against a stored string: a fixture restores the unconditional behaviour,
and `model.info` and the object graph must come back identical on both a
genuinely self-referential model (the case the cache exists for, where the
traversal must still run) and a flat one (where a counting fixture pins that it
does not).

Refs #1563

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0151gQm9fk3XGLi5f18Urdba
@Jammy2211
Jammy2211 merged commit cab17a3 into main Sep 6, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the claude/ci-test-timing-epic-ke2lul branch September 6, 2026 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: replace_promise walks the whole model graph even when no promise escaped (model.info cost on every modeling script)

2 participants