-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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>> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
) -> 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>> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
) -> 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>> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
) -> 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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; |
There was a problem hiding this comment.
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.
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()); |
There was a problem hiding this comment.
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
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 thecount += 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.