Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Avoid zeroed allocation in reading avro #1127

Merged
merged 1 commit into from
Jun 29, 2022
Merged
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
4 changes: 2 additions & 2 deletions src/io/avro/read/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ fn read_block<R: Read>(
};

block.data.clear();
block.data.resize(bytes, 0);
reader.read_exact(&mut block.data)?;
block.data.try_reserve(bytes)?;
reader.take(bytes as u64).read_to_end(&mut block.data)?;

let mut marker = [0u8; 16];
reader.read_exact(&mut marker)?;
Expand Down
5 changes: 3 additions & 2 deletions src/io/avro/read/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ fn decode_variable<R: Read>(reader: &mut R) -> Result<u64> {

fn _read_binary<R: Read>(reader: &mut R) -> Result<Vec<u8>> {
let len: usize = zigzag_i64(reader)? as usize;
let mut buf = vec![0u8; len];
reader.read_exact(&mut buf)?;
let mut buf = vec![];
buf.try_reserve(len)?;
reader.take(len as u64).read_to_end(&mut buf)?;
Ok(buf)
}

Expand Down
7 changes: 5 additions & 2 deletions src/io/avro/read_async/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ async fn read_block<R: AsyncRead + Unpin + Send>(
};

block.data.clear();
block.data.resize(bytes, 0);
reader.read_exact(&mut block.data).await?;
block.data.try_reserve(bytes)?;
reader
.take(bytes as u64)
.read_to_end(&mut block.data)
.await?;

let mut marker = [0u8; 16];
reader.read_exact(&mut marker).await?;
Expand Down
5 changes: 3 additions & 2 deletions src/io/avro/read_async/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ async fn read_file_marker<R: AsyncRead + Unpin + Send>(reader: &mut R) -> Result

async fn _read_binary<R: AsyncRead + Unpin + Send>(reader: &mut R) -> Result<Vec<u8>> {
let len: usize = zigzag_i64(reader).await? as usize;
let mut buf = vec![0u8; len];
reader.read_exact(&mut buf).await?;
let mut buf = vec![];
buf.try_reserve(len)?;
reader.take(len as u64).read_to_end(&mut buf).await?;
Ok(buf)
}

Expand Down