Skip to content

Commit 9f6e9f1

Browse files
committed
ntex: Update bench tweaks
1 parent 3d7306d commit 9f6e9f1

File tree

2 files changed

+23
-41
lines changed

2 files changed

+23
-41
lines changed

frameworks/Rust/ntex/Cargo.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ neon = ["ntex/neon"]
6767
neon-uring = ["ntex/neon-uring"]
6868

6969
[dependencies]
70-
ntex = "2.12"
71-
ntex-compio = "0.2"
72-
ntex-neon = "0.1.13"
73-
ntex-net = "2.5.9"
70+
ntex = "2.13"
71+
ntex-compio = "0.3"
72+
ntex-neon = "0.1.31"
73+
ntex-net = "2.5.27"
7474
ntex-bytes = { version = "0.1.21", features=["simd"] }
7575
mimalloc = { version = "0.1.25", default-features = false }
7676
snmalloc-rs = { version = "0.3.3", features = ["native-cpu"] }
@@ -80,7 +80,8 @@ env_logger = "0.11"
8080
nanorand = { version = "0.7", default-features = false, features = ["std", "wyrand", "tls"] }
8181
atoi = "2.0"
8282
futures = "0.3"
83-
sonic-rs = "0.4.0"
83+
sonic-rs = "0.5.1"
84+
smallvec = "1.1"
8485
serde = { version = "1", features = ["derive"] }
8586
serde_json = "1"
8687
log = { version = "0.4", features = ["release_max_level_off"] }

frameworks/Rust/ntex/src/db.rs

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
#![allow(clippy::uninit_vec)]
2-
use std::{borrow::Cow, cell::RefCell, fmt::Write as FmtWrite};
1+
use std::{borrow::Cow, cell::RefCell};
32

43
use nanorand::{Rng, WyRand};
54
use ntex::util::{Bytes, BytesMut};
6-
use tokio_postgres::types::ToSql;
75
use tokio_postgres::{connect, Client, Statement};
86
use yarte::TemplateBytesTrait;
7+
use smallvec::SmallVec;
98

109
use super::utils;
1110

@@ -33,7 +32,7 @@ pub struct PgConnection {
3332
fortune: Statement,
3433
world: Statement,
3534
rng: WyRand,
36-
updates: Vec<Statement>,
35+
updates: Statement,
3736
buf: RefCell<BytesMut>,
3837
fbuf: RefCell<Vec<Fortune>>,
3938
}
@@ -48,24 +47,7 @@ impl PgConnection {
4847
});
4948

5049
let fortune = cl.prepare("SELECT * FROM fortune").await.unwrap();
51-
let mut updates = Vec::new();
52-
for num in 1..=500u16 {
53-
let mut pl: u16 = 1;
54-
let mut q = String::new();
55-
q.push_str("UPDATE world SET randomnumber = CASE id ");
56-
for _ in 1..=num {
57-
let _ = write!(&mut q, "when ${} then ${} ", pl, pl + 1);
58-
pl += 2;
59-
}
60-
q.push_str("ELSE randomnumber END WHERE id IN (");
61-
for _ in 1..=num {
62-
let _ = write!(&mut q, "${},", pl);
63-
pl += 1;
64-
}
65-
q.pop();
66-
q.push(')');
67-
updates.push(cl.prepare(&q).await.unwrap());
68-
}
50+
let updates = cl.prepare("UPDATE world w SET randomnumber = u.new_val FROM (SELECT unnest($1::int[]) as id, unnest($2::int[]) as new_val) u WHERE w.id = u.id").await.unwrap();
6951
let world = cl.prepare("SELECT * FROM world WHERE id=$1").await.unwrap();
7052

7153
PgConnection {
@@ -124,31 +106,30 @@ impl PgConnection {
124106

125107
pub async fn update(&self, num: usize) -> Bytes {
126108
let mut rng = nanorand::tls_rng();
127-
let mut queries = Vec::with_capacity(num);
109+
let mut ids = Vec::with_capacity(num);
110+
let mut numbers = Vec::with_capacity(num);
111+
let mut queries = SmallVec::<[_; 32]>::new();
112+
128113
(0..num).for_each(|_| {
129114
let w_id = (rng.generate::<u32>() % 10_000 + 1) as i32;
115+
ids.push(w_id);
116+
numbers.push((rng.generate::<u32>() % 10_000 + 1) as i32);
130117
queries.push(self.cl.query_one(&self.world, &[&w_id]));
131118
});
119+
ids.sort();
132120

133-
let mut worlds = Vec::with_capacity(num);
134121
for fut in queries.into_iter() {
135-
let row = fut.await.unwrap();
122+
fut.await.unwrap();
123+
}
124+
125+
let mut worlds = Vec::with_capacity(num);
126+
for row in self.cl.query(&self.updates, &[&ids, &numbers]).await.unwrap() {
136127
worlds.push(World {
137128
id: row.get(0),
138-
randomnumber: (rng.generate::<u32>() % 10_000 + 1) as i32,
129+
randomnumber: row.get(1)
139130
});
140131
}
141132

142-
let mut params: Vec<&dyn ToSql> = Vec::with_capacity(num * 3);
143-
for w in &worlds {
144-
params.push(&w.id);
145-
params.push(&w.randomnumber);
146-
}
147-
for w in &worlds {
148-
params.push(&w.id);
149-
}
150-
let _ = self.cl.query(&self.updates[num - 1], &params).await;
151-
152133
let mut body = self.buf.borrow_mut();
153134
utils::reserve(&mut body, 2 * 1024);
154135
sonic_rs::to_writer(utils::BytesWriter(&mut body), &worlds[..]).unwrap();

0 commit comments

Comments
 (0)