Skip to content

Commit 877bcad

Browse files
committed
Reduce row materialization overhead
1 parent 589e9f4 commit 877bcad

3 files changed

Lines changed: 47 additions & 22 deletions

File tree

‎src/any.rs‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,23 @@ impl AnyConnectionBackend for OdbcConnection {
9191
Ok(OdbcExecution::Done(result)) => {
9292
stream::once(future::ready(Ok(Either::Left(map_result(result))))).boxed()
9393
}
94-
Ok(OdbcExecution::Rows(rows)) => stream::iter(
95-
rows.into_iter()
96-
.map(|row| {
97-
let column_names = column_names(row.columns());
98-
AnyRow::map_from(&row, column_names).map(Either::Right)
99-
})
100-
.chain(std::iter::once(Ok(Either::Left(map_result(
94+
Ok(OdbcExecution::Rows(rows)) => {
95+
if rows.is_empty() {
96+
stream::once(future::ready(Ok(Either::Left(map_result(
10197
OdbcQueryResult::new(0),
102-
))))),
103-
)
104-
.boxed(),
98+
)))))
99+
.boxed()
100+
} else {
101+
let column_names =
102+
column_names(rows.first().expect("rows is not empty").columns());
103+
let rows = rows.into_iter().map(move |row| {
104+
AnyRow::map_from(&row, Arc::clone(&column_names)).map(Either::Right)
105+
});
106+
let done =
107+
std::iter::once(Ok(Either::Left(map_result(OdbcQueryResult::new(0)))));
108+
stream::iter(rows.chain(done)).boxed()
109+
}
110+
}
105111
Err(error) => stream::once(future::ready(Err(error))).boxed(),
106112
})
107113
.flatten()

‎src/connection.rs‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use sqlx_core::executor::{Execute, Executor};
1212
use sqlx_core::transaction::Transaction;
1313
use sqlx_core::Either;
1414
use std::future::Future;
15+
use std::sync::Arc;
1516

1617
/// Blocking ODBC connection wrapper.
1718
///
@@ -506,10 +507,11 @@ where
506507
),
507508
)
508509
})?;
509-
let columns = bindings
510+
let columns: Arc<[OdbcColumn]> = bindings
510511
.iter()
511512
.map(|binding| binding.column.clone())
512-
.collect::<Vec<_>>();
513+
.collect::<Vec<_>>()
514+
.into();
513515
let mut rows = Vec::new();
514516

515517
while let Some(batch) = row_set_cursor.fetch().map_err(|error| {
@@ -530,12 +532,24 @@ where
530532
})
531533
.collect::<std::result::Result<Vec<_>, _>>()?;
532534

535+
let mut column_iters = column_values
536+
.into_iter()
537+
.map(Vec::into_iter)
538+
.collect::<Vec<_>>();
539+
533540
for row_index in 0..batch.num_rows() {
534-
let values = column_values
535-
.iter()
536-
.map(|values| OdbcValue::new(values[row_index].clone()))
537-
.collect::<Vec<_>>();
538-
rows.push(OdbcRow::new(columns.clone(), values));
541+
let values = column_iters
542+
.iter_mut()
543+
.map(|values| {
544+
values.next().map(OdbcValue::new).ok_or_else(|| {
545+
sqlx_core::Error::Protocol(format!(
546+
"ODBC buffered fetch produced too few values for row {}",
547+
row_index + 1
548+
))
549+
})
550+
})
551+
.collect::<std::result::Result<Vec<_>, _>>()?;
552+
rows.push(OdbcRow::new_shared(Arc::clone(&columns), values));
539553
}
540554
}
541555

@@ -719,7 +733,7 @@ fn collect_rows_unbuffered<C>(mut cursor: C) -> std::result::Result<Vec<OdbcRow>
719733
where
720734
C: Cursor + ResultSetMetadata,
721735
{
722-
let columns = collect_columns(&mut cursor)?;
736+
let columns: Arc<[OdbcColumn]> = collect_columns(&mut cursor)?.into();
723737
let mut rows = Vec::new();
724738

725739
while let Some(mut cursor_row) = cursor.next_row().map_err(|error| {
@@ -730,15 +744,15 @@ where
730744
})? {
731745
let mut values = Vec::with_capacity(columns.len());
732746

733-
for column in &columns {
747+
for column in columns.iter() {
734748
let column_number = u16::try_from(sqlx_core::column::Column::ordinal(column) + 1)
735749
.map_err(|_| {
736750
sqlx_core::Error::Protocol("ODBC column index exceeds u16".to_owned())
737751
})?;
738752
values.push(fetch_value(&mut cursor_row, column_number, column)?);
739753
}
740754

741-
rows.push(OdbcRow::new(columns.clone(), values));
755+
rows.push(OdbcRow::new_shared(Arc::clone(&columns), values));
742756
}
743757

744758
Ok(rows)

‎src/row.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
use crate::{Odbc, OdbcColumn, OdbcValue};
2+
use std::sync::Arc;
23

34
/// Minimal ODBC row container used by the SQLx-core skeleton.
45
#[derive(Debug, Clone, Default)]
56
pub struct OdbcRow {
6-
columns: Vec<OdbcColumn>,
7+
columns: Arc<[OdbcColumn]>,
78
values: Vec<OdbcValue>,
89
}
910

1011
impl OdbcRow {
1112
/// Creates a row from column metadata and values.
1213
pub fn new(columns: Vec<OdbcColumn>, values: Vec<OdbcValue>) -> Self {
14+
Self::new_shared(columns.into(), values)
15+
}
16+
17+
pub(crate) fn new_shared(columns: Arc<[OdbcColumn]>, values: Vec<OdbcValue>) -> Self {
1318
Self { columns, values }
1419
}
1520
}
@@ -18,7 +23,7 @@ impl sqlx_core::row::Row for OdbcRow {
1823
type Database = Odbc;
1924

2025
fn columns(&self) -> &[OdbcColumn] {
21-
&self.columns
26+
self.columns.as_ref()
2227
}
2328

2429
fn try_get_raw<I>(

0 commit comments

Comments
 (0)