Browse Source

ntex: update version, try different allocator (#9206)

Nikolay Kim 1 year ago
parent
commit
919e13b01e

+ 1 - 1
frameworks/Rust/ntex/Cargo.toml

@@ -37,7 +37,7 @@ tokio = ["ntex/tokio"]
 async-std = ["ntex/async-std"]
 
 [dependencies]
-ntex = "=2.0.3"
+ntex = "=2.1.0"
 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"] }

+ 29 - 30
frameworks/Rust/ntex/src/db.rs

@@ -1,11 +1,11 @@
-use std::{cell::RefCell, fmt::Write as FmtWrite};
+#![allow(clippy::uninit_vec)]
+use std::{borrow::Cow, cell::RefCell, fmt::Write as FmtWrite};
 
-use futures::stream::{futures_unordered::FuturesUnordered, StreamExt};
 use nanorand::{Rng, WyRand};
 use ntex::util::{BufMut, Bytes, BytesMut};
 use smallvec::SmallVec;
 use tokio_postgres::types::ToSql;
-use tokio_postgres::{connect, Client, Row, Statement};
+use tokio_postgres::{connect, Client, Statement};
 use yarte::{ywrite_html, Serialize};
 
 use super::utils;
@@ -17,9 +17,9 @@ pub struct World {
 }
 
 #[derive(Serialize, Debug)]
-pub struct Fortune<'a> {
+pub struct Fortune {
     pub id: i32,
-    pub message: &'a str,
+    pub message: Cow<'static, str>,
 }
 
 /// Postgres interface
@@ -60,10 +60,7 @@ impl PgConnection {
             q.push(')');
             updates.push(cl.prepare(&q).await.unwrap());
         }
-        let world = cl
-            .prepare("SELECT id, randomnumber FROM world WHERE id=$1")
-            .await
-            .unwrap();
+        let world = cl.prepare("SELECT * FROM world WHERE id=$1").await.unwrap();
 
         PgConnection {
             cl,
@@ -83,7 +80,7 @@ impl PgConnection {
         let row = self.cl.query_one(&self.world, &[&random_id]).await.unwrap();
 
         let mut body = self.buf.borrow_mut();
-        utils::reserve(&mut body, 256);
+        utils::reserve(&mut body, 8 * 1024);
         World {
             id: row.get(0),
             randomnumber: row.get(1),
@@ -92,20 +89,17 @@ impl PgConnection {
         body.split().freeze()
     }
 
-    async fn get_one_world(&self, id: i32) -> Row {
-        self.cl.query_one(&self.world, &[&id]).await.unwrap()
-    }
-
     pub async fn get_worlds(&self, num: usize) -> Bytes {
         let mut rng = self.rng.clone();
-        let mut queries = FuturesUnordered::new();
+        let mut queries = SmallVec::<[_; 32]>::new();
         (0..num).for_each(|_| {
             let w_id = (rng.generate::<u32>() % 10_000 + 1) as i32;
-            queries.push(self.get_one_world(w_id))
+            queries.push(self.cl.query_one(&self.world, &[&w_id]));
         });
 
         let mut worlds = SmallVec::<[_; 32]>::new();
-        while let Some(row) = queries.next().await {
+        for fut in queries {
+            let row = fut.await.unwrap();
             worlds.push(World {
                 id: row.get(0),
                 randomnumber: row.get(1),
@@ -126,14 +120,15 @@ impl PgConnection {
 
     pub async fn update(&self, num: usize) -> Bytes {
         let mut rng = self.rng.clone();
-        let mut queries = FuturesUnordered::new();
+        let mut queries = SmallVec::<[_; 32]>::new();
         (0..num).for_each(|_| {
             let w_id = (rng.generate::<u32>() % 10_000 + 1) as i32;
-            queries.push(self.get_one_world(w_id))
+            queries.push(self.cl.query_one(&self.world, &[&w_id]));
         });
 
         let mut worlds = SmallVec::<[_; 32]>::new();
-        while let Some(row) = queries.next().await {
+        for fut in queries.into_iter() {
+            let row = fut.await.unwrap();
             worlds.push(World {
                 id: row.get(0),
                 randomnumber: (rng.generate::<u32>() % 10_000 + 1) as i32,
@@ -163,18 +158,22 @@ impl PgConnection {
     }
 
     pub async fn tell_fortune(&self) -> Bytes {
-        let rows = self.cl.query_raw(&self.fortune, &[]).await.unwrap();
+        let fut = self.cl.query_raw(&self.fortune, &[]);
 
-        let mut fortunes = Vec::with_capacity(rows.len() + 1);
-        fortunes.push(Fortune {
+        let rows = fut.await.unwrap();
+        let mut fortunes: SmallVec<[_; 32]> = smallvec::smallvec![Fortune {
             id: 0,
-            message: "Additional fortune added at request time.",
-        });
-        fortunes.extend(rows.iter().map(|row| Fortune {
-            id: row.get(0),
-            message: row.get(1),
-        }));
-        fortunes.sort_by(|it, next| it.message.cmp(next.message));
+            message: Cow::Borrowed("Additional fortune added at request time."),
+        }];
+
+        for row in rows {
+            fortunes.push(Fortune {
+                id: row.get(0),
+                message: Cow::Owned(row.get(1)),
+            });
+        }
+
+        fortunes.sort_by(|it, next| it.message.cmp(&next.message));
 
         let mut body = std::mem::replace(&mut *self.buf.borrow_mut(), BytesMut::new());
         utils::reserve(&mut body, 8 * 1024);

+ 2 - 1
frameworks/Rust/ntex/src/main.rs

@@ -1,5 +1,6 @@
 #[global_allocator]
-static GLOBAL: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
+static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
+// static GLOBAL: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
 
 use ntex::http::header::{CONTENT_TYPE, SERVER};
 use ntex::{http, time::Seconds, util::BytesMut, util::PoolId, web};

+ 2 - 2
frameworks/Rust/ntex/src/main_db.rs

@@ -1,7 +1,7 @@
 #[cfg(not(target_os = "macos"))]
 #[global_allocator]
-static GLOBAL: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
-// static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
+static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
+// static GLOBAL: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
 
 use ntex::http::header::{CONTENT_TYPE, SERVER};
 use ntex::http::{HttpService, KeepAlive, Request, Response, StatusCode};

+ 1 - 0
frameworks/Rust/ntex/src/main_plt.rs

@@ -1,5 +1,6 @@
 #[global_allocator]
 static GLOBAL: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
+
 use std::{future::Future, io, pin::Pin, task::Context, task::Poll};
 
 use ntex::{fn_service, http::h1, io::Io, io::RecvError, util::ready, util::PoolId};