Add batch and suppress context managers for mesa_signals#3251
Add batch and suppress context managers for mesa_signals#3251falloficarus22 wants to merge 10 commits intomesa:mainfrom
Conversation
|
Performance benchmarks:
|
|
Thanks for this PR. I am not sure about this PR. It makes a number of choices that might have to be discussed first. For example, the context managers now work at the instance level rather than being global. What are the pro's and con's of this design? Also, I want to see a more elegant design for how signals are aggregated in the batch context manager. Most importantly, this must be easily user extendable for custom signals that a user might define. |
I think the key design tradeoff is instance-local vs global batching/suppression. Instance-local context managersPros:
Cons:
Global context managersPros:
Cons:
My preference is instance-local as the safe default, and if needed we can add an explicit opt-in “global coordinator” later rather than making global behavior implicit by default. I can make current signal aggregation more extensible by replacing hardcoded |
|
Thanks for the overview of pro's and cons.
This one makes no sense to me. When done well signals should always stay within a single model unless the user does something very strange
Too me this is critical, so I would like to see this developed at least at the API level before moving forward with this PR. For the record, I have been playing around with this and have some directions I might explore myself. |
e78fe1d to
bdc943f
Compare
|
|
Thanks for the update.
|
96d7695 to
6dc9901
Compare
0e66e20 to
74591fb
Compare
|
Closed this in favour of #3261 |
Summary
This PR adds two new context managers to
mesa_signalsviaHasObservables:batch_signals(): buffers signals and flushes at context exit.suppress_signals(): suppresses signal emission while active.It is designed to reduce signal storms during bulk updates while keeping current behavior unchanged outside the contexts.
Motive
While making model behavior more reactive, frequent updates can emit many intermediate signals that are not useful to observers (especially during bulk state changes or data collection phases).
This PR introduces explicit signal-flow control so users can:
batch_signals)suppress_signals)Implementation
Changes were made in
core.pyand tests intest_mesa_signals.pyKey implementation details:
HasObservables:_batch_depth_suppress_depth_signal_buffernotify()to route through_buffer_or_dispatch():_mesa_notify()._flush_signal_buffer():ObservableSignals.CHANGED, keep only the last signal per observable name in the batch.non-CHANGEDsignals in original order.HasObservables:batch_signals()supports nesting and flushes only on outermost exit.suppress_signals()supports nesting.Usage Examples
Additional Notes