Skip to content

perf(v5): replace copy_from_slice in Publish::new and LastWill::new#1043

Open
de-sh wants to merge 2 commits into
bytebeamio:mainfrom
de-sh:perf/v5-copy
Open

perf(v5): replace copy_from_slice in Publish::new and LastWill::new#1043
de-sh wants to merge 2 commits into
bytebeamio:mainfrom
de-sh:perf/v5-copy

Conversation

@de-sh
Copy link
Copy Markdown
Contributor

@de-sh de-sh commented Apr 6, 2026

Both used Bytes::copy_from_slice, which allocates a String and copies its bytes into a second Bytes allocation before dropping the String. Replace with Bytes::from to take ownership of the String's buffer directly — same approach as the v4 Publish constructor — cutting the topic allocation count from 2, down to 1 per publish.

benchmarked with bench/throughput

Scenario Before After
fresh (Vec/msg) 40,157 blocks 30,157 blocks (–1 alloc/msg)
reused (Bytes clone) 30,158 blocks 20,158 blocks (–1 alloc/msg)

Type of change

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.

…new`

Both used `Bytes::copy_from_slice`, which allocates a String and copies
its bytes into a second Bytes allocation before dropping the String.
Replace with `Bytes::from` to take ownership of the String's buffer
directly — same approach as the v4 Publish constructor — cutting the
topic allocation count from 2, down to 1 per publish.
1. `Publish::new` topic bound changed from `T: Into<String>` to `T: Into<Bytes>`.
   `String::from(&str)` (clone) ~> `Bytes::from(String::into_bytes())` = 0 copies.

2. `handle_publish`/`handle_try_publish`/`handle_publish_bytes` now validate the
   topic string before consuming it, passing the resulting `Bytes` directly to
   `Publish::new` instead of passing `&String` (saving on an unnecessary clone).
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