Repository navigation
perf: run replace_promise only when a recursion promise actually escaped - #1564
Merged
Merged
Conversation
`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
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.
Closes #1563. Phase 8c (library leg) of the
ci-timing-fast-testsepic (PyAutoMinddraft/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:RecursionPromisegains ausedflag (__slots__), set whenDynamicRecursionCachehands the placeholder back to a caller;replace_promise— the recursivesetattrwalk 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.force_traversalfixture plus a traversal counter: a genuinely self-referentialCollection(the case the cache exists for — traversal still runs, graph andmodel.infointact) and a flat model (traversal count 0,model.infoidentical).Measured
autolens_workspace scripts/multi_galaxy/features/advanced/shapelets/modeling.pyunder the smoke profile, warm: 10.58 s → 6.73 s; entire stdout includingprint(model.info)byte-identical.Test Plan
python3 -m pytest -q -n auto— 2421 passed, 25 skippedlib-tests.ymlon the PR (both Python legs)🤖 Generated with Claude Code
https://claude.ai/code/session_0151gQm9fk3XGLi5f18Urdba
Generated by Claude Code