Skip to content

Add diffing engine for dependency autodiscovery in mesa_signals/core.py - #3482

Merged
quaquel merged 12 commits into
mesa:mainfrom
codebreaker32:diffing_deps
May 22, 2026
Merged

quaquel merged 12 commits into
mesa:mainfrom
codebreaker32:diffing_deps

Conversation

@codebreaker32

Copy link
Copy Markdown
Collaborator

Summary

This PR addresses:

simplify and enhance performance of @computed_property autodiscovery of dependencies. Currently, on every call, it's fully rebuilt after first disposing of all parents. But subscribing is not cheap. So, it might be better to diff the dependencies and update only the changed ones. This also moves the subscription responsibility back into the @computable_property, which is conceptually cleaner

from #3227

Motivation

Currently, when a @computed_property re-evaluates, it eagerly calls unobserve on every parent in its graph, executes the function, and then calls observe on every discovered dependency from scratch. For a stable graph where the code path doesn't branch, this results in a costly and redundant "destroy and rebuild" cycle on every recalculation.

We replaced the rebuild cycle with a "diffing" strategy using Python set math.

Key Changes

  1. Removed ComputedState._remove_parents()
  2. Implemented a diffing engine in @computed_property

@codebreaker32

Copy link
Copy Markdown
Collaborator Author

With this and #3462, We can comfortably close #3227

@github-actions

github-actions Bot commented Mar 8, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.1% [-1.3%, -0.7%] 🔵 -0.8% [-1.0%, -0.7%]
BoltzmannWealth large 🔵 -1.8% [-2.1%, -1.3%] 🔵 -1.8% [-2.6%, -1.0%]
Schelling small 🔵 -1.7% [-2.1%, -1.3%] 🔵 -1.6% [-1.8%, -1.4%]
Schelling large 🔵 -1.4% [-2.0%, -0.7%] 🔵 +0.5% [-0.8%, +2.2%]
WolfSheep small 🔵 -0.3% [-0.5%, -0.1%] 🔵 -0.6% [-0.8%, -0.5%]
WolfSheep large 🔵 -0.7% [-1.7%, +0.3%] 🔵 +1.3% [+0.1%, +2.7%]
SugarscapeG1mt small 🔵 -1.0% [-1.4%, -0.5%] 🔵 -0.7% [-0.9%, -0.5%]
SugarscapeG1mt large 🔵 +0.1% [-1.3%, +1.4%] 🔵 -0.7% [-1.1%, -0.2%]
BoidFlockers small 🔵 -1.4% [-1.7%, -1.0%] 🔵 -0.8% [-1.0%, -0.6%]
BoidFlockers large 🔵 -0.3% [-0.8%, +0.2%] 🔵 -1.6% [-1.8%, -1.5%]

@EwoutH
EwoutH requested a review from quaquel March 8, 2026 18:25
@EwoutH EwoutH added experimental Release notes label enhancement Release notes label labels Mar 8, 2026
Comment thread mesa/experimental/mesa_signals/core.py Resolved
@codebreaker32
codebreaker32 requested a review from quaquel March 16, 2026 05:44
@codebreaker32

Copy link
Copy Markdown
Collaborator Author

Hi @quaquel
Is there anything else you would like me to change?

@quaquel

quaquel commented Mar 18, 2026

Copy link
Copy Markdown
Member

Sorry, I am rather busy at the moment. I had hoped to handle a couple of PRs today, but other stuff cropped up.

@codebreaker32

Copy link
Copy Markdown
Collaborator Author

Sorry, I am rather busy at the moment. I had hoped to handle a couple of PRs today, but other stuff cropped up.

Its perfectly fine, I was just checking up!

@quaquel

quaquel commented May 21, 2026

Copy link
Copy Markdown
Member

@codebreaker32, I guess this still warrants a review?

I quickly scanned it and it looks fine to me, but if we still want to merge this, I'll give it a closer look as well.

@codebreaker32

Copy link
Copy Markdown
Collaborator Author

@codebreaker32, I guess this still warrants a review?

I quickly scanned it and it looks fine to me, but if we still want to merge this, I'll give it a closer look as well.

Yes and I have also given it a final look, LGTM so far!

@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.

LGTM

@quaquel
quaquel merged commit 30ae6c2 into mesa:main May 22, 2026
11 checks passed
@codebreaker32
codebreaker32 deleted the diffing_deps branch May 24, 2026 05:22
@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

enhancement Release notes label experimental Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants