Skip to content

Commit

Permalink
Merge pull request #8 from sakex/optimisation/dont-reallocate-on-read
Browse files Browse the repository at this point in the history
Don't reallocate the read buffer on every read in
  • Loading branch information
rainliu committed May 6, 2022
2 parents f74400d + 7875d49 commit 4e0b7ce
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions crates/media/src/io/h264_reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ pub struct H264Reader<R: Read> {
count_of_consecutive_zero_bytes: usize,
nal_prefix_parsed: bool,
read_buffer: Vec<u8>,
temp_buf: Vec<u8>,
}

impl<R: Read> H264Reader<R> {
Expand All @@ -154,13 +155,14 @@ impl<R: Read> H264Reader<R> {
count_of_consecutive_zero_bytes: 0,
nal_prefix_parsed: false,
read_buffer: vec![],
temp_buf: vec![0u8; 4096],
}
}

fn read(&mut self, num_to_read: usize) -> Bytes {
let mut buf = vec![0u8; 4096];
let buf = &mut self.temp_buf;
while self.read_buffer.len() < num_to_read {
let n = match self.reader.read(&mut buf) {
let n = match self.reader.read(buf) {
Ok(n) => {
if n == 0 {
break;
Expand Down

0 comments on commit 4e0b7ce

Please sign in to comment.