Skip to content

Commit 451efad

Browse files
luca020400andymandias
authored andcommitted
Don't dup message bytes before parsing
The first allocation may happen if the slice isn't a valid UTF-8 string or when we run the nom parser over it.
1 parent c3c3354 commit 451efad

2 files changed

Lines changed: 4 additions & 6 deletions

File tree

irc/proto/src/parse.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use nom::{Finish, IResult, Parser};
1212

1313
use crate::{Command, Message, Source, Tags, User};
1414

15-
pub fn message_bytes(bytes: Vec<u8>) -> Result<Message, Error> {
16-
let input = String::from_utf8_lossy(&bytes);
15+
pub fn message_bytes(bytes: &[u8]) -> Result<Message, Error> {
16+
let input = String::from_utf8_lossy(bytes);
1717
message(&input)
1818
}
1919

@@ -496,7 +496,7 @@ mod test {
496496
];
497497

498498
for (test, expected) in tests {
499-
let message = super::message_bytes(test).unwrap();
499+
let message = super::message_bytes(&test).unwrap();
500500
assert_eq!(message, expected);
501501
}
502502
}

irc/src/codec.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ impl Decoder for Codec {
2020
return Ok(None);
2121
};
2222

23-
let bytes = Vec::from(src.split_to(pos + 2));
24-
25-
Ok(Some(parse::message_bytes(bytes)))
23+
Ok(Some(parse::message_bytes(&src.split_to(pos + 2))))
2624
}
2725
}
2826

0 commit comments

Comments
 (0)