Skip to content

Commit a72a46f

Browse files
committed
perf(v5): replace copy_from_slice in Publish::new and LastWill::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 parent 2167da0 commit a72a46f

2 files changed

Lines changed: 2 additions & 2 deletions

File tree

rumqttc/src/v5/mqttbytes/v5/connect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ impl LastWill {
371371
retain: bool,
372372
properties: Option<LastWillProperties>,
373373
) -> LastWill {
374-
let topic = Bytes::copy_from_slice(topic.into().as_bytes());
374+
let topic = Bytes::from(topic.into().into_bytes());
375375
LastWill {
376376
topic,
377377
message: Bytes::from(payload.into()),

rumqttc/src/v5/mqttbytes/v5/publish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl Publish {
2020
payload: P,
2121
properties: Option<PublishProperties>,
2222
) -> Self {
23-
let topic = Bytes::copy_from_slice(topic.into().as_bytes());
23+
let topic = Bytes::from(topic.into().into_bytes());
2424
Self {
2525
qos,
2626
topic,

0 commit comments

Comments
 (0)