From 4c9edec0b183353a643b705ea0703a5f9658ef90 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Wed, 20 May 2020 05:37:02 +0900 Subject: [PATCH] Remove unnecessary allocations (#49) --- src/bin/server.rs | 2 +- src/client.rs | 16 +++++++--------- src/frame.rs | 8 ++++---- src/parse.rs | 7 ++----- 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/bin/server.rs b/src/bin/server.rs index f5fa014..ca85288 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -19,7 +19,7 @@ pub async fn main() -> mini_redis::Result<()> { tracing_subscriber::fmt::try_init()?; let cli = Cli::from_args(); - let port = cli.port.unwrap_or(DEFAULT_PORT.to_string()); + let port = cli.port.as_deref().unwrap_or(DEFAULT_PORT); // Bind a TCP listener let listener = TcpListener::bind(&format!("127.0.0.1:{}", port)).await?; diff --git a/src/client.rs b/src/client.rs index 917ba95..290ac0a 100644 --- a/src/client.rs +++ b/src/client.rs @@ -323,7 +323,7 @@ impl Client { // num-subscribed is the number of channels that the client // is currently subscribed to. [subscribe, schannel, ..] - if **subscribe == "subscribe" && **schannel == channel => {} + if *subscribe == "subscribe" && *schannel == channel => {} _ => return Err(response.to_error()), }, frame => return Err(frame.to_error()), @@ -374,12 +374,10 @@ impl Subscriber { match mframe { Frame::Array(ref frame) => match frame.as_slice() { - [message, channel, content] if **message == "message" => { - Ok(Some(Message { - channel: channel.to_string(), - content: Bytes::from(content.to_string()), - })) - } + [message, channel, content] if *message == "message" => Ok(Some(Message { + channel: channel.to_string(), + content: Bytes::from(content.to_string()), + })), _ => Err(mframe.to_error()), }, frame => Err(frame.to_error()), @@ -447,7 +445,7 @@ impl Subscriber { match response { Frame::Array(ref frame) => match frame.as_slice() { - [unsubscribe, channel, ..] if **unsubscribe == "unsubscribe" => { + [unsubscribe, channel, ..] if *unsubscribe == "unsubscribe" => { let len = self.subscribed_channels.len(); if len == 0 { @@ -456,7 +454,7 @@ impl Subscriber { } // unsubscribed channel should exist in the subscribed list at this point - self.subscribed_channels.retain(|c| **channel != &c[..]); + self.subscribed_channels.retain(|c| *channel != &c[..]); // Only a single channel should be removed from the // liste of subscribed channels. diff --git a/src/frame.rs b/src/frame.rs index 1419940..473f43e 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -12,7 +12,7 @@ pub(crate) enum Frame { Integer(u64), Bulk(Bytes), Null, - Array(Vec>), + Array(Vec), } #[derive(Debug)] @@ -38,7 +38,7 @@ impl Frame { pub(crate) fn push_bulk(&mut self, bytes: Bytes) { match self { Frame::Array(vec) => { - vec.push(Box::new(Frame::Bulk(bytes))); + vec.push(Frame::Bulk(bytes)); } _ => panic!("not an array frame"), } @@ -52,7 +52,7 @@ impl Frame { pub(crate) fn push_int(&mut self, value: u64) { match self { Frame::Array(vec) => { - vec.push(Box::new(Frame::Integer(value))); + vec.push(Frame::Integer(value)); } _ => panic!("not an array frame"), } @@ -154,7 +154,7 @@ impl Frame { let mut out = Vec::with_capacity(len); for _ in 0..len { - out.push(Box::new(Frame::parse(src)?)); + out.push(Frame::parse(src)?); } Ok(Frame::Array(out)) diff --git a/src/parse.rs b/src/parse.rs index 4306150..f2a73b5 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -12,7 +12,7 @@ use std::{fmt, str, vec}; #[derive(Debug)] pub(crate) struct Parse { /// Array frame iterator. - parts: vec::IntoIter>, + parts: vec::IntoIter, } /// Error encountered while parsing a frame. @@ -47,10 +47,7 @@ impl Parse { /// Return the next entry. Array frames are arrays of frames, so the next /// entry is a frame. fn next(&mut self) -> Result { - self.parts - .next() - .map(|frame| *frame) - .ok_or(ParseError::EndOfStream) + self.parts.next().ok_or(ParseError::EndOfStream) } /// Return the next entry as a string.