Skip to content

Commit f010d11

Browse files
authored
Reduce Vec allocations (#2835)
1 parent 4545f45 commit f010d11

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

src/dynamic/execute.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{
22
ConnectionTrait, DbBackend, DbErr, EntityTrait, FromQueryResult, QueryResult, Select,
33
Statement, dynamic,
44
};
5+
use itertools::Itertools;
56
use sea_query::{DynIden, Expr, IntoIden, SelectStatement};
67
use std::marker::PhantomData;
78

@@ -96,11 +97,10 @@ where
9697
where
9798
C: ConnectionTrait,
9899
{
99-
let rows = db.query_all(&self.query).await?;
100-
let mut models = Vec::new();
101-
for row in rows.into_iter() {
102-
models.push(self.selector.from_raw_query_result(row)?);
103-
}
104-
Ok(models)
100+
db.query_all(&self.query)
101+
.await?
102+
.into_iter()
103+
.map(|row| self.selector.from_raw_query_result(row))
104+
.try_collect()
105105
}
106106
}

src/executor/returning.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::SelectorTrait;
22
use crate::{ConnectionTrait, StatementBuilder, error::*};
3+
use itertools::Itertools;
34
use std::marker::PhantomData;
45

56
#[derive(Clone, Debug)]
@@ -39,11 +40,10 @@ where
3940
where
4041
C: ConnectionTrait,
4142
{
42-
let rows = db.query_all(&self.query).await?;
43-
let mut models = Vec::new();
44-
for row in rows.into_iter() {
45-
models.push(S::from_raw_query_result(row)?);
46-
}
47-
Ok(models)
43+
db.query_all(&self.query)
44+
.await?
45+
.into_iter()
46+
.map(|row| S::from_raw_query_result(row))
47+
.try_collect()
4848
}
4949
}

src/executor/select.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
StreamTrait, TryGetableMany, error::*,
88
};
99
use futures_util::{Stream, TryStreamExt};
10+
use itertools::Itertools;
1011
use sea_query::SelectStatement;
1112
use std::{marker::PhantomData, pin::Pin};
1213

@@ -674,12 +675,11 @@ where
674675
where
675676
C: ConnectionTrait,
676677
{
677-
let rows = db.query_all(&self.query).await?;
678-
let mut models = Vec::new();
679-
for row in rows.into_iter() {
680-
models.push(S::from_raw_query_result(row)?);
681-
}
682-
Ok(models)
678+
db.query_all(&self.query)
679+
.await?
680+
.into_iter()
681+
.map(|row| S::from_raw_query_result(row))
682+
.try_collect()
683683
}
684684

685685
/// Stream the results of the Select operation
@@ -952,12 +952,11 @@ where
952952
where
953953
C: ConnectionTrait,
954954
{
955-
let rows = db.query_all_raw(self.stmt).await?;
956-
let mut models = Vec::new();
957-
for row in rows.into_iter() {
958-
models.push(S::from_raw_query_result(row)?);
959-
}
960-
Ok(models)
955+
db.query_all_raw(self.stmt)
956+
.await?
957+
.into_iter()
958+
.map(|row| S::from_raw_query_result(row))
959+
.try_collect()
961960
}
962961

963962
/// Stream the results of the Select operation

0 commit comments

Comments
 (0)