Skip to content

Refactor: extract dependency-tracked evaluation from computed_property into ComputedState.evaluate() - #3764

Merged
quaquel merged 2 commits into
mesa:mainfrom
codebreaker32:refactor/signal
Jun 29, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
codebreaker32:refactor/signal

Conversation

@codebreaker32

@codebreaker32 codebreaker32 commented Jun 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Extracts the dependency-tracking evaluation logic out of computed_property's wrapper closure into a new ComputedState.evaluate() method. No behavioral change for existing computed_property users, this is a pure refactor that gives ComputedState a public, reusable evaluation primitive instead of keeping that mechanism locked inside one decorator's closure.

Motive

computed_property's wrapper inlines 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 clear is_dirty. ComputedState already 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 on ComputedState turns this from a decorator-only trick into something any future caller can construct and drive directly.

Implementation

  • Added ComputedState.evaluate(), which performs exactly the block that used to be inlined in wrapper: enters the tracking frame, calls self.func(self.owner), diffs parents before/after, resubscribes/unsubscribes, and clears is_dirty on success.
  • computed_property's wrapper now calls state.evaluate() when its own staleness check (changed) determines a recompute is needed, and keeps the existing unconditional state.is_dirty = False afterward — preserving the original structure exactly (this line is a no-op when evaluate() ran, since evaluate() already cleared it; it's the only thing that clears is_dirty when changed was False).
  • No change to 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:

# old: only reachable through @computed_property
@computed_property
def area(self):
    return self.width * self.height

After, the same dependency-tracked evaluation is available directly on ComputedState for 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:

comp_state = ComputedState(owner=agent, name="rate", func=lambda a: a.acceleration)
...
if comp_state.is_dirty:
    comp_state.evaluate()   # tracks `acceleration` as a dependency automatically
value = comp_state.value

Additional Notes

This PR touches only core.py and is self-contained; it doesn't change any existing public behavior.

@codebreaker32
codebreaker32 requested a review from quaquel June 29, 2026 06:27
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.0% [-0.3%, +0.4%] 🟢 -4.5% [-4.8%, -4.3%]
BoltzmannWealth large 🔵 +0.2% [-0.3%, +0.9%] 🟢 -4.6% [-5.4%, -3.7%]
Schelling small 🔵 -0.2% [-0.5%, +0.2%] 🔵 +0.5% [-0.2%, +1.1%]
Schelling large 🔵 -2.3% [-3.0%, -1.8%] 🔵 -2.5% [-3.1%, -1.8%]
WolfSheep small 🔵 +0.3% [-0.1%, +0.7%] 🔵 -1.9% [-5.9%, +2.2%]
WolfSheep large 🔵 -0.5% [-1.1%, +0.0%] 🔵 -3.8% [-5.0%, -2.7%]
SugarscapeG1mt small 🔵 +0.7% [+0.2%, +1.2%] 🔵 -0.8% [-2.3%, +0.9%]
SugarscapeG1mt large 🔵 -0.3% [-1.1%, +0.6%] 🔵 -2.3% [-4.1%, -0.5%]
BoidFlockers small 🔵 -0.3% [-0.6%, +0.0%] 🔵 -0.2% [-0.6%, +0.2%]
BoidFlockers large 🔵 -0.1% [-0.5%, +0.3%] 🔵 +0.1% [-0.1%, +0.3%]

@quaquel quaquel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes good sense

@quaquel
quaquel merged commit ae6c7b7 into mesa:main Jun 29, 2026
8 of 9 checks passed
@EwoutH EwoutH mentioned this pull request Nov 15, 2026
53 of 65 tasks
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.

2 participants