Skip to content

postgres-replication: reuse underlying allocation for tuple data #29

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

Merged
merged 1 commit into from
Apr 9, 2025
Merged
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
19 changes: 16 additions & 3 deletions postgres-replication/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,8 @@ impl TupleData {
TUPLE_DATA_TOAST_TAG => TupleData::UnchangedToast,
TUPLE_DATA_TEXT_TAG => {
let len = buf.read_i32::<BigEndian>()?;
let mut data = vec![0; len as usize];
buf.read_exact(&mut data)?;
TupleData::Text(data.into())
let data = buf.read_buf(len as usize)?;
TupleData::Text(data)
}
TUPLE_DATA_BINARY_TAG => {
let len = buf.read_i32::<BigEndian>()?;
Expand Down Expand Up @@ -778,6 +777,20 @@ impl Buffer {
self.idx = self.bytes.len();
buf
}

#[inline]
fn read_buf(&mut self, len: usize) -> io::Result<Bytes> {
if self.idx + len <= self.bytes.len() {
let buf = self.bytes.slice(self.idx..(self.idx + len));
self.idx += len;
Ok(buf)
} else {
Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"unexpected EOF",
))
}
}
}

impl Read for Buffer {
Expand Down
Loading