Skip to content

Commit 4c9edec

Browse files
authored
Remove unnecessary allocations (#49)
1 parent bbaa16c commit 4c9edec

File tree

4 files changed

+14
-19
lines changed

4 files changed

+14
-19
lines changed

src/bin/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub async fn main() -> mini_redis::Result<()> {
1919
tracing_subscriber::fmt::try_init()?;
2020

2121
let cli = Cli::from_args();
22-
let port = cli.port.unwrap_or(DEFAULT_PORT.to_string());
22+
let port = cli.port.as_deref().unwrap_or(DEFAULT_PORT);
2323

2424
// Bind a TCP listener
2525
let listener = TcpListener::bind(&format!("127.0.0.1:{}", port)).await?;

src/client.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl Client {
323323
// num-subscribed is the number of channels that the client
324324
// is currently subscribed to.
325325
[subscribe, schannel, ..]
326-
if **subscribe == "subscribe" && **schannel == channel => {}
326+
if *subscribe == "subscribe" && *schannel == channel => {}
327327
_ => return Err(response.to_error()),
328328
},
329329
frame => return Err(frame.to_error()),
@@ -374,12 +374,10 @@ impl Subscriber {
374374

375375
match mframe {
376376
Frame::Array(ref frame) => match frame.as_slice() {
377-
[message, channel, content] if **message == "message" => {
378-
Ok(Some(Message {
379-
channel: channel.to_string(),
380-
content: Bytes::from(content.to_string()),
381-
}))
382-
}
377+
[message, channel, content] if *message == "message" => Ok(Some(Message {
378+
channel: channel.to_string(),
379+
content: Bytes::from(content.to_string()),
380+
})),
383381
_ => Err(mframe.to_error()),
384382
},
385383
frame => Err(frame.to_error()),
@@ -447,7 +445,7 @@ impl Subscriber {
447445

448446
match response {
449447
Frame::Array(ref frame) => match frame.as_slice() {
450-
[unsubscribe, channel, ..] if **unsubscribe == "unsubscribe" => {
448+
[unsubscribe, channel, ..] if *unsubscribe == "unsubscribe" => {
451449
let len = self.subscribed_channels.len();
452450

453451
if len == 0 {
@@ -456,7 +454,7 @@ impl Subscriber {
456454
}
457455

458456
// unsubscribed channel should exist in the subscribed list at this point
459-
self.subscribed_channels.retain(|c| **channel != &c[..]);
457+
self.subscribed_channels.retain(|c| *channel != &c[..]);
460458

461459
// Only a single channel should be removed from the
462460
// liste of subscribed channels.

src/frame.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub(crate) enum Frame {
1212
Integer(u64),
1313
Bulk(Bytes),
1414
Null,
15-
Array(Vec<Box<Frame>>),
15+
Array(Vec<Frame>),
1616
}
1717

1818
#[derive(Debug)]
@@ -38,7 +38,7 @@ impl Frame {
3838
pub(crate) fn push_bulk(&mut self, bytes: Bytes) {
3939
match self {
4040
Frame::Array(vec) => {
41-
vec.push(Box::new(Frame::Bulk(bytes)));
41+
vec.push(Frame::Bulk(bytes));
4242
}
4343
_ => panic!("not an array frame"),
4444
}
@@ -52,7 +52,7 @@ impl Frame {
5252
pub(crate) fn push_int(&mut self, value: u64) {
5353
match self {
5454
Frame::Array(vec) => {
55-
vec.push(Box::new(Frame::Integer(value)));
55+
vec.push(Frame::Integer(value));
5656
}
5757
_ => panic!("not an array frame"),
5858
}
@@ -154,7 +154,7 @@ impl Frame {
154154
let mut out = Vec::with_capacity(len);
155155

156156
for _ in 0..len {
157-
out.push(Box::new(Frame::parse(src)?));
157+
out.push(Frame::parse(src)?);
158158
}
159159

160160
Ok(Frame::Array(out))

src/parse.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::{fmt, str, vec};
1212
#[derive(Debug)]
1313
pub(crate) struct Parse {
1414
/// Array frame iterator.
15-
parts: vec::IntoIter<Box<Frame>>,
15+
parts: vec::IntoIter<Frame>,
1616
}
1717

1818
/// Error encountered while parsing a frame.
@@ -47,10 +47,7 @@ impl Parse {
4747
/// Return the next entry. Array frames are arrays of frames, so the next
4848
/// entry is a frame.
4949
fn next(&mut self) -> Result<Frame, ParseError> {
50-
self.parts
51-
.next()
52-
.map(|frame| *frame)
53-
.ok_or(ParseError::EndOfStream)
50+
self.parts.next().ok_or(ParseError::EndOfStream)
5451
}
5552

5653
/// Return the next entry as a string.

0 commit comments

Comments
 (0)