Skip to content

Commit

Permalink
feat: optimize string length comp for v3_bin_packet_encoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
Totodore authored Apr 14, 2024
1 parent c665a5f commit e799085
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
3 changes: 2 additions & 1 deletion engineioxide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ smallvec = { version = "1.13.1", features = ["union"] }
tracing = { workspace = true, optional = true }

# Engine.io V3 payload
itoa = { workspace = true, optional = true }
memchr = { version = "2.5.0", optional = true }
unicode-segmentation = { version = "1.10.1", optional = true }

Expand All @@ -53,7 +54,7 @@ axum.workspace = true
hyper-util = { workspace = true, features = ["tokio", "client-legacy"] }

[features]
v3 = ["memchr", "unicode-segmentation"]
v3 = ["memchr", "unicode-segmentation", "itoa"]
tracing = ["dep:tracing"]

[[bench]]
Expand Down
23 changes: 8 additions & 15 deletions engineioxide/src/transport/polling/payload/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,16 @@ 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 = 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.format(bin.len() + 1);
let len_len = len.len(); // len is guaranteed to be ascii

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

data.put_u8(0x1);
for char in len.chars() {
data.put_u8(0x1); // 1 = binary
for char in len.bytes() {
data.put_u8(char as u8 - 48);

Check warning

Code scanning / clippy

casting to the same type is unnecessary (u8 -> u8) Warning

casting to the same type is unnecessary (u8 -> u8)
}
data.put_u8(BINARY_PACKET_SEPARATOR_V3); // separator
Expand All @@ -137,17 +134,13 @@ 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.format(packet.len() + 1);
let len_len = len.len(); // len is guaranteed to be ascii

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

data.put_u8(0x0); // 0 = string
for char in len.chars() {
for char in len.bytes() {
data.put_u8(char as u8 - 48);

Check warning

Code scanning / clippy

casting to the same type is unnecessary (u8 -> u8) Warning

casting to the same type is unnecessary (u8 -> u8)
}
data.put_u8(BINARY_PACKET_SEPARATOR_V3); // separator
Expand Down

0 comments on commit e799085

Please sign in to comment.