diff --git a/crates/ember-protocol/src/parse.rs b/crates/ember-protocol/src/parse.rs index d47d98fc..89c74528 100644 --- a/crates/ember-protocol/src/parse.rs +++ b/crates/ember-protocol/src/parse.rs @@ -31,6 +31,7 @@ const MAX_BULK_LEN: i64 = 512 * 1024 * 1024; /// Returns `Ok(Some(frame))` if a complete frame was parsed, /// `Ok(None)` if the buffer doesn't contain enough data yet, /// or `Err(...)` if the data is malformed. +#[inline] pub fn parse_frame(buf: &[u8]) -> Result, ProtocolError> { if buf.is_empty() { return Ok(None); diff --git a/crates/ember-protocol/src/serialize.rs b/crates/ember-protocol/src/serialize.rs index 1f68cc54..4c2df431 100644 --- a/crates/ember-protocol/src/serialize.rs +++ b/crates/ember-protocol/src/serialize.rs @@ -14,6 +14,7 @@ impl Frame { /// /// Writes the full RESP3 wire representation, including type prefix /// and trailing `\r\n` delimiters. + #[inline] pub fn serialize(&self, dst: &mut BytesMut) { match self { Frame::Simple(s) => { @@ -63,6 +64,7 @@ impl Frame { } /// Writes an i64 as its decimal ASCII representation directly into the buffer. +#[inline] fn write_i64(val: i64, dst: &mut BytesMut) { let mut buf = itoa::Buffer::new(); dst.put_slice(buf.format(val).as_bytes()); diff --git a/crates/ember-server/src/connection.rs b/crates/ember-server/src/connection.rs index def8f565..e0de1290 100644 --- a/crates/ember-server/src/connection.rs +++ b/crates/ember-server/src/connection.rs @@ -331,7 +331,7 @@ fn handle_sub_command( let rx = pubsub.subscribe(&ch); channel_rxs.insert(ch.clone(), rx); let count = channel_rxs.len() + pattern_rxs.len(); - serialize_sub_response("subscribe", &ch, count, out); + serialize_sub_response(b"subscribe", &ch, count, out); } } Command::Unsubscribe { channels } => { @@ -342,18 +342,18 @@ fn handle_sub_command( channel_rxs.remove(&ch); pubsub.unsubscribe(&ch); let count = channel_rxs.len() + pattern_rxs.len(); - serialize_sub_response("unsubscribe", &ch, count, out); + serialize_sub_response(b"unsubscribe", &ch, count, out); } if channel_rxs.is_empty() && pattern_rxs.is_empty() { // send a final response with count 0 if we had nothing - serialize_sub_response("unsubscribe", "", 0, out); + serialize_sub_response(b"unsubscribe", "", 0, out); } } else { for ch in channels { channel_rxs.remove(&ch); pubsub.unsubscribe(&ch); let count = channel_rxs.len() + pattern_rxs.len(); - serialize_sub_response("unsubscribe", &ch, count, out); + serialize_sub_response(b"unsubscribe", &ch, count, out); } } } @@ -377,7 +377,7 @@ fn handle_sub_command( let rx = pubsub.psubscribe(&pat); pattern_rxs.insert(pat.clone(), rx); let count = channel_rxs.len() + pattern_rxs.len(); - serialize_sub_response("psubscribe", &pat, count, out); + serialize_sub_response(b"psubscribe", &pat, count, out); } } Command::PUnsubscribe { patterns } => { @@ -387,17 +387,17 @@ fn handle_sub_command( pattern_rxs.remove(&pat); pubsub.punsubscribe(&pat); let count = channel_rxs.len() + pattern_rxs.len(); - serialize_sub_response("punsubscribe", &pat, count, out); + serialize_sub_response(b"punsubscribe", &pat, count, out); } if channel_rxs.is_empty() && pattern_rxs.is_empty() { - serialize_sub_response("punsubscribe", "", 0, out); + serialize_sub_response(b"punsubscribe", "", 0, out); } } else { for pat in patterns { pattern_rxs.remove(&pat); pubsub.punsubscribe(&pat); let count = channel_rxs.len() + pattern_rxs.len(); - serialize_sub_response("punsubscribe", &pat, count, out); + serialize_sub_response(b"punsubscribe", &pat, count, out); } } } @@ -462,9 +462,9 @@ async fn recv_any_message( } /// Serializes a subscribe/unsubscribe response: ["type", channel, count] -fn serialize_sub_response(kind: &str, channel: &str, count: usize, out: &mut BytesMut) { +fn serialize_sub_response(kind: &'static [u8], channel: &str, count: usize, out: &mut BytesMut) { Frame::Array(vec![ - Frame::Bulk(Bytes::copy_from_slice(kind.as_bytes())), + Frame::Bulk(Bytes::from_static(kind)), Frame::Bulk(Bytes::copy_from_slice(channel.as_bytes())), Frame::Integer(count as i64), ])