Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions frameworks/Rust/ntex/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "ntex-bench"
name = "ntex-framework"
version = "3.0.0"
edition = "2018"

Expand Down Expand Up @@ -60,17 +60,15 @@ tokio = ["ntex/tokio"]
# compio runtime
compio = ["ntex/compio"]

# neon runtime
neon = ["ntex/neon"]
# neon polling runtime
neon = ["ntex/neon-polling"]

# neon runtime
# neon io-uring runtime
neon-uring = ["ntex/neon-uring"]

[dependencies]
ntex = "3.0.0-pre.5"
ntex-neon = "0.1.35"
ntex-net = "3.0.0"
ntex-bytes = { version = "1", features=["simd"] }
ntex = "3.0.0-pre.11"
ntex-bytes = { version = "1.2", features=["simd"] }
mimalloc = { version = "0.1.25", default-features = false }
snmalloc-rs = { version = "0.3.3", features = ["native-cpu"] }
yarte = { version = "0.15", features = ["bytes-buf", "json"] }
Expand Down
42 changes: 22 additions & 20 deletions frameworks/Rust/ntex/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::cell::RefCell;
use std::cell::Cell;

use nanorand::{Rng, WyRand};
use ntex::util::{Bytes, BytesMut};
use ntex::util::{Bytes, BytesVec};
use smallvec::SmallVec;
use tokio_postgres::{connect, Client, Statement};
use yarte::TemplateBytesTrait;
Expand Down Expand Up @@ -33,7 +33,7 @@ pub struct PgConnection {
world: Statement,
rng: WyRand,
updates: Statement,
buf: RefCell<BytesMut>,
buf: Cell<Option<BytesVec>>,
}

impl PgConnection {
Expand All @@ -55,7 +55,7 @@ impl PgConnection {
world,
updates,
rng: WyRand::new(),
buf: RefCell::new(BytesMut::with_capacity(10 * 1024 * 1024)),
buf: Cell::new(Some(BytesVec::with_capacity(10 * 1024 * 1024))),
}
}
}
Expand All @@ -66,17 +66,18 @@ 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, 1024);
let mut body = self.buf.take().unwrap();
sonic_rs::to_writer(
utils::BytesWriter(&mut body),
utils::BVecWriter::new(&mut body),
&World {
id: row.get(0),
randomnumber: row.get(1),
},
)
.unwrap();
body.split().freeze()
let result = body.take_bytes();
self.buf.set(Some(body));
result
}

pub async fn get_worlds(&self, num: usize) -> Bytes {
Expand All @@ -96,10 +97,11 @@ impl PgConnection {
})
}

let mut body = self.buf.borrow_mut();
utils::reserve(&mut body, 2 * 1024);
sonic_rs::to_writer(utils::BytesWriter(&mut body), &worlds[..]).unwrap();
body.split().freeze()
let mut body = self.buf.take().unwrap();
sonic_rs::to_writer(utils::BVecWriter::new(&mut body), &worlds[..]).unwrap();
let result = body.take_bytes();
self.buf.set(Some(body));
result
}

pub async fn update(&self, num: usize) -> Bytes {
Expand Down Expand Up @@ -131,10 +133,11 @@ impl PgConnection {

update.await.unwrap();

let mut body = self.buf.borrow_mut();
utils::reserve(&mut body, 2 * 1024);
sonic_rs::to_writer(utils::BytesWriter(&mut body), &worlds[..]).unwrap();
body.split().freeze()
let mut body = self.buf.take().unwrap();
sonic_rs::to_writer(utils::BVecWriter::new(&mut body), &worlds[..]).unwrap();
let result = body.take_bytes();
self.buf.set(Some(body));
result
}

pub async fn tell_fortune(&self) -> Bytes {
Expand All @@ -151,17 +154,16 @@ impl PgConnection {
}));
fortunes.sort_by(|it, next| it.message.cmp(&next.message));

let mut body = std::mem::replace(&mut *self.buf.borrow_mut(), BytesMut::new());
let mut body = self.buf.take().unwrap();
utils::reserve(&mut body, 4 * 1024);

FortunesTemplate {
fortunes: &fortunes,
}
.write_call(&mut body);
fortunes.clear();

let result = body.split().freeze();
let _ = std::mem::replace(&mut *self.buf.borrow_mut(), body);
let result = body.take_bytes();
self.buf.set(Some(body));
result
}
}
8 changes: 4 additions & 4 deletions frameworks/Rust/ntex/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

use ntex::http::header::{CONTENT_TYPE, SERVER};
use ntex::{http, util::BytesMut, web};
use ntex::{http, util::BytesVec, web};
use sonic_rs::Serialize;

mod utils;
Expand All @@ -14,16 +14,16 @@ pub struct Message {

#[web::get("/json")]
async fn json() -> web::HttpResponse {
let mut body = BytesMut::with_capacity(utils::SIZE);
let mut body = BytesVec::with_capacity(utils::SIZE);
sonic_rs::to_writer(
utils::BytesWriter(&mut body),
utils::BVecWriter(&mut body),
&Message {
message: "Hello, World!",
},
)
.unwrap();

let mut response = web::HttpResponse::with_body(http::StatusCode::OK, body.into());
let mut response = web::HttpResponse::with_body(http::StatusCode::OK, body.freeze().into());
response.headers_mut().insert(SERVER, utils::HDR_SERVER);
response
.headers_mut()
Expand Down
48 changes: 23 additions & 25 deletions frameworks/Rust/ntex/src/main_plt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

use std::{future::Future, io, pin::Pin, task::ready, task::Context, task::Poll};

use ntex::{fn_service, http::h1, io::Io, io::RecvError};
use ntex::{fn_service, http::h1, http::DateService, io::Io, io::RecvError};
use sonic_rs::Serialize;

mod utils;
Expand Down Expand Up @@ -35,32 +35,30 @@ impl Future for App {
match ready!(this.io.poll_recv(&this.codec, cx)) {
Ok((req, _)) => {
let _ = this.io.with_write_buf(|buf| {
buf.with_bytes_mut(|buf| {
utils::reserve(buf, 2 * 1024);
match req.path() {
"/json" => {
buf.extend_from_slice(JSON);
this.codec.set_date_header(buf);
utils::reserve(buf, 2 * 1024);
match req.path() {
"/json" => {
buf.extend_from_slice(JSON);
DateService.bset_date_header(buf);

sonic_rs::to_writer(
utils::BytesWriter(buf),
&Message {
message: "Hello, World!",
},
)
.unwrap();
}
"/plaintext" => {
buf.extend_from_slice(PLAIN);
this.codec.set_date_header(buf);
buf.extend_from_slice(BODY);
}
_ => {
buf.extend_from_slice(HTTPNFOUND);
buf.extend_from_slice(HDR_SERVER);
}
sonic_rs::to_writer(
utils::BVecWriter(buf),
&Message {
message: "Hello, World!",
},
)
.unwrap();
}
})
"/plaintext" => {
buf.extend_from_slice(PLAIN);
DateService.bset_date_header(buf);
buf.extend_from_slice(BODY);
}
_ => {
buf.extend_from_slice(HTTPNFOUND);
buf.extend_from_slice(HDR_SERVER);
}
}
});
}
Err(RecvError::WriteBackpressure) => {
Expand Down
18 changes: 13 additions & 5 deletions frameworks/Rust/ntex/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::{cmp, io, io::Write, mem::MaybeUninit, slice::from_raw_parts_mut};

use atoi::FromRadix10;
use ntex::http::{header::HeaderValue, HttpServiceConfig, KeepAlive};
use ntex::{io::IoConfig, time::Seconds, util::BufMut, util::Bytes, util::BytesMut, SharedCfg};
use ntex::util::{BufMut, Bytes, BytesVec};
use ntex::{io::IoConfig, time::Seconds, SharedCfg};
use sonic_rs::writer::WriteExt;

pub const HDR_SERVER: HeaderValue = HeaderValue::from_static("N");
Expand Down Expand Up @@ -57,16 +58,23 @@ pub fn get_query_param(query: Option<&str>) -> usize {
cmp::min(500, cmp::max(1, q) as usize)
}

pub fn reserve(buf: &mut BytesMut, lw: usize) {
pub fn reserve(buf: &mut BytesVec, lw: usize) {
let remaining = buf.remaining_mut();
if remaining < lw {
buf.reserve(HW);
}
}

pub struct BytesWriter<'a>(pub &'a mut BytesMut);
pub struct BVecWriter<'a>(pub &'a mut BytesVec);

impl Write for BytesWriter<'_> {
impl<'a> BVecWriter<'a> {
pub fn new(buf: &'a mut BytesVec) -> BVecWriter<'a> {
reserve(buf, 2048);
Self(buf)
}
}

impl Write for BVecWriter<'_> {
fn write(&mut self, src: &[u8]) -> Result<usize, io::Error> {
self.0.extend_from_slice(src);
Ok(src.len())
Expand All @@ -77,7 +85,7 @@ impl Write for BytesWriter<'_> {
}
}

impl WriteExt for BytesWriter<'_> {
impl WriteExt for BVecWriter<'_> {
#[inline(always)]
fn reserve_with(&mut self, additional: usize) -> Result<&mut [MaybeUninit<u8>], io::Error> {
self.0.reserve(additional);
Expand Down
Loading