Skip to content

relay: the accept-and-broadcast path clones each event twice and serializes it once per subscriber #1211

Open

Description

@chappie-daemon

Measured on rewrite/main at ff045445. Three separate costs on the accept-and-fan-out path, all in the same few hundred lines.

  • Two full clones per accepted event. crates/nostream-relay/src/strategies/default.rs clones the event into the EventRecord (event: event.clone()), then clones it again to wrap in an Arc for broadcast. The first is unavoidable if the record owns its event; the second is not — the Arc should be created once, before the record, and both the store call and the broadcast should take it.
  • The same broadcast line is copy-pasted into ten strategy files (broadcast.send(Arc::new(event.clone())): contact_list, delete, dvm_job_request, ephemeral, gift_wrap, group, parameterized_replaceable, replaceable, report, timestamp, plus default). A fix to the clone has to be applied ten times, and nothing fails if one is missed.
  • Fan-out serializes once per subscriber. relay.rs's fan-out clones the Arc per subscription (cheap, correct), but sink::send then calls message.to_text() per connection, so N matching subscribers means N full JSON serializations of the same event. Serializing once per event and writing the same bytes to each sink would remove N−1 of them.

None of this is wrong; it is work done N times where once would do, on the path every published event takes.

Acceptance:

  • One Arc<Event> per accepted event, created before the record, cloned nowhere on the broadcast path.
  • The broadcast is one helper called by every strategy, so the ten copies cannot diverge — asserted by a test that fails if a strategy serializes its own frame.
  • Fan-out serializes the event once per publish, not once per subscriber: measured by a counting encoder in a test with N subscribers, and by the allocation count on the same path.

Activity

  1. added
    rewrite
    rust
    on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    rewriterust

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions