Skip to content

Commit cb9b511

Browse files
perf(proto): add fast path for handling +OK\r\n
1 parent a1b789b commit cb9b511

File tree

1 file changed

+14
-3
lines changed
  • watermelon-proto/src/proto/decoder

1 file changed

+14
-3
lines changed

watermelon-proto/src/proto/decoder/mod.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ pub(super) fn decode(
6767
loop {
6868
match status {
6969
DecoderStatus::ControlLine { last_bytes_read } => {
70+
if read_buf.starts_with(b"+OK\r\n") {
71+
// Fast path for handling `+OK`
72+
debug_assert_eq!(
73+
*last_bytes_read, 0,
74+
"we shouldn't have handled any bytes before"
75+
);
76+
read_buf.advance("+OK\r\n".len());
77+
return Ok(Some(ServerOp::Success));
78+
}
79+
7080
if *last_bytes_read == read_buf.len() {
7181
// No progress has been made
7282
return Ok(None);
@@ -80,9 +90,7 @@ pub(super) fn decode(
8090
let mut control_line = read_buf.split_to(control_line_len + "\r\n".len());
8191
control_line.truncate(control_line.len() - 2);
8292

83-
return if control_line.starts_with(b"+OK") {
84-
Ok(Some(ServerOp::Success))
85-
} else if control_line.starts_with(b"MSG ") {
93+
return if control_line.starts_with(b"MSG ") {
8694
*status = decode_msg(control_line)?;
8795
continue;
8896
} else if control_line.starts_with(b"HMSG ") {
@@ -92,6 +100,9 @@ pub(super) fn decode(
92100
Ok(Some(ServerOp::Ping))
93101
} else if control_line.starts_with(b"PONG") {
94102
Ok(Some(ServerOp::Pong))
103+
} else if control_line.starts_with(b"+OK") {
104+
// Slow path for handling `+OK`
105+
Ok(Some(ServerOp::Success))
95106
} else if control_line.starts_with(b"-ERR ") {
96107
control_line.advance("-ERR ".len());
97108
if !control_line.starts_with(b"'") || !control_line.ends_with(b"'") {

0 commit comments

Comments
 (0)