Skip to content

perf(v4): only one never ending sleep - #1040

Open
de-sh wants to merge 1 commit into
bytebeamio:mainfrom
de-sh:perf/lazy-sleep-alloc
Open

de-sh wants to merge 1 commit into
bytebeamio:mainfrom
de-sh:perf/lazy-sleep-alloc

Conversation

@de-sh

@de-sh de-sh commented Apr 4, 2026 •

Copy link
Copy Markdown
Contributor

Fewer allocations when keep-alive is not set => better performance

Using a perf benchmark against the client, I was able to confirm 2x througput and ~N fewer allocations.

Metric bench/throughput (baseline) perf/lazy-sleep-alloc (fix) Delta
fresh — total bytes 2,274,000 1,235,664 −45.6%
fresh — total blocks 40,141 30,157 −9,984 allocs
reused — total bytes 2,274,024 1,235,688 −45.6%
reused — total blocks 40,142 30,158 −9,984 allocs
time / 10k msgs (fresh) 158.8 ms 78.8 ms −50% / 2× faster
time / 10k msgs (reused) 152.4 ms 75.4 ms −50% / 2× faster

Regarding the Duration::MAX change

This was a latent spin hazard. sleep(Duration::ZERO) produces a future that is ready instantly. If that select! branch fires on every call to select(). That would be a silent busy-loop: no panic, no log, just 100% CPU usage and also, no keepalives sent, which is why Duration::MAX makes more sense here.

Type of change

Performance improvement, should not break anything. Impact is clearest against v4 EventLoop that that has no keepalive configured.

Checklist:

  • Formatted with cargo fmt
  • Make an entry to CHANGELOG.md if it's relevant to the users of the library. If it's not relevant mention why.

Fewer allocations when keep-alive is not set => better performance
@de-sh de-sh changed the title perf(v4): initialize never ending sleep perf(v4): only one never ending sleep Apr 4, 2026
@de-sh
de-sh force-pushed the perf/lazy-sleep-alloc branch from 6d374cf to e876363 Compare April 5, 2026 07:22
thehouseisonfire added a commit to thehouseisonfire/rumqtt that referenced this pull request Apr 24, 2026
thehouseisonfire added a commit to thehouseisonfire/rumqtt that referenced this pull request Apr 29, 2026
…etime (MQTT 5 §3.3.2.3.4)

Topic aliases in MQTT 5 are scoped to a single network connection and must
not survive across reconnects. Previously the single `topic_alises` map was
never cleared on reconnect, causing alias-only publishes to be replayed with
empty topics (protocol-invalid) or stale mappings from a previous connection.

Changes:
- Split `topic_alises` into `incoming_topic_aliases` / `outgoing_topic_aliases`
  to distinguish server-to-client from client-to-server alias directions.
- Clear both maps and reset `broker_topic_alias_max` on CONNACK, clean(), and
  session reset via `reset_connection_scoped_state()`.
- During replay (`clean` / `EventLoop::clean`), resolve alias-only publishes
  using the outgoing alias map captured before the reset. Publishes whose
  topic cannot be recovered are dropped with the new
  `PublishNoticeError::TopicAliasReplayUnavailable(alias)` instead of being
  sent with an empty topic.
- Strip topic-alias from replayed publishes (the alias value is meaningless
  on a new connection) and re-record the mapping from topic-bearing publishes
  encountered during replay so later alias-only publishes in the same batch
  can be resolved.
- Store the full topic in `outgoing_pub` tracking (via
  `publish_for_replay_tracking`) so collision-replay also has the topic
  available even if the original publish used an alias-only form.
- DISCONNECT with `ProtocolError` on unknown incoming aliases (early return
  instead of fall-through after `handle_protocol_error`).
- Extract `validate_outgoing_topic_alias`, `record_outgoing_topic_alias`,
  `publish_topic_alias`, and `strip_publish_topic_alias` helpers for clarity.

Roughly maps to upstream PR bytebeamio#1040.

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
thehouseisonfire added a commit to thehouseisonfire/rumqtt that referenced this pull request Jul 20, 2026
thehouseisonfire added a commit to thehouseisonfire/rumqtt that referenced this pull request Jul 20, 2026
…etime (MQTT 5 §3.3.2.3.4)

Topic aliases in MQTT 5 are scoped to a single network connection and must
not survive across reconnects. Previously the single `topic_alises` map was
never cleared on reconnect, causing alias-only publishes to be replayed with
empty topics (protocol-invalid) or stale mappings from a previous connection.

Changes:
- Split `topic_alises` into `incoming_topic_aliases` / `outgoing_topic_aliases`
  to distinguish server-to-client from client-to-server alias directions.
- Clear both maps and reset `broker_topic_alias_max` on CONNACK, clean(), and
  session reset via `reset_connection_scoped_state()`.
- During replay (`clean` / `EventLoop::clean`), resolve alias-only publishes
  using the outgoing alias map captured before the reset. Publishes whose
  topic cannot be recovered are dropped with the new
  `PublishNoticeError::TopicAliasReplayUnavailable(alias)` instead of being
  sent with an empty topic.
- Strip topic-alias from replayed publishes (the alias value is meaningless
  on a new connection) and re-record the mapping from topic-bearing publishes
  encountered during replay so later alias-only publishes in the same batch
  can be resolved.
- Store the full topic in `outgoing_pub` tracking (via
  `publish_for_replay_tracking`) so collision-replay also has the topic
  available even if the original publish used an alias-only form.
- DISCONNECT with `ProtocolError` on unknown incoming aliases (early return
  instead of fall-through after `handle_protocol_error`).
- Extract `validate_outgoing_topic_alias`, `record_outgoing_topic_alias`,
  `publish_topic_alias`, and `strip_publish_topic_alias` helpers for clarity.

Roughly maps to upstream PR bytebeamio#1040.

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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