Skip to content

Commit

Permalink
More lazily advance ptr over arrays
Browse files Browse the repository at this point in the history
Array iteration was using the wrong tempo for advancing pointers,
so arrays with leading nulls sometimes would advance too far.
  • Loading branch information
workingjubilee authored and eeeebbbbrrrr committed Jun 29, 2023
1 parent a2f8374 commit 4a4c070
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions pgrx/src/datum/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,9 +626,13 @@ impl<'a, T: FromDatum> Iterator for ArrayIterator<'a, T> {
fn next(&mut self) -> Option<Self::Item> {
let Self { array, curr, ptr } = self;
let Some(is_null) = array.null_slice.get(*curr) else { return None };
let element = unsafe { array.bring_it_back_now(*ptr, is_null) };
*curr += 1;
if let Some(false) = array.null_slice.get(*curr) {

let element = unsafe { array.bring_it_back_now(*ptr, is_null) };
if !is_null {
// SAFETY: This has to not move for nulls, as they occupy 0 data bytes,
// and it has to move only after unpacking a non-null varlena element,
// as the iterator starts by pointing to the first non-null element!
*ptr = unsafe { array.one_hop_this_time(*ptr) };
}
Some(element)
Expand Down Expand Up @@ -679,9 +683,13 @@ impl<'a, T: FromDatum> Iterator for ArrayIntoIterator<'a, T> {
fn next(&mut self) -> Option<Self::Item> {
let Self { array, curr, ptr } = self;
let Some(is_null) = array.null_slice.get(*curr) else { return None };
let element = unsafe { array.bring_it_back_now(*ptr, is_null) };
*curr += 1;
if let Some(false) = array.null_slice.get(*curr) {

let element = unsafe { array.bring_it_back_now(*ptr, is_null) };
if !is_null {
// SAFETY: This has to not move for nulls, as they occupy 0 data bytes,
// and it has to move only after unpacking a non-null varlena element,
// as the iterator starts by pointing to the first non-null element!
*ptr = unsafe { array.one_hop_this_time(*ptr) };
}
Some(element)
Expand Down

0 comments on commit 4a4c070

Please sign in to comment.