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

Perf improvements #46

Merged
merged 2 commits into from
Dec 23, 2022
Merged
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
10 changes: 10 additions & 0 deletions supabase-wrappers/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ impl Row {
iter = keep.iter();
self.cells.retain(|_| *iter.next().unwrap());
}

pub fn clear(&mut self) {
self.cols.clear();
self.cells.clear();
}
}

/// A restiction value used in [`Qual`], either a [`Cell`] or an array of [`Cell`]
Expand Down Expand Up @@ -379,6 +384,11 @@ pub trait ForeignDataWrapper {
/// [See more details](https://www.postgresql.org/docs/current/fdw-callbacks.html#FDW-CALLBACKS-SCAN).
fn iter_scan(&mut self) -> Option<Row>;

fn iter_scan_borrowed(&mut self, row: &mut Row) -> Option<()> {
let _ = std::mem::replace(row, self.iter_scan()?);
Some(())
}

/// Called when restart the scan from the beginning.
///
/// [See more details](https://www.postgresql.org/docs/current/fdw-callbacks.html#FDW-CALLBACKS-SCAN).
Expand Down
18 changes: 12 additions & 6 deletions supabase-wrappers/src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct FdwState<W: ForeignDataWrapper> {
// query result list
values: Vec<Datum>,
nulls: Vec<bool>,
row: Row,
}

impl<W: ForeignDataWrapper> FdwState<W> {
Expand All @@ -56,6 +57,7 @@ impl<W: ForeignDataWrapper> FdwState<W> {
.switch_to(|_| PgMemoryContexts::new("Wrappers temp data")),
values: Vec::new(),
nulls: Vec::new(),
row: Row::new(),
}
}

Expand All @@ -79,10 +81,11 @@ impl<W: ForeignDataWrapper> FdwState<W> {
)
}

fn iter_scan(&mut self) -> Option<Row> {
self.instance.iter_scan()
fn iter_scan_borrowed(&mut self) -> Option<()> {
self.instance.iter_scan_borrowed(&mut self.row)
}


fn re_scan(&mut self) {
self.instance.re_scan()
}
Expand Down Expand Up @@ -306,7 +309,8 @@ pub(super) extern "C" fn begin_foreign_scan<W: ForeignDataWrapper>(
pub(super) extern "C" fn iterate_foreign_scan<W: ForeignDataWrapper>(
node: *mut pg_sys::ForeignScanState,
) -> *mut pg_sys::TupleTableSlot {
debug2!("---> iterate_foreign_scan");
// `debug!` macros are quite expensive at the moment, so avoid logging in the inner loop
// debug2!("---> iterate_foreign_scan");
unsafe {
let mut state = PgBox::<FdwState<W>>::from_pg((*node).fdw_state as _);

Expand All @@ -317,8 +321,9 @@ pub(super) extern "C" fn iterate_foreign_scan<W: ForeignDataWrapper>(
state.tmp_ctx.reset();
let mut old_ctx = state.tmp_ctx.set_as_current();

if let Some(mut row) = state.iter_scan() {
if row.cols.len() != state.tgts.len() {
state.row.clear();
if state.iter_scan_borrowed().is_some() {
if state.row.cols.len() != state.tgts.len() {
report_error(
PgSqlErrorCode::ERRCODE_FDW_INVALID_COLUMN_NUMBER,
"target column number not match",
Expand All @@ -327,8 +332,9 @@ pub(super) extern "C" fn iterate_foreign_scan<W: ForeignDataWrapper>(
return slot;
}

for (i, cell) in row.cells.iter_mut().enumerate() {
for i in 0..state.row.cells.len() {
let att_idx = state.tgt_attnos[i] - 1;
let cell = state.row.cells.get_unchecked_mut(i);
match cell.take() {
Some(cell) => {
state.values[att_idx] = cell.into_datum().unwrap();
Expand Down