Repository navigation
perf(watch): move channel metadata to lock-free atomics - #451
Merged
Merged
Conversation
`watch` previously protected its version counter, endpoint counts, disconnection state, value, and waiter set behind one exclusive mutex, causing read-only state queries (`has_changed`, `is_disconnected`), endpoint clone/drop operations, ready `changed` polls, and post-wake cancellation drops to contend on the channel lock. Track `version`, `closed`, `senders`, and `receivers` with atomics (falling back to a mutex-backed `u64` only on targets without 64-bit atomics) and separate waiter registration into its own mutex: - `Receiver::has_changed`, `Receiver::is_disconnected`, `Sender::clone`, non-final `Sender::drop`, `Receiver::clone`, `Receiver::drop`, ready `Change::poll` checks, and post-notification `Change::drop` paths run without locking. - `publish` and terminal `Sender::drop` detach waiter registrations before publishing the new version or `closed` flag with release ordering, preserving `WakerToken` validity and ensuring final unseen updates are observed before `RecvError::Disconnected`. - `Sender::subscribe` increments the receiver count while holding the slot lock so concurrent zero-receiver `send` and `subscribe` calls linearize.
`Receiver::get` and `Receiver::recv` previously cloned the retained value under an exclusive mutex, serializing concurrent readers even when no publication was in progress. Store the current `(value, version)` pair in `internal::rwlock::RwLock` and expose `RwLock::read` so `Receiver::get`, `Receiver::recv`, and `Sender::subscribe` take shared read access while `Sender::send` and `Sender::send_replace` take exclusive write access. Concurrent readers now clone `T` in parallel while still pairing the cloned value with its exact published version. Because concurrent readers inspect `&T` simultaneously across threads, sending or sharing `watch::Sender<T>` and `watch::Receiver<T>` across threads now requires `T: Send + Sync` rather than `T: Send` alone; single-threaded channels over `!Sync` values remain supported.
Add `primitives::watch` workloads covering `has_changed` queries, receiver
clone/drop cycles, concurrent `get` snapshots across scalar, 1 KiB, and
16 KiB `Clone` payloads, overwrite sends, ready `changed` cycles, pending
fan-out notifications, and pending wait cancellations.
Update `CHANGELOG.md` for the `T: Send + Sync` thread-safety requirement on
`watch::{Sender, Receiver}` and the measured `watch` performance profile.
8 tasks done
… slot Keep `Slot<T>` protected by `internal::mutex::Mutex` rather than `RwLock` for the `0.8.x` series so `watch::Sender<T>` and `watch::Receiver<T>` continue to implement `Send` and `Sync` whenever `T: Send`, avoiding a semver-breaking `T: Sync` requirement and avoiding multi-core reader-count CAS contention on small payloads while retaining lock-free channel metadata. Assert `Send + Sync` statically for `watch::Sender<Cell<u8>>` and `watch::Receiver<Cell<u8>>` in `traits_test`, and update the benchmark suite and `CHANGELOG.md` accordingly.
tisonkun
enabled auto-merge (squash)
November 22, 2026 12:25
This was referenced Nov 22, 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
watchversion, terminal disconnection state, and sender/receiver counts to atomics (with a mutex-backedu64fallback on targets without 64-bit atomics) and separate waiter registration into its own mutex soReceiver::has_changed,Receiver::is_disconnected,Sender::clone, non-finalSender::drop,Receiver::clone,Receiver::drop, readyChangepolls, and post-notificationChangedrops execute without locking.(value, version)slot behindinternal::mutex::Mutexsowatch::Sender<T>andwatch::Receiver<T>preserveT: Sendcross-thread compatibility for0.8.x(asserted statically intraits_test).primitives::watchbenchmarks, includewatch_testincargo x miri, and updateCHANGELOG.md.Part of #445.
Design Notes
publishand terminalSender::dropdetach waiter registrations under the waiter mutex before storing the new version orclosedflag withReleaseordering, so any lock-freeChange::pollorChange::dropobserving readiness knows itsWakerTokenhas already been detached. Whenchange_sinceobservesclosed == true, it re-loadsversionso a final update published right before the last sender dropped is always observed beforeRecvError::Disconnected.Sender::subscribeincrementsreceiverswhile holding the value slot mutex so a concurrentSender::sendeither fails before the subscription's initialseenversion or publishes a newer version that the new subscription still observes as unseen.Benchmarks
Measured on Apple M4 Max (
aarch64-apple-darwin, optimized Cargobenchprofile, 5 interleaved runs) comparingmain(13b13e4) againstHEAD:13b13e4)HEAD)ecosystemwatch/snapshotecosystemwatch/publish-then-receiveecosystemwatch/notify-4-waitersprimitives::watch::has_changed/threads=1primitives::watch::has_changed/threads=2primitives::watch::has_changed/threads=4primitives::watch::has_changed/threads=8primitives::watch::clone_drop_receiver/t=1primitives::watch::clone_drop_receiver/t=8primitives::watch::get_word/threads=1primitives::watch::get_word/threads=8primitives::watch::publish_overwriteprimitives::watch::changed_readyprimitives::watch::notify_pending_fanout/1primitives::watch::notify_pending_fanout/4primitives::watch::notify_pending_fanout/16primitives::watch::cancel_pending