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 PgBindIter for encoding and use it as the implementation encoding &[T] #3651

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tylerhawkes
Copy link
Contributor

Partial fix for postgres of #294

I've been using this implementation in several different projects and thought that I would see if it can be accepted upstream. I don't care about the name, but it is only useful when calling .bind(PgBindIter(iter...)), so that's what I've been calling it.

I use this for writing inserts using unnest in postgres and it can easily hit 100k rows/sec on any hardware I've run it on (my mac, ec2, gcp, etc.)

The only overhead this implementation has over the current one for &[T] is the count += 1 in the encoding loop. Hopefully this gets optimized out for slices due to the check in the calling function before delegating the call.

The existing tests for &[T] should cover testing this since it is now the implementation.

I have used a peeking iterator for the first stuff in the past, but that probably has some iteration overhead that is larger than just handling the first item separately.

Copy link
Contributor

@paolobarbolini paolobarbolini left a comment

Choose a reason for hiding this comment

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

Drive-by review: I think this is a good idea. I've left a couple of mostly nitpicks.

// need ownership to iterate
mut iter: I,
buf: &mut PgArgumentBuffer,
) -> Result<IsNull, Box<dyn std::error::Error + Send + Sync + 'static>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
) -> Result<IsNull, Box<dyn std::error::Error + Send + Sync + 'static>> {
) -> Result<IsNull, BoxDynError> {

fn encode_by_ref(
&self,
buf: &mut PgArgumentBuffer,
) -> Result<IsNull, Box<dyn std::error::Error + Send + Sync + 'static>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
) -> Result<IsNull, Box<dyn std::error::Error + Send + Sync + 'static>> {
) -> Result<IsNull, BoxDynError> {

fn encode(
self,
buf: &mut PgArgumentBuffer,
) -> Result<IsNull, Box<dyn std::error::Error + Send + Sync + 'static>>
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
) -> Result<IsNull, Box<dyn std::error::Error + Send + Sync + 'static>>
) -> Result<IsNull, BoxDynError>

}

// set the length now that we know what it is.
buf[len_start..(len_start + 4)].copy_from_slice(count.to_be_bytes().as_slice());
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
buf[len_start..(len_start + 4)].copy_from_slice(count.to_be_bytes().as_slice());
buf[len_start..(len_start + 4)].copy_from_slice(&count.to_be_bytes());

}

let mut count = 1_i32;
const MAX: usize = i32::MAX as usize;
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like an oversight. We've already written 1 item at this point.

Suggested change
const MAX: usize = i32::MAX as usize;
const MAX: usize = i32::MAX as usize - 1;


const OVERFLOW: usize = MAX + 1;
if iter.next().is_some() {
return Err(format!("encoded iterator is too large for Postgres: {OVERFLOW}").into());
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if printing the Iterator::size_hint would be more interesting here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants