Repository navigation
Refactor: extract dependency-tracked evaluation from computed_property into ComputedState.evaluate() - #3764
Merged
Conversation
|
Performance benchmarks:
|
53 of 65 tasks
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.
Summary
Extracts the dependency-tracking evaluation logic out of
computed_property'swrapperclosure into a newComputedState.evaluate()method. No behavioral change for existingcomputed_propertyusers, this is a pure refactor that givesComputedStatea public, reusable evaluation primitive instead of keeping that mechanism locked inside one decorator's closure.Motive
computed_property'swrapperinlines a fairly involved block: enter the dependency-tracking frame (CURRENT_COMPUTED,PROCESSING_SIGNALS), call the user function, diff old vs. new parent Observables, subscribe/unsubscribe accordingly, and clearis_dirty.ComputedStatealready owns all the relevant data (value,parents,is_dirty,func), but had no method that could actually run this evaluation, the logic only existed inline inside the decorator, so it was unreachable from anywhere else.That's a real limitation: dependency-tracked reactive evaluation is a generally useful primitive beyond simple computed properties, for example, any feature that needs to re-evaluate a function whenever the Observables it reads change (a derived rate, a derived rule, a cached lookup keyed on agent state) would currently have no way to reuse Mesa's tracking machinery short of re-implementing the same save/restore-globals + diff-and-resubscribe dance by hand. Making
evaluate()a method onComputedStateturns this from a decorator-only trick into something any future caller can construct and drive directly.Implementation
ComputedState.evaluate(), which performs exactly the block that used to be inlined inwrapper: enters the tracking frame, callsself.func(self.owner), diffsparentsbefore/after, resubscribes/unsubscribes, and clearsis_dirtyon success.computed_property'swrappernow callsstate.evaluate()when its own staleness check (changed) determines a recompute is needed, and keeps the existing unconditionalstate.is_dirty = Falseafterward — preserving the original structure exactly (this line is a no-op whenevaluate()ran, sinceevaluate()already cleared it; it's the only thing that clearsis_dirtywhenchangedwasFalse).wrapper's own staleness check (the "did any parent's value actually change" loop), that policy stays where it is; only the "how do we evaluate and re-track" mechanism moved.Usage Examples
Before, this evaluation mechanism only existed inline in the decorator and wasn't reusable:
After, the same dependency-tracked evaluation is available directly on
ComputedStatefor any caller that needs reactive, automatic-dependency-tracking evaluation outside the property-decorator pattern, for instance, something that derives a per-agent rate or value from other Observables and needs it kept in sync without manually wiring up subscriptions:Additional Notes
This PR touches only
core.pyand is self-contained; it doesn't change any existing public behavior.