Skip to content

Commit 44435bf

Browse files
committed
[xitca-web] reduce memory allocation
1 parent 5e14b91 commit 44435bf

File tree

5 files changed

+82
-121
lines changed

5 files changed

+82
-121
lines changed

frameworks/Rust/xitca-web/Cargo.lock

Lines changed: 31 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frameworks/Rust/xitca-web/Cargo.toml

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pg = ["dep:xitca-postgres"]
2929
# diesel orm optional
3030
diesel = ["dep:diesel", "dep:diesel-async", "dep:xitca-postgres-diesel", "dep:futures-util"]
3131
# toasty orm optional
32-
toasty = ["dep:toasty", "dep:xitca-postgres-toasty"]
32+
toasty = ["dep:toasty", "dep:xitca-postgres-toasty", "futures-util/alloc"]
3333
# http router optional
3434
router = ["xitca-http/router"]
3535
# web optional
@@ -64,7 +64,7 @@ xitca-postgres = { version = "0.3", optional = true }
6464
# diesel orm optional
6565
diesel = { version = "2", features = ["postgres"], optional = true }
6666
diesel-async = { version = "0.7", features = ["bb8", "postgres"], optional = true }
67-
xitca-postgres-diesel = { version = "0.2", optional = true }
67+
xitca-postgres-diesel = { version = "0.2", default-features = false, optional = true }
6868
futures-util = { version = "0.3", default-features = false, optional = true }
6969

7070
# toasty orm optional
@@ -94,16 +94,17 @@ codegen-units = 1
9494
panic = "abort"
9595

9696
[patch.crates-io]
97-
xitca-postgres-diesel = { git = "https://github.com/fakeshadow/xitca-postgres-diesel", rev = "8ce4e5b" }
97+
xitca-postgres-diesel = { git = "https://github.com/fakeshadow/xitca-postgres-diesel", rev = "7671975" }
9898
xitca-postgres-toasty = { git = "https://github.com/fakeshadow/xitca-postgres-toasty", rev = "04bedb8" }
9999

100-
toasty = { git = "https://github.com/tokio-rs/toasty", rev = "e0c84c5" }
101-
toasty-core = { git = "https://github.com/tokio-rs/toasty", rev = "e0c84c5" }
102-
toasty-sql = { git = "https://github.com/tokio-rs/toasty", rev = "e0c84c5" }
103-
104-
xitca-codegen = { git = "http://github.com/HFQR/xitca-web", rev = "c9c4b25" }
105-
xitca-http = { git = "http://github.com/HFQR/xitca-web", rev = "c9c4b25" }
106-
xitca-postgres = { git = "http://github.com/HFQR/xitca-web", rev = "c9c4b25" }
107-
xitca-server = { git = "http://github.com/HFQR/xitca-web", rev = "c9c4b25" }
108-
xitca-service = { git = "http://github.com/HFQR/xitca-web", rev = "c9c4b25" }
109-
xitca-web = { git = "http://github.com/HFQR/xitca-web", rev = "c9c4b25" }
100+
# personal fork for efficient toasty engine fine tuned with pipelined xitca-postgres client
101+
toasty = { git = "https://github.com/fakeshadow/toasty", branch = "engine" }
102+
toasty-core = { git = "https://github.com/fakeshadow/toasty", branch = "engine" }
103+
toasty-sql = { git = "https://github.com/fakeshadow/toasty", branch = "engine" }
104+
105+
xitca-codegen = { git = "http://github.com/HFQR/xitca-web", rev = "cf70ed7" }
106+
xitca-http = { git = "http://github.com/HFQR/xitca-web", rev = "cf70ed7" }
107+
xitca-postgres = { git = "http://github.com/HFQR/xitca-web", rev = "cf70ed7" }
108+
xitca-server = { git = "http://github.com/HFQR/xitca-web", rev = "cf70ed7" }
109+
xitca-service = { git = "http://github.com/HFQR/xitca-web", rev = "cf70ed7" }
110+
xitca-web = { git = "http://github.com/HFQR/xitca-web", rev = "cf70ed7" }

frameworks/Rust/xitca-web/src/db_diesel.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use diesel_async::{
1010
RunQueryDsl,
1111
pooled_connection::{AsyncDieselConnectionManager, bb8},
1212
};
13-
use futures_util::future::{TryFutureExt, try_join, try_join_all};
13+
use futures_util::future::{TryFutureExt, TryJoinAll, try_join};
1414
use xitca_postgres_diesel::AsyncPgConnection;
1515

1616
use crate::{
17-
ser::{Fortune, Fortunes, World},
17+
ser::{Fortunes, World},
1818
util::{DB_URL, HandleResult, Rand},
1919
};
2020

@@ -52,52 +52,54 @@ impl Pool {
5252
}
5353

5454
pub async fn get_worlds(&self, num: u16) -> HandleResult<Vec<World>> {
55-
try_join_all({
55+
{
5656
use crate::schema::world::dsl::*;
5757

5858
let mut conn = self.pool.get().await?;
5959
let mut rng = self.rng.borrow_mut();
6060

61-
core::iter::repeat_with(|| {
61+
core::iter::repeat_with(move || {
6262
let w_id = rng.gen_id();
6363
world.filter(id.eq(w_id)).first(&mut conn).map_err(Into::into)
6464
})
6565
.take(num as _)
66-
.collect::<Vec<_>>()
67-
})
66+
.collect::<TryJoinAll<_>>()
67+
}
6868
.await
6969
}
7070

7171
pub async fn update(&self, num: u16) -> HandleResult<Vec<World>> {
72-
let (get, update) = {
72+
{
7373
use crate::schema::world::dsl::*;
7474

7575
let mut conn = self.pool.get().await?;
7676
let mut rng = self.rng.borrow_mut();
77+
let mut params = Vec::with_capacity(num as _);
7778

78-
let (rngs, get) = core::iter::repeat_with(|| {
79+
let get = core::iter::repeat_with(|| {
7980
let w_id = rng.gen_id();
8081
let rng = rng.gen_id();
8182

8283
let get = world.filter(id.eq(w_id)).first::<World>(&mut conn);
8384

84-
((w_id, rng), async move {
85+
params.push((w_id, rng));
86+
87+
async move {
8588
let mut w = get.await?;
8689
w.randomnumber = rng;
8790
HandleResult::Ok(w)
88-
})
91+
}
8992
})
9093
.take(num as _)
91-
.collect::<(Vec<_>, Vec<_>)>();
92-
93-
let update = diesel::sql_query(update_query_from_ids(rngs))
94-
.execute(&mut conn)
95-
.map_err(Into::into);
94+
.collect::<TryJoinAll<_>>();
9695

97-
(try_join_all(get), update)
98-
};
96+
let sql = update_query_from_ids(params);
97+
let update = diesel::sql_query(sql).execute(&mut conn).map_err(Into::into);
9998

100-
try_join(get, update).await.map(|(worlds, _)| worlds)
99+
try_join(get, update)
100+
}
101+
.await
102+
.map(|(worlds, _)| worlds)
101103
}
102104

103105
pub async fn tell_fortune(&self) -> HandleResult<Fortunes> {

0 commit comments

Comments
 (0)