perf(gateway): share broadcast payload via Arc<str>; scope unregister #103

Merged
icub3d merged 1 commit from feature/85-gateway-arc-payload into main 2026-07-09 04:15:24 +00:00
Owner

Summary

Fixes the two gateway hot-path costs in #85.

Broadcast payload — one copy, not N

broadcast_to_channel / send_to_user / broadcast_all did payload.to_string() per recipient — a fresh heap copy of the same JSON for every subscriber, all under the registry read lock. The outbound channel now carries Arc<str>: the payload is built once and each recipient gets a pointer clone. Lock-held work drops from O(recipients) allocations to O(recipients) refcount bumps. Each connection task materializes an owned socket frame only when it actually writes, off the broadcast path.

unregister — touch only joined channels

It previously scanned every channel's subscription set on each disconnect (O(channels)). A new connection_channels index records what each connection joined, so cleanup touches only those.

Tests

  • New unregister_cleans_channel_subscriptions (subscribe to 2 channels → unregister empties both).
  • Existing gateway tests updated to the Arc<str> channel; the shed-slow-consumer and close-signal behavior is unchanged.
  • Full server suite (131) + clippy -D warnings green.

Closes #85

## Summary Fixes the two gateway hot-path costs in #85. ### Broadcast payload — one copy, not N `broadcast_to_channel` / `send_to_user` / `broadcast_all` did `payload.to_string()` **per recipient** — a fresh heap copy of the same JSON for every subscriber, all under the registry read lock. The outbound channel now carries `Arc<str>`: the payload is built once and each recipient gets a pointer clone. Lock-held work drops from O(recipients) allocations to O(recipients) refcount bumps. Each connection task materializes an owned socket frame only when it actually writes, off the broadcast path. ### `unregister` — touch only joined channels It previously scanned **every** channel's subscription set on each disconnect (O(channels)). A new `connection_channels` index records what each connection joined, so cleanup touches only those. ## Tests - New `unregister_cleans_channel_subscriptions` (subscribe to 2 channels → unregister empties both). - Existing gateway tests updated to the `Arc<str>` channel; the shed-slow-consumer and close-signal behavior is unchanged. - Full server suite (131) + `clippy -D warnings` green. Closes #85
perf(gateway): share broadcast payload via Arc<str>; scope unregister cleanup
Some checks failed
Security Audit / Gitleaks (Secrets) (pull_request) Successful in 10s
Security Audit / PNPM Audit (JS SCA) (pull_request) Failing after 21s
Security Audit / Trivy (SCA & IaC) (pull_request) Failing after 28s
CI / Client — test & lint (pull_request) Successful in 34s
Security Audit / Semgrep (SAST) (pull_request) Failing after 1m3s
Security Audit / Cargo Audit (Rust SCA) (pull_request) Failing after 4m13s
CI / Server — test & lint (pull_request) Successful in 6m15s
5cba249f61
`broadcast_to_channel`/`send_to_user`/`broadcast_all` did `payload.to_string()`
per recipient — one heap copy of the same JSON per subscriber, all while holding
the registry read lock. The outbound channel now carries `Arc<str>`: the payload
is built once and each recipient gets a cheap pointer clone, so the lock-held
work is O(recipients) refcount bumps instead of O(recipients) string allocations.
Each connection task converts back to an owned frame only when it actually writes
to its socket, off the broadcast path.

`unregister` previously scanned *every* channel's subscription set on each
disconnect (O(channels)). Track each connection's subscribed channels in
`connection_channels` so cleanup touches only the channels it actually joined.

Adds `unregister_cleans_channel_subscriptions`; updated existing gateway tests to
the `Arc<str>` channel. Full server suite (131) + clippy -D warnings green.

Closes #85

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
icub3d merged commit a6da07c943 into main 2026-07-09 04:15:24 +00:00
icub3d deleted branch feature/85-gateway-arc-payload 2026-07-09 04:15:24 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
icub3d/decentcom!103
No description provided.