Skip to content

Commit 9002572

Browse files
authored
[xitca-web] reduce memory allocation (#10343)
* [xitca-web] reduce memory allocation * generate rng with iterator * clean up module layout
1 parent 8abe032 commit 9002572

File tree

10 files changed

+237
-283
lines changed

10 files changed

+237
-283
lines changed

frameworks/Rust/xitca-web/Cargo.lock

Lines changed: 39 additions & 39 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.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::{
1010
util::{DB_URL, HandleResult},
1111
};
1212

13-
use db_util::{FORTUNE_STMT, Shared, UPDATE_BATCH_STMT, WORLD_STMT, not_found};
13+
use db_util::{FORTUNE_STMT, Shared, UPDATE_STMT, WORLD_STMT, not_found};
1414

1515
pub struct Client {
1616
pool: Pool,
@@ -35,19 +35,19 @@ impl Client {
3535
}
3636

3737
pub async fn get_worlds(&self, num: u16) -> HandleResult<Vec<World>> {
38-
let len = num as usize;
39-
4038
let mut conn = self.pool.get().await?;
4139
let stmt = WORLD_STMT.execute(&mut conn).await?;
4240

4341
let mut res = {
4442
let (ref mut rng, ref mut buf) = *self.shared.borrow_mut();
45-
let mut pipe = Pipeline::with_capacity_from_buf(len, buf);
46-
(0..num).try_for_each(|_| stmt.bind([rng.gen_id()]).query(&mut pipe))?;
43+
let mut pipe = Pipeline::with_capacity_from_buf(num as _, buf);
44+
rng.gen_multi()
45+
.take(num as _)
46+
.try_for_each(|id| stmt.bind([id]).query(&mut pipe))?;
4747
pipe.query(&conn.consume())?
4848
};
4949

50-
let mut worlds = Vec::with_capacity(len);
50+
let mut worlds = Vec::with_capacity(num as _);
5151

5252
while let Some(mut item) = res.try_next().await? {
5353
let row = item.try_next().await?.ok_or_else(not_found)?;
@@ -58,28 +58,26 @@ impl Client {
5858
}
5959

6060
pub async fn update(&self, num: u16) -> HandleResult<Vec<World>> {
61-
let len = num as usize;
62-
6361
let mut conn = self.pool.get().await?;
6462
let world_stmt = WORLD_STMT.execute(&mut conn).await?;
65-
let update_stmt = UPDATE_BATCH_STMT.execute(&mut conn).await?;
63+
let update_stmt = UPDATE_STMT.execute(&mut conn).await?;
6664

6765
let (mut res, worlds) = {
6866
let (ref mut rng, ref mut buf) = *self.shared.borrow_mut();
69-
let mut pipe = Pipeline::with_capacity_from_buf(len + 1, buf);
70-
71-
let (mut params, worlds) = core::iter::repeat_with(|| {
72-
let id = rng.gen_id();
73-
let rand = rng.gen_id();
74-
world_stmt.bind([id]).query(&mut pipe)?;
75-
HandleResult::Ok(((id, rand), World::new(id, rand)))
76-
})
77-
.take(len)
78-
.collect::<Result<(Vec<_>, Vec<_>), _>>()?;
79-
80-
params.sort();
81-
let (ids, rngs) = params.into_iter().collect::<(Vec<_>, Vec<_>)>();
82-
67+
let mut pipe = Pipeline::with_capacity_from_buf((num + 1) as _, buf);
68+
69+
let mut ids = rng.gen_multi().take(num as _).collect::<Vec<_>>();
70+
ids.sort();
71+
72+
let (rngs, worlds) = ids
73+
.iter()
74+
.cloned()
75+
.zip(rng.gen_multi())
76+
.map(|(id, rand)| {
77+
world_stmt.bind([id]).query(&mut pipe)?;
78+
HandleResult::Ok((rand, World::new(id, rand)))
79+
})
80+
.collect::<HandleResult<(Vec<_>, Vec<_>)>>()?;
8381
update_stmt.bind([&ids, &rngs]).query(&mut pipe)?;
8482
(pipe.query(&conn.consume())?, worlds)
8583
};

0 commit comments

Comments
 (0)