Skip to content

Optimize Subscribable dispatch with copy-on-write snapshots - #2

Draft
apodgorny wants to merge 4 commits into
masterfrom
perf/subscribable-cow-dispatch
Draft

apodgorny wants to merge 4 commits into
masterfrom
perf/subscribable-cow-dispatch

Conversation

@apodgorny

@apodgorny apodgorny commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Change

One copy-on-write dispatch snapshot is rebuilt under the existing lock during subscribe(), update(), or cancel(). _dispatch() iterates it directly, with no per-event lock or snapshot allocation. A pre-normalized frozenset also avoids redundant normalization. Cancellation remains checked immediately before invoking the callback.

Only lib/subscribable.py changes in the runtime. The added GitHub Actions workflow provides a reproducible same-runner benchmark.

Verified direct-dispatch performance

7 alternating rounds x 100,000 operations, same Python 3.11 runner, both baselines installed separately. Full benchmark run.

Subscribers master (µs) PR (µs) reduction speedup
0 0.723 0.166 −77.0% 4.4×
1 1.192 0.281 −76.5% 4.2×
10 4.260 1.320 −69.0% 3.2×

Both Python 3.11 and 3.12 standard test jobs passed: CI run.

Concurrency

The immutable tuple freezes the identifier view for each dispatch. update() publishes a replacement tuple under the existing lock. cancel() marks the registration inactive and removes it from future tuples. Checking _active immediately before callback invocation preserves the previously agreed nonblocking/best-effort in-flight semantics. It does not claim a strict no-new-start-after-return guarantee in the narrow race between checking the flag and calling the callback.

No O or Web source files changed.

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.

1 participant