Skip to content

Commit 919e13b

Browse files
authored
ntex: update version, try different allocator (#9206)
1 parent a77b6eb commit 919e13b

File tree

5 files changed

+35
-34
lines changed

5 files changed

+35
-34
lines changed

frameworks/Rust/ntex/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ tokio = ["ntex/tokio"]
3737
async-std = ["ntex/async-std"]
3838

3939
[dependencies]
40-
ntex = "=2.0.3"
40+
ntex = "=2.1.0"
4141
ntex-bytes = { version = "0.1.21", features=["simd"] }
4242
mimalloc = { version = "0.1.25", default-features = false }
4343
snmalloc-rs = { version = "0.3.3", features = ["native-cpu"] }

frameworks/Rust/ntex/src/db.rs

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

3-
use futures::stream::{futures_unordered::FuturesUnordered, StreamExt};
44
use nanorand::{Rng, WyRand};
55
use ntex::util::{BufMut, Bytes, BytesMut};
66
use smallvec::SmallVec;
77
use tokio_postgres::types::ToSql;
8-
use tokio_postgres::{connect, Client, Row, Statement};
8+
use tokio_postgres::{connect, Client, Statement};
99
use yarte::{ywrite_html, Serialize};
1010

1111
use super::utils;
@@ -17,9 +17,9 @@ pub struct World {
1717
}
1818

1919
#[derive(Serialize, Debug)]
20-
pub struct Fortune<'a> {
20+
pub struct Fortune {
2121
pub id: i32,
22-
pub message: &'a str,
22+
pub message: Cow<'static, str>,
2323
}
2424

2525
/// Postgres interface
@@ -60,10 +60,7 @@ impl PgConnection {
6060
q.push(')');
6161
updates.push(cl.prepare(&q).await.unwrap());
6262
}
63-
let world = cl
64-
.prepare("SELECT id, randomnumber FROM world WHERE id=$1")
65-
.await
66-
.unwrap();
63+
let world = cl.prepare("SELECT * FROM world WHERE id=$1").await.unwrap();
6764

6865
PgConnection {
6966
cl,
@@ -83,7 +80,7 @@ impl PgConnection {
8380
let row = self.cl.query_one(&self.world, &[&random_id]).await.unwrap();
8481

8582
let mut body = self.buf.borrow_mut();
86-
utils::reserve(&mut body, 256);
83+
utils::reserve(&mut body, 8 * 1024);
8784
World {
8885
id: row.get(0),
8986
randomnumber: row.get(1),
@@ -92,20 +89,17 @@ impl PgConnection {
9289
body.split().freeze()
9390
}
9491

95-
async fn get_one_world(&self, id: i32) -> Row {
96-
self.cl.query_one(&self.world, &[&id]).await.unwrap()
97-
}
98-
9992
pub async fn get_worlds(&self, num: usize) -> Bytes {
10093
let mut rng = self.rng.clone();
101-
let mut queries = FuturesUnordered::new();
94+
let mut queries = SmallVec::<[_; 32]>::new();
10295
(0..num).for_each(|_| {
10396
let w_id = (rng.generate::<u32>() % 10_000 + 1) as i32;
104-
queries.push(self.get_one_world(w_id))
97+
queries.push(self.cl.query_one(&self.world, &[&w_id]));
10598
});
10699

107100
let mut worlds = SmallVec::<[_; 32]>::new();
108-
while let Some(row) = queries.next().await {
101+
for fut in queries {
102+
let row = fut.await.unwrap();
109103
worlds.push(World {
110104
id: row.get(0),
111105
randomnumber: row.get(1),
@@ -126,14 +120,15 @@ impl PgConnection {
126120

127121
pub async fn update(&self, num: usize) -> Bytes {
128122
let mut rng = self.rng.clone();
129-
let mut queries = FuturesUnordered::new();
123+
let mut queries = SmallVec::<[_; 32]>::new();
130124
(0..num).for_each(|_| {
131125
let w_id = (rng.generate::<u32>() % 10_000 + 1) as i32;
132-
queries.push(self.get_one_world(w_id))
126+
queries.push(self.cl.query_one(&self.world, &[&w_id]));
133127
});
134128

135129
let mut worlds = SmallVec::<[_; 32]>::new();
136-
while let Some(row) = queries.next().await {
130+
for fut in queries.into_iter() {
131+
let row = fut.await.unwrap();
137132
worlds.push(World {
138133
id: row.get(0),
139134
randomnumber: (rng.generate::<u32>() % 10_000 + 1) as i32,
@@ -163,18 +158,22 @@ impl PgConnection {
163158
}
164159

165160
pub async fn tell_fortune(&self) -> Bytes {
166-
let rows = self.cl.query_raw(&self.fortune, &[]).await.unwrap();
161+
let fut = self.cl.query_raw(&self.fortune, &[]);
167162

168-
let mut fortunes = Vec::with_capacity(rows.len() + 1);
169-
fortunes.push(Fortune {
163+
let rows = fut.await.unwrap();
164+
let mut fortunes: SmallVec<[_; 32]> = smallvec::smallvec![Fortune {
170165
id: 0,
171-
message: "Additional fortune added at request time.",
172-
});
173-
fortunes.extend(rows.iter().map(|row| Fortune {
174-
id: row.get(0),
175-
message: row.get(1),
176-
}));
177-
fortunes.sort_by(|it, next| it.message.cmp(next.message));
166+
message: Cow::Borrowed("Additional fortune added at request time."),
167+
}];
168+
169+
for row in rows {
170+
fortunes.push(Fortune {
171+
id: row.get(0),
172+
message: Cow::Owned(row.get(1)),
173+
});
174+
}
175+
176+
fortunes.sort_by(|it, next| it.message.cmp(&next.message));
178177

179178
let mut body = std::mem::replace(&mut *self.buf.borrow_mut(), BytesMut::new());
180179
utils::reserve(&mut body, 8 * 1024);

frameworks/Rust/ntex/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#[global_allocator]
2-
static GLOBAL: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
2+
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
3+
// static GLOBAL: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
34

45
use ntex::http::header::{CONTENT_TYPE, SERVER};
56
use ntex::{http, time::Seconds, util::BytesMut, util::PoolId, web};

frameworks/Rust/ntex/src/main_db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[cfg(not(target_os = "macos"))]
22
#[global_allocator]
3-
static GLOBAL: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
4-
// static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
3+
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
4+
// static GLOBAL: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
55

66
use ntex::http::header::{CONTENT_TYPE, SERVER};
77
use ntex::http::{HttpService, KeepAlive, Request, Response, StatusCode};

frameworks/Rust/ntex/src/main_plt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#[global_allocator]
22
static GLOBAL: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
3+
34
use std::{future::Future, io, pin::Pin, task::Context, task::Poll};
45

56
use ntex::{fn_service, http::h1, io::Io, io::RecvError, util::ready, util::PoolId};

0 commit comments

Comments
 (0)