From 9f6e9f1dd60b3a15294134902ef5411e77a14121 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 20 Jun 2025 17:02:37 +0500 Subject: [PATCH 1/6] ntex: Update bench tweaks --- frameworks/Rust/ntex/Cargo.toml | 11 +++---- frameworks/Rust/ntex/src/db.rs | 53 +++++++++++---------------------- 2 files changed, 23 insertions(+), 41 deletions(-) diff --git a/frameworks/Rust/ntex/Cargo.toml b/frameworks/Rust/ntex/Cargo.toml index e4b476f46cd..4974a8d30c1 100755 --- a/frameworks/Rust/ntex/Cargo.toml +++ b/frameworks/Rust/ntex/Cargo.toml @@ -67,10 +67,10 @@ neon = ["ntex/neon"] neon-uring = ["ntex/neon-uring"] [dependencies] -ntex = "2.12" -ntex-compio = "0.2" -ntex-neon = "0.1.13" -ntex-net = "2.5.9" +ntex = "2.13" +ntex-compio = "0.3" +ntex-neon = "0.1.31" +ntex-net = "2.5.27" ntex-bytes = { version = "0.1.21", features=["simd"] } mimalloc = { version = "0.1.25", default-features = false } snmalloc-rs = { version = "0.3.3", features = ["native-cpu"] } @@ -80,7 +80,8 @@ env_logger = "0.11" nanorand = { version = "0.7", default-features = false, features = ["std", "wyrand", "tls"] } atoi = "2.0" futures = "0.3" -sonic-rs = "0.4.0" +sonic-rs = "0.5.1" +smallvec = "1.1" serde = { version = "1", features = ["derive"] } serde_json = "1" log = { version = "0.4", features = ["release_max_level_off"] } diff --git a/frameworks/Rust/ntex/src/db.rs b/frameworks/Rust/ntex/src/db.rs index da33f1f6c35..927bbe43f49 100644 --- a/frameworks/Rust/ntex/src/db.rs +++ b/frameworks/Rust/ntex/src/db.rs @@ -1,11 +1,10 @@ -#![allow(clippy::uninit_vec)] -use std::{borrow::Cow, cell::RefCell, fmt::Write as FmtWrite}; +use std::{borrow::Cow, cell::RefCell}; use nanorand::{Rng, WyRand}; use ntex::util::{Bytes, BytesMut}; -use tokio_postgres::types::ToSql; use tokio_postgres::{connect, Client, Statement}; use yarte::TemplateBytesTrait; +use smallvec::SmallVec; use super::utils; @@ -33,7 +32,7 @@ pub struct PgConnection { fortune: Statement, world: Statement, rng: WyRand, - updates: Vec, + updates: Statement, buf: RefCell, fbuf: RefCell>, } @@ -48,24 +47,7 @@ impl PgConnection { }); let fortune = cl.prepare("SELECT * FROM fortune").await.unwrap(); - let mut updates = Vec::new(); - for num in 1..=500u16 { - let mut pl: u16 = 1; - let mut q = String::new(); - q.push_str("UPDATE world SET randomnumber = CASE id "); - for _ in 1..=num { - let _ = write!(&mut q, "when ${} then ${} ", pl, pl + 1); - pl += 2; - } - q.push_str("ELSE randomnumber END WHERE id IN ("); - for _ in 1..=num { - let _ = write!(&mut q, "${},", pl); - pl += 1; - } - q.pop(); - q.push(')'); - updates.push(cl.prepare(&q).await.unwrap()); - } + 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(); let world = cl.prepare("SELECT * FROM world WHERE id=$1").await.unwrap(); PgConnection { @@ -124,31 +106,30 @@ impl PgConnection { pub async fn update(&self, num: usize) -> Bytes { let mut rng = nanorand::tls_rng(); - let mut queries = Vec::with_capacity(num); + let mut ids = Vec::with_capacity(num); + let mut numbers = Vec::with_capacity(num); + let mut queries = SmallVec::<[_; 32]>::new(); + (0..num).for_each(|_| { let w_id = (rng.generate::() % 10_000 + 1) as i32; + ids.push(w_id); + numbers.push((rng.generate::() % 10_000 + 1) as i32); queries.push(self.cl.query_one(&self.world, &[&w_id])); }); + ids.sort(); - let mut worlds = Vec::with_capacity(num); for fut in queries.into_iter() { - let row = fut.await.unwrap(); + fut.await.unwrap(); + } + + let mut worlds = Vec::with_capacity(num); + for row in self.cl.query(&self.updates, &[&ids, &numbers]).await.unwrap() { worlds.push(World { id: row.get(0), - randomnumber: (rng.generate::() % 10_000 + 1) as i32, + randomnumber: row.get(1) }); } - let mut params: Vec<&dyn ToSql> = Vec::with_capacity(num * 3); - for w in &worlds { - params.push(&w.id); - params.push(&w.randomnumber); - } - for w in &worlds { - params.push(&w.id); - } - let _ = self.cl.query(&self.updates[num - 1], ¶ms).await; - let mut body = self.buf.borrow_mut(); utils::reserve(&mut body, 2 * 1024); sonic_rs::to_writer(utils::BytesWriter(&mut body), &worlds[..]).unwrap(); From 7c26ff5389e02bf2da2b17b848c04394e6ead7a4 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 20 Jun 2025 17:36:41 +0500 Subject: [PATCH 2/6] fix --- frameworks/Rust/ntex/src/db.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/frameworks/Rust/ntex/src/db.rs b/frameworks/Rust/ntex/src/db.rs index 927bbe43f49..131511132fa 100644 --- a/frameworks/Rust/ntex/src/db.rs +++ b/frameworks/Rust/ntex/src/db.rs @@ -2,9 +2,9 @@ use std::{borrow::Cow, cell::RefCell}; use nanorand::{Rng, WyRand}; use ntex::util::{Bytes, BytesMut}; +use smallvec::SmallVec; use tokio_postgres::{connect, Client, Statement}; use yarte::TemplateBytesTrait; -use smallvec::SmallVec; use super::utils; @@ -108,27 +108,30 @@ impl PgConnection { let mut rng = nanorand::tls_rng(); let mut ids = Vec::with_capacity(num); let mut numbers = Vec::with_capacity(num); - let mut queries = SmallVec::<[_; 32]>::new(); + let mut worlds = SmallVec::<[_; 32]>::new(); (0..num).for_each(|_| { let w_id = (rng.generate::() % 10_000 + 1) as i32; ids.push(w_id); numbers.push((rng.generate::() % 10_000 + 1) as i32); - queries.push(self.cl.query_one(&self.world, &[&w_id])); }); ids.sort(); - for fut in queries.into_iter() { - fut.await.unwrap(); - } - - let mut worlds = Vec::with_capacity(num); - for row in self.cl.query(&self.updates, &[&ids, &numbers]).await.unwrap() { - worlds.push(World { - id: row.get(0), - randomnumber: row.get(1) - }); - } + let _ = (0..num) + .map(|idx| { + worlds.push(World { + id: ids[idx], + randomnumber: numbers[idx], + }); + self.cl.query_one(&self.world, &[&ids[idx]]) + }) + .last() + .unwrap() + .await; + self.cl + .query(&self.updates, &[&ids, &numbers]) + .await + .unwrap(); let mut body = self.buf.borrow_mut(); utils::reserve(&mut body, 2 * 1024); From 53b19f10dd99f70995f69e54120b4a1bedf6e6e1 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 20 Jun 2025 17:50:41 +0500 Subject: [PATCH 3/6] wip --- frameworks/Rust/ntex/src/db.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/frameworks/Rust/ntex/src/db.rs b/frameworks/Rust/ntex/src/db.rs index 131511132fa..68f1309ea45 100644 --- a/frameworks/Rust/ntex/src/db.rs +++ b/frameworks/Rust/ntex/src/db.rs @@ -117,18 +117,15 @@ impl PgConnection { }); ids.sort(); - let _ = (0..num) - .map(|idx| { - worlds.push(World { - id: ids[idx], - randomnumber: numbers[idx], - }); - self.cl.query_one(&self.world, &[&ids[idx]]) - }) - .last() - .unwrap() - .await; - self.cl + (0..num).map(|idx| { + worlds.push(World { + id: ids[idx], + randomnumber: numbers[idx], + }); + self.cl.query_one(&self.world, &[&ids[idx]]) + }); + let _ = self + .cl .query(&self.updates, &[&ids, &numbers]) .await .unwrap(); From 67f3839dbc218bf54cdd78ac4db322e7ffde66f8 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 20 Jun 2025 17:55:22 +0500 Subject: [PATCH 4/6] wip --- frameworks/Rust/ntex/src/db.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/Rust/ntex/src/db.rs b/frameworks/Rust/ntex/src/db.rs index 68f1309ea45..d93af25dad1 100644 --- a/frameworks/Rust/ntex/src/db.rs +++ b/frameworks/Rust/ntex/src/db.rs @@ -117,12 +117,12 @@ impl PgConnection { }); ids.sort(); - (0..num).map(|idx| { + (0..num).for_each(|idx| { worlds.push(World { id: ids[idx], randomnumber: numbers[idx], }); - self.cl.query_one(&self.world, &[&ids[idx]]) + let _ = self.cl.query_one(&self.world, &[&ids[idx]]); }); let _ = self .cl From d1961abdf455ecbd5754f57d354837af8b1832d3 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 20 Jun 2025 18:00:55 +0500 Subject: [PATCH 5/6] wip --- frameworks/Rust/ntex/src/db.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frameworks/Rust/ntex/src/db.rs b/frameworks/Rust/ntex/src/db.rs index d93af25dad1..a85b137139c 100644 --- a/frameworks/Rust/ntex/src/db.rs +++ b/frameworks/Rust/ntex/src/db.rs @@ -109,6 +109,7 @@ impl PgConnection { let mut ids = Vec::with_capacity(num); let mut numbers = Vec::with_capacity(num); let mut worlds = SmallVec::<[_; 32]>::new(); + let mut queries = SmallVec::<[_; 32]>::new(); (0..num).for_each(|_| { let w_id = (rng.generate::() % 10_000 + 1) as i32; @@ -122,7 +123,7 @@ impl PgConnection { id: ids[idx], randomnumber: numbers[idx], }); - let _ = self.cl.query_one(&self.world, &[&ids[idx]]); + queries.push(self.cl.query_one(&self.world, &[&ids[idx]])); }); let _ = self .cl From ce6b6cd87991b21335b335fab162513969281f28 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 20 Jun 2025 18:14:55 +0500 Subject: [PATCH 6/6] wip --- frameworks/Rust/ntex/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/Rust/ntex/Cargo.toml b/frameworks/Rust/ntex/Cargo.toml index 4974a8d30c1..a4baf02318f 100755 --- a/frameworks/Rust/ntex/Cargo.toml +++ b/frameworks/Rust/ntex/Cargo.toml @@ -81,7 +81,7 @@ nanorand = { version = "0.7", default-features = false, features = ["std", "wyra atoi = "2.0" futures = "0.3" sonic-rs = "0.5.1" -smallvec = "1.1" +smallvec = "1.15" serde = { version = "1", features = ["derive"] } serde_json = "1" log = { version = "0.4", features = ["release_max_level_off"] }