Skip to content

Commit

Permalink
Preallocate vector with exact size when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Mosolov Sergey authored and uklotzde committed Jan 31, 2022
1 parent 00d79c4 commit 184b0ee
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl TryFrom<Bytes> for Request {
if bytes.len() < (6 + byte_count as usize) {
return Err(Error::new(ErrorKind::InvalidData, "Invalid byte count"));
}
let mut data = vec![];
let mut data = Vec::with_capacity(quantity as usize);
for _ in 0..quantity {
data.push(rdr.read_u16::<BigEndian>()?);
}
Expand All @@ -191,10 +191,10 @@ impl TryFrom<Bytes> for Request {
let write_address = rdr.read_u16::<BigEndian>()?;
let write_quantity = rdr.read_u16::<BigEndian>()?;
let write_count = rdr.read_u8()? as usize;
let mut data = vec![];
if bytes.len() < (10 + write_count as usize) {
return Err(Error::new(ErrorKind::InvalidData, "Invalid byte count"));
}
let mut data = Vec::with_capacity(write_quantity as usize);
for _ in 0..write_quantity {
data.push(rdr.read_u16::<BigEndian>()?);
}
Expand Down Expand Up @@ -253,7 +253,7 @@ impl TryFrom<Bytes> for Response {
0x04 => {
let byte_count = rdr.read_u8()?;
let quantity = byte_count / 2;
let mut data = vec![];
let mut data = Vec::with_capacity(quantity as usize);
for _ in 0..quantity {
data.push(rdr.read_u16::<BigEndian>()?);
}
Expand All @@ -262,7 +262,7 @@ impl TryFrom<Bytes> for Response {
0x03 => {
let byte_count = rdr.read_u8()?;
let quantity = byte_count / 2;
let mut data = vec![];
let mut data = Vec::with_capacity(quantity as usize);
for _ in 0..quantity {
data.push(rdr.read_u16::<BigEndian>()?);
}
Expand All @@ -276,7 +276,7 @@ impl TryFrom<Bytes> for Response {
0x17 => {
let byte_count = rdr.read_u8()?;
let quantity = byte_count / 2;
let mut data = vec![];
let mut data = Vec::with_capacity(quantity as usize);
for _ in 0..quantity {
data.push(rdr.read_u16::<BigEndian>()?);
}
Expand Down

0 comments on commit 184b0ee

Please sign in to comment.