Skip to content

Add peek() to HasEmitters for non-reactive attribute access - #3774

Merged
quaquel merged 2 commits into
mesa:mainfrom
codebreaker32:featureadd_peek
Jul 17, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
codebreaker32:featureadd_peek

Conversation

@codebreaker32

@codebreaker32 codebreaker32 commented Jul 15, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds a peek method to the HasEmitters. This allows developers to retrieve the value of an Observable without registering it in the reactive dependency graph.

Motive

In the mesa_signals architecture, reading an Observable from within a ComputedState automatically logs it as a dependency. For components like we discussed in #3766 , we frequently need to read a state (e.g., model.time) without permanently binding to it or triggering a reactive signal cascade every time the clock ticks.

Implementation

  • Implemented .peek() directly on the HasEmitters mixin.
  • The method temporarily sets core.CURRENT_COMPUTED to None, extracts the value via getattr, and securely restores the previous tracker state in a finally block.

Usage Examples

# Standard access
current_time = instance.model.time

# Peek access 
current_time = instance.model.peek("time", 0.0) 

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -3.4% [-3.9%, -2.9%] 🔵 +2.5% [+2.3%, +2.6%]
BoltzmannWealth large 🔵 -2.4% [-2.8%, -2.0%] 🔵 -0.4% [-1.9%, +1.0%]
Schelling small 🔵 -2.4% [-3.0%, -1.9%] 🔵 -0.3% [-1.1%, +0.4%]
Schelling large 🔵 +0.4% [-0.3%, +1.2%] 🔵 -0.9% [-4.1%, +2.2%]
WolfSheep small 🔵 -3.6% [-4.2%, -2.9%] 🔵 +0.1% [-3.7%, +4.3%]
WolfSheep large 🔵 -1.2% [-2.1%, -0.4%] 🔵 +1.2% [+0.0%, +2.2%]
SugarscapeG1mt small 🔵 +0.4% [-0.1%, +0.9%] 🔵 +0.2% [-1.5%, +2.0%]
SugarscapeG1mt large 🔵 +1.1% [+0.1%, +2.1%] 🔵 -2.7% [-3.8%, -1.5%]
BoidFlockers small 🔵 +0.4% [+0.1%, +0.6%] 🔵 -0.3% [-0.7%, +0.0%]
BoidFlockers large 🔵 +0.3% [-0.1%, +0.7%] 🔵 -0.1% [-0.4%, +0.1%]

CURRENT_COMPUTED = None

try:
return getattr(self, name, default)

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.

Just curious, but why don't you simply access the private attribute so you don't have to mess with the current computed stuff?

Looking at BaseObservable, the private name is always the public name with a leading underscore. This is also how you were accessing time.

Separate issue: I assume you'll add tests? Once those are in, this can be merged.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

That was actually my first thought too But I realized that directly accessing _{name} is a bit brittle after running some smoke test.
First, it breaks on @computed_property, which stores its internal state under _computed_{name} rather than _{name}.
Even if you correctly guessed the name _computed_speed, grabbing the private variable directly bypasses the reactive engine entirely. When you access a @computed_property via its public name, the framework's descriptor checks if state.is_dirty or if its parents have changed. If dependencies have changed, it explicitly calls state.evaluate() to calculate the fresh value before returning it. If you access the private variable directly, you are just grabbing the internal ComputedState object, and its cached .value might be entirely stale. By using .peek("speed"), you route the request through the public descriptor, which natively executes the is_dirty evaluation logic, while still safely blinding the CURRENT_COMPUTED tracker

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done with tests!

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3fefc0bc-4b80-45fb-affb-035a8730044f

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@quaquel quaquel added experimental Release notes label enhancement Release notes label labels Jul 17, 2026
@quaquel
quaquel merged commit cf30a33 into mesa:main Jul 17, 2026
11 of 12 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

enhancement Release notes label experimental Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants