Skip to content

Commit

Permalink
engineio(polling/v3): use itoa for packet length and count bytes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Totodore authored Mar 18, 2024
1 parent 4f95065 commit 615409b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
1 change: 1 addition & 0 deletions engineioxide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
bytes.workspace = true
itoa.workspace = true
futures.workspace = true
http.workspace = true
http-body.workspace = true
Expand Down
18 changes: 6 additions & 12 deletions engineioxide/src/transport/polling/payload/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,12 @@ pub fn v3_bin_packet_encoder(packet: Packet, data: &mut bytes::BytesMut) -> Resu
use crate::transport::polling::payload::BINARY_PACKET_SEPARATOR_V3;
use bytes::BufMut;

let mut itoa_buf = itoa::Buffer::new();

match packet {
Packet::BinaryV3(bin) => {
let len = (bin.len() + 1).to_string();
let len_len = if let (_, Some(upper)) = len.chars().size_hint() {
upper
} else {
0
};
let len = itoa_buf.format(bin.len() + 1);
let len_len = len.len(); // len is garanteed to be ascii

data.reserve(1 + len_len + 2 + bin.len());

Expand All @@ -129,12 +127,8 @@ pub fn v3_bin_packet_encoder(packet: Packet, data: &mut bytes::BytesMut) -> Resu
}
packet => {
let packet: String = packet.try_into()?;
let len = packet.len().to_string();
let len_len = if let (_, Some(upper)) = len.chars().size_hint() {
upper
} else {
0
};
let len = itoa_buf.format(packet.len());
let len_len = len.len(); // len is garanteed to be ascii

data.reserve(1 + len_len + 1 + packet.as_bytes().len());

Expand Down

0 comments on commit 615409b

Please sign in to comment.