Skip to content

Commit ccf42af

Browse files
committed
wip
1 parent 3c1e4dd commit ccf42af

File tree

5 files changed

+34
-77
lines changed

5 files changed

+34
-77
lines changed

frameworks/Rust/ntex/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ neon = ["ntex/neon-polling"]
6767
neon-uring = ["ntex/neon-uring"]
6868

6969
[dependencies]
70-
ntex = "3.0.0-pre.10"
70+
ntex = "3.0.0-pre.11"
7171
ntex-bytes = { version = "1.2", features=["simd"] }
7272
mimalloc = { version = "0.1.25", default-features = false }
7373
snmalloc-rs = { version = "0.3.3", features = ["native-cpu"] }

frameworks/Rust/ntex/src/db.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::cell::Cell;
22

33
use nanorand::{Rng, WyRand};
4-
use ntex::util::{Bytes, BytesVec, BufMut};
4+
use ntex::util::{Bytes, BytesVec};
55
use smallvec::SmallVec;
66
use tokio_postgres::{connect, Client, Statement};
77
use yarte::TemplateBytesTrait;
@@ -155,11 +155,7 @@ impl PgConnection {
155155
fortunes.sort_by(|it, next| it.message.cmp(&next.message));
156156

157157
let mut body = self.buf.take().unwrap();
158-
let remaining = body.remaining_mut();
159-
if remaining < 4 * 1024 {
160-
body.reserve(128 * 1024);
161-
}
162-
158+
utils::reserve(&mut body, 4 * 1024);
163159
FortunesTemplate {
164160
fortunes: &fortunes,
165161
}

frameworks/Rust/ntex/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
33

44
use ntex::http::header::{CONTENT_TYPE, SERVER};
5-
use ntex::{http, util::BytesMut, web};
5+
use ntex::{http, util::BytesVec, web};
66
use sonic_rs::Serialize;
77

88
mod utils;
@@ -14,16 +14,16 @@ pub struct Message {
1414

1515
#[web::get("/json")]
1616
async fn json() -> web::HttpResponse {
17-
let mut body = BytesMut::with_capacity(utils::SIZE);
17+
let mut body = BytesVec::with_capacity(utils::SIZE);
1818
sonic_rs::to_writer(
19-
utils::BytesWriter(&mut body),
19+
utils::BVecWriter(&mut body),
2020
&Message {
2121
message: "Hello, World!",
2222
},
2323
)
2424
.unwrap();
2525

26-
let mut response = web::HttpResponse::with_body(http::StatusCode::OK, body.into());
26+
let mut response = web::HttpResponse::with_body(http::StatusCode::OK, body.freeze().into());
2727
response.headers_mut().insert(SERVER, utils::HDR_SERVER);
2828
response
2929
.headers_mut()

frameworks/Rust/ntex/src/main_plt.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
33

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

6-
use ntex::{fn_service, http::h1, io::Io, io::RecvError};
6+
use ntex::{fn_service, http::h1, http::DateService, io::Io, io::RecvError};
77
use sonic_rs::Serialize;
88

99
mod utils;
@@ -35,32 +35,30 @@ impl Future for App {
3535
match ready!(this.io.poll_recv(&this.codec, cx)) {
3636
Ok((req, _)) => {
3737
let _ = this.io.with_write_buf(|buf| {
38-
buf.with_bytes_mut(|buf| {
39-
utils::reserve(buf, 2 * 1024);
40-
match req.path() {
41-
"/json" => {
42-
buf.extend_from_slice(JSON);
43-
this.codec.set_date_header(buf);
38+
utils::reserve(buf, 2 * 1024);
39+
match req.path() {
40+
"/json" => {
41+
buf.extend_from_slice(JSON);
42+
DateService.bset_date_header(buf);
4443

45-
sonic_rs::to_writer(
46-
utils::BytesWriter(buf),
47-
&Message {
48-
message: "Hello, World!",
49-
},
50-
)
51-
.unwrap();
52-
}
53-
"/plaintext" => {
54-
buf.extend_from_slice(PLAIN);
55-
this.codec.set_date_header(buf);
56-
buf.extend_from_slice(BODY);
57-
}
58-
_ => {
59-
buf.extend_from_slice(HTTPNFOUND);
60-
buf.extend_from_slice(HDR_SERVER);
61-
}
44+
sonic_rs::to_writer(
45+
utils::BVecWriter(buf),
46+
&Message {
47+
message: "Hello, World!",
48+
},
49+
)
50+
.unwrap();
6251
}
63-
})
52+
"/plaintext" => {
53+
buf.extend_from_slice(PLAIN);
54+
DateService.bset_date_header(buf);
55+
buf.extend_from_slice(BODY);
56+
}
57+
_ => {
58+
buf.extend_from_slice(HTTPNFOUND);
59+
buf.extend_from_slice(HDR_SERVER);
60+
}
61+
}
6462
});
6563
}
6664
Err(RecvError::WriteBackpressure) => {

frameworks/Rust/ntex/src/utils.rs

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{cmp, io, io::Write, mem::MaybeUninit, slice::from_raw_parts_mut};
33

44
use atoi::FromRadix10;
55
use ntex::http::{header::HeaderValue, HttpServiceConfig, KeepAlive};
6-
use ntex::util::{BufMut, Bytes, BytesMut, BytesVec};
6+
use ntex::util::{BufMut, Bytes, BytesVec};
77
use ntex::{io::IoConfig, time::Seconds, SharedCfg};
88
use sonic_rs::writer::WriteExt;
99

@@ -58,55 +58,18 @@ pub fn get_query_param(query: Option<&str>) -> usize {
5858
cmp::min(500, cmp::max(1, q) as usize)
5959
}
6060

61-
pub fn reserve(buf: &mut BytesMut, lw: usize) {
61+
pub fn reserve(buf: &mut BytesVec, lw: usize) {
6262
let remaining = buf.remaining_mut();
6363
if remaining < lw {
6464
buf.reserve(HW);
6565
}
6666
}
6767

68-
pub struct BytesWriter<'a>(pub &'a mut BytesMut);
69-
70-
impl Write for BytesWriter<'_> {
71-
fn write(&mut self, src: &[u8]) -> Result<usize, io::Error> {
72-
self.0.extend_from_slice(src);
73-
Ok(src.len())
74-
}
75-
76-
fn flush(&mut self) -> Result<(), io::Error> {
77-
Ok(())
78-
}
79-
}
80-
81-
impl WriteExt for BytesWriter<'_> {
82-
#[inline(always)]
83-
fn reserve_with(&mut self, additional: usize) -> Result<&mut [MaybeUninit<u8>], io::Error> {
84-
self.0.reserve(additional);
85-
86-
unsafe {
87-
let ptr = self.0.as_mut_ptr().add(self.0.len()) as *mut MaybeUninit<u8>;
88-
Ok(from_raw_parts_mut(ptr, additional))
89-
}
90-
}
91-
92-
#[inline(always)]
93-
unsafe fn flush_len(&mut self, additional: usize) -> io::Result<()> {
94-
unsafe {
95-
let new_len = self.0.len() + additional;
96-
self.0.set_len(new_len);
97-
}
98-
Ok(())
99-
}
100-
}
101-
102-
pub struct BVecWriter<'a>(&'a mut BytesVec);
68+
pub struct BVecWriter<'a>(pub &'a mut BytesVec);
10369

10470
impl<'a> BVecWriter<'a> {
10571
pub fn new(buf: &'a mut BytesVec) -> BVecWriter<'a> {
106-
let remaining = buf.remaining_mut();
107-
if remaining < 2048 {
108-
buf.reserve(HW);
109-
}
72+
reserve(buf, 2048);
11073
Self(buf)
11174
}
11275
}

0 commit comments

Comments
 (0)