Conversation
|
Performance benchmarks:
|
quaquel
reviewed
Jan 23, 2026
quaquel
reviewed
Jan 23, 2026
quaquel
reviewed
Jan 23, 2026
| def _has_subscribers(self, name: str, signal_type: str | SignalType) -> bool: | ||
| """Check if there are any subscribers for a given observable and signal type.""" | ||
| key = (name, signal_type) | ||
| if key not in self.subscribers: |
Member
There was a problem hiding this comment.
we are using a defaultdict, so this check is not needed. We allways get a list back
Collaborator
Author
There was a problem hiding this comment.
But If I removed the check and just write if len(self.subscribers[key]) > 0, the defaultdict will automatically insert an empty list [] for every single property(mutate the subscriber) we'll ever check. That will result in unnecessary memory bloat if we'll have large number of keys
Member
There was a problem hiding this comment.
It's a micro optimization. I doubt it matters in everyday use, where keys probably are. quite small. But fine with me to do it this way.
Member
|
I think this is basically ready to go. |
Added comment to clarify the purpose of the 'subscribers' dictionary.
quaquel
approved these changes
Jan 24, 2026
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
This PR optimizes the
mesa_signalmodule by flattening the internal subscriber storage and introducing a "fast path" that bypasses the signal emission pipeline forObservableproperties with no active subscribers.Motive
When an agent updates an observable (e.g.,
agent.wealth = 10), the following sequence currently occurs regardless of subscriber status:getattr(self, _wealth)is called to fetch theold_value. This is wasteful if no one is listening.notify()method creates a newMessageobject. This allocates memory for a signal payload that will immediately be discarded.self.notify(...)andself._mesa_notify(message)are called._mesa_notify, the system checks the dictionary.See #3131 (comment) also.
Implementation
The optimization relies on flattening the data structure to make the subscriber check as cheap as possible
We changed the internal
subscribersstructure from a nested dictionary (dict[str, dict[str, list]]) to a flat dictionary using tuple keys (dict[tuple[str, str], list]). This reduces pointer chasing and simplifies the lookup logic.Added a helper method
_has_subscribersthat utilizes the new flat structure to check for listeners in a single step:BaseObservable.__set__to check_has_subscribersimmediately. If it returnsFalse, the method exits early, skippinggetattr,Messageallocation, and notification calls.