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

Add Insert::written_bytes method #191

Open
wants to merge 1 commit into
base: main
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
13 changes: 11 additions & 2 deletions src/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const_assert!(BUFFER_SIZE.is_power_of_two()); // to use the whole buffer's capac
pub struct Insert<T> {
state: InsertState,
buffer: BytesMut,
written_bytes: usize,
#[cfg(feature = "lz4")]
compression: Compression,
send_timeout: Option<Duration>,
Expand Down Expand Up @@ -137,6 +138,7 @@ impl<T> Insert<T> {
sql,
},
buffer: BytesMut::with_capacity(BUFFER_SIZE),
written_bytes: 0,
#[cfg(feature = "lz4")]
compression: client.compression,
send_timeout: None,
Expand Down Expand Up @@ -222,7 +224,7 @@ impl<T> Insert<T> {
}

#[inline(always)]
pub(crate) fn do_write(&mut self, row: &T) -> Result<usize>
pub(crate) fn do_write(&mut self, row: &T) -> Result<()>
where
T: Serialize,
{
Expand All @@ -240,7 +242,8 @@ impl<T> Insert<T> {
self.abort();
}

result.and(Ok(written))
self.written_bytes += written;
result.and(Ok(()))
}

/// Ends `INSERT`, the server starts processing the data.
Expand All @@ -256,6 +259,12 @@ impl<T> Insert<T> {
self.state.terminated();
self.wait_handle().await
}

/// Returns the number of serialized bytes that have been written because of
/// write operations.
pub fn written_bytes(&self) -> usize {
Copy link
Collaborator

@loyd loyd Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add #[inline], please, it's supposed to be called on every row, so it's reasonable to allow over-crate inlining.

Also, please run benchmarks (the insert bench with `--features inserter) to ensure we don't introduce regression here. Run on master and this branch and share results.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it's questionable to use usize vs u64 here.

Formally, it's possible to write more than 4GiB on 32b systems inside one INSERT query.

So, I think we should use u64 here.

self.written_bytes
}

async fn send_chunk(&mut self) -> Result<()> {
debug_assert!(matches!(self.state, InsertState::Active { .. }));
Expand Down
4 changes: 2 additions & 2 deletions src/inserter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ where
}

match self.insert.as_mut().unwrap().do_write(row) {
Ok(bytes) => {
self.pending.bytes += bytes as u64;
Ok(()) => {
self.pending.bytes = self.insert.as_ref().unwrap().written_bytes() as u64;
self.pending.rows += 1;

if !self.in_transaction {
Expand Down
Loading