Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't preallocate a full vector, allocate on demand #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions symphonia-core/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ pub trait ReadBytes {
/// Reads exactly the number of bytes required to fill be provided buffer or returns an error.
fn read_buf_exact(&mut self, buf: &mut [u8]) -> io::Result<()>;

fn read_bytes_exact(&mut self, count: usize, to: &mut Vec<u8>) -> io::Result<()> {
let mut buffer = vec![0_u8; count.min(1024)];

let mut bytes_left = count;
loop {
let length = self.read_buf(&mut buffer[0..(bytes_left.min(1024))])?;
if length == 0 {
return Err(io::ErrorKind::UnexpectedEof.into());
}
to.extend_from_slice(&buffer[0..length]);
bytes_left -= length;
if bytes_left == 0 {
return Ok(());
}
}
}

/// Reads a single unsigned byte from the stream and returns it or an error.
#[inline(always)]
fn read_u8(&mut self) -> io::Result<u8> {
Expand Down Expand Up @@ -275,8 +292,8 @@ pub trait ReadBytes {
/// Reads exactly the number of bytes requested, and returns a boxed slice of the data or an
/// error.
fn read_boxed_slice_exact(&mut self, len: usize) -> io::Result<Box<[u8]>> {
let mut buf = vec![0u8; len];
self.read_buf_exact(&mut buf)?;
let mut buf = Vec::new();
self.read_bytes_exact(len, &mut buf)?;
Ok(buf.into_boxed_slice())
}

Expand Down
4 changes: 2 additions & 2 deletions symphonia-metadata/src/vorbis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ pub fn read_comment_no_framing<B: ReadBytes>(
let comment_length = reader.read_u32()?;

// Read the comment string.
let mut comment_byte = vec![0; comment_length as usize];
reader.read_buf_exact(&mut comment_byte)?;
let mut comment_byte = Vec::new();
reader.read_bytes_exact(comment_length as usize, &mut comment_byte)?;

// Parse the comment string into a Tag and insert it into the parsed tag list.
metadata.add_tag(parse(&String::from_utf8_lossy(&comment_byte)));
Expand Down
8 changes: 4 additions & 4 deletions symphonia-utils-xiph/src/flac/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,8 @@ pub fn read_picture_block<B: ReadBytes>(
let media_type_len = reader.read_be_u32()? as usize;

// Read the Media Type bytes
let mut media_type_buf = vec![0u8; media_type_len];
reader.read_buf_exact(&mut media_type_buf)?;
let mut media_type_buf = Vec::new();
reader.read_bytes_exact(media_type_len, &mut media_type_buf)?;

// Convert Media Type bytes to an ASCII string. Non-printable ASCII characters are invalid.
let media_type = match printable_ascii_to_string(&media_type_buf) {
Expand All @@ -439,8 +439,8 @@ pub fn read_picture_block<B: ReadBytes>(
let desc_len = reader.read_be_u32()? as usize;

// Read the description bytes.
let mut desc_buf = vec![0u8; desc_len];
reader.read_buf_exact(&mut desc_buf)?;
let mut desc_buf = Vec::new();
reader.read_bytes_exact(desc_len, &mut desc_buf)?;

let desc = String::from_utf8_lossy(&desc_buf);

Expand Down