Skip to content

perf(watch): move channel metadata to lock-free atomics - #451

Merged
tisonkun merged 4 commits into
mainfrom
perf/watch-atomic-metadata-concurrent-reads
Nov 22, 2026
Merged

tisonkun merged 4 commits into
mainfrom
perf/watch-atomic-metadata-concurrent-reads

Conversation

@tisonkun

@tisonkun tisonkun commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Move watch version, terminal disconnection state, and sender/receiver counts to atomics (with a mutex-backed u64 fallback on targets without 64-bit atomics) and separate waiter registration into its own mutex so Receiver::has_changed, Receiver::is_disconnected, Sender::clone, non-final Sender::drop, Receiver::clone, Receiver::drop, ready Change polls, and post-notification Change drops execute without locking.
  • Keep the (value, version) slot behind internal::mutex::Mutex so watch::Sender<T> and watch::Receiver<T> preserve T: Send cross-thread compatibility for 0.8.x (asserted statically in traits_test).
  • Add primitives::watch benchmarks, include watch_test in cargo x miri, and update CHANGELOG.md.

Part of #445.

Design Notes

  • Waiter token safety and final-value ordering: publish and terminal Sender::drop detach waiter registrations under the waiter mutex before storing the new version or closed flag with Release ordering, so any lock-free Change::poll or Change::drop observing readiness knows its WakerToken has already been detached. When change_since observes closed == true, it re-loads version so a final update published right before the last sender dropped is always observed before RecvError::Disconnected.
  • Zero-receiver subscription linearizability: Sender::subscribe increments receivers while holding the value slot mutex so a concurrent Sender::send either fails before the subscription's initial seen version or publishes a newer version that the new subscription still observes as unseen.

Benchmarks

Measured on Apple M4 Max (aarch64-apple-darwin, optimized Cargo bench profile, 5 interleaved runs) comparing main (13b13e4) against HEAD:

Benchmark Rounds Baseline (13b13e4) Candidate (HEAD) Change
ecosystem watch/snapshot 75,000,000 325 ms (vs Tokio 482) 315 ms (vs Tokio 348) -3.1%
ecosystem watch/publish-then-receive 18,000,000 475 ms (vs Tokio 1057) 368 ms (vs Tokio 885) -22.5%
ecosystem watch/notify-4-waiters 1,700,000 249 ms (vs Tokio 643) 234 ms (vs Tokio 594) -6.0%
primitives::watch::has_changed/threads=1 48,000,000 211 ms 29 ms -86.3% (7.3x)
primitives::watch::has_changed/threads=2 48,000,000 356 ms 15 ms -95.8% (23.7x)
primitives::watch::has_changed/threads=4 48,000,000 1,337 ms 7 ms -99.5% (191x)
primitives::watch::has_changed/threads=8 48,000,000 720 ms 4 ms -99.4% (180x)
primitives::watch::clone_drop_receiver/t=1 24,000,000 228 ms 80 ms -64.9% (2.9x)
primitives::watch::clone_drop_receiver/t=8 24,000,000 895 ms 4,002 ms +347%
primitives::watch::get_word/threads=1 48,000,000 201 ms 208 ms +3.5%
primitives::watch::get_word/threads=8 48,000,000 621 ms 703 ms +13.2%
primitives::watch::publish_overwrite 24,000,000 314 ms 395 ms +25.8%
primitives::watch::changed_ready 24,000,000 450 ms 456 ms +1.3%
primitives::watch::notify_pending_fanout/1 4,000,000 214 ms 224 ms +4.7%
primitives::watch::notify_pending_fanout/4 1,000,000 149 ms 143 ms -4.0%
primitives::watch::notify_pending_fanout/16 250,000 120 ms 113 ms -5.8%
primitives::watch::cancel_pending 24,000,000 292 ms 330 ms +13.0%

`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.
… 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 tisonkun changed the title perf(watch): move metadata to atomics and allow concurrent snapshots perf(watch): move channel metadata to lock-free atomics Nov 22, 2026
@tisonkun
tisonkun enabled auto-merge (squash) November 22, 2026 12:25
@tisonkun
tisonkun merged commit d8157bc into main Nov 22, 2026
9 checks passed
@tisonkun
tisonkun deleted the perf/watch-atomic-metadata-concurrent-reads branch November 22, 2026 12:27
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