Skip to content

Commit 85f40bb

Browse files
committed
[xitca-web] clean up and optimization
1 parent 954da00 commit 85f40bb

File tree

10 files changed

+124
-149
lines changed

10 files changed

+124
-149
lines changed

frameworks/Rust/xitca-web/Cargo.lock

Lines changed: 22 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frameworks/Rust/xitca-web/Cargo.toml

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ edition = "2024"
66
[[bin]]
77
name = "xitca-web"
88
path = "./src/main.rs"
9-
required-features = ["io-uring", "pg", "router", "template"]
9+
required-features = ["io-uring", "pg", "router", "template", "zero-copy"]
1010

1111
[[bin]]
1212
name = "xitca-web-barebone"
1313
path = "./src/main_barebone.rs"
14-
required-features = ["perf", "perf-json", "pg", "template"]
14+
required-features = ["perf", "perf-json", "pg", "template", "zero-copy"]
1515

1616
[[bin]]
1717
name = "xitca-web-diesel"
@@ -38,6 +38,8 @@ web-codegen = ["xitca-web/codegen", "xitca-web/urlencoded"]
3838
template = ["dep:sailfish"]
3939
# io-uring optional
4040
io-uring = ["dep:tokio-uring", "xitca-http/io-uring", "xitca-server/io-uring"]
41+
# zero-copy database row parsing optional(not supported by ORMs)
42+
zero-copy = []
4143
# unrealistic performance optimization
4244
perf = ["perf-allocator", "dep:core_affinity", "tokio/parking_lot"]
4345
perf-allocator = ["dep:mimalloc"]
@@ -70,7 +72,7 @@ xitca-postgres-diesel = { version = "0.2", default-features = false, optional =
7072
futures-util = { version = "0.3", default-features = false, optional = true }
7173

7274
# toasty orm optional
73-
xitca-postgres-toasty = { version = "0.1", optional = true }
75+
xitca-postgres-toasty = { version = "0.1", features = ["nightly"], optional = true }
7476
toasty = { version = "0.1", optional = true }
7577

7678
# template optional
@@ -99,18 +101,18 @@ panic = "abort"
99101

100102
[patch.crates-io]
101103
xitca-postgres-diesel = { git = "https://github.com/fakeshadow/xitca-postgres-diesel", rev = "b6d1922" }
102-
xitca-postgres-toasty = { git = "https://github.com/fakeshadow/xitca-postgres-toasty", rev = "6034a22" }
104+
xitca-postgres-toasty = { git = "https://github.com/fakeshadow/xitca-postgres-toasty", rev = "6e91d2f" }
103105

104-
toasty = { git = "https://github.com/fakeshadow/toasty", branch = "engine" }
105-
toasty-core = { git = "https://github.com/fakeshadow/toasty", branch = "engine" }
106-
toasty-sql = { git = "https://github.com/fakeshadow/toasty", branch = "engine" }
106+
toasty = { git = "https://github.com/fakeshadow/toasty", rev = "51c8186" }
107+
toasty-core = { git = "https://github.com/fakeshadow/toasty", rev = "51c8186" }
108+
toasty-sql = { git = "https://github.com/fakeshadow/toasty", rev = "51c8186" }
107109

108110
# personal fork of tokio-uring with tokio local runtime enabled
109111
tokio-uring = { git = "http://github.com/fakeshadow/tokio-uring", rev = "c3d5887" }
110112

111-
xitca-codegen = { git = "http://github.com/HFQR/xitca-web", rev = "3f38156" }
112-
xitca-http = { git = "http://github.com/HFQR/xitca-web", rev = "3f38156" }
113-
xitca-postgres = { git = "http://github.com/HFQR/xitca-web", rev = "3f38156" }
114-
xitca-server = { git = "http://github.com/HFQR/xitca-web", rev = "3f38156" }
115-
xitca-service = { git = "http://github.com/HFQR/xitca-web", rev = "3f38156" }
116-
xitca-web = { git = "http://github.com/HFQR/xitca-web", rev = "3f38156" }
113+
xitca-codegen = { git = "http://github.com/HFQR/xitca-web", rev = "2fb7b62" }
114+
xitca-http = { git = "http://github.com/HFQR/xitca-web", rev = "2fb7b62" }
115+
xitca-postgres = { git = "http://github.com/HFQR/xitca-web", rev = "2fb7b62" }
116+
xitca-server = { git = "http://github.com/HFQR/xitca-web", rev = "2fb7b62" }
117+
xitca-service = { git = "http://github.com/HFQR/xitca-web", rev = "2fb7b62" }
118+
xitca-web = { git = "http://github.com/HFQR/xitca-web", rev = "2fb7b62" }

frameworks/Rust/xitca-web/src/main.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use xitca_http::{
1414
bytes::Bytes,
1515
h1::RequestBody,
1616
http::{
17-
self, IntoResponse as _, RequestExt, StatusCode,
18-
const_header_value::{TEXT_HTML_UTF8, TEXT_UTF8},
17+
self, HeaderValue, IntoResponse as _, RequestExt, StatusCode,
18+
const_header_value::{JSON, TEXT_HTML_UTF8, TEXT_UTF8},
1919
header::{CONTENT_TYPE, SERVER},
2020
},
2121
util::{
@@ -28,8 +28,8 @@ use xitca_http::{
2828
};
2929
use xitca_service::{Service, ServiceExt, fn_service};
3030

31-
use ser::{HELLO_BYTES, Message};
32-
use util::{HandleResult, QueryParse, SERVER_HEADER_VALUE};
31+
use ser::{HELLO, Message};
32+
use util::{HandleResult, QueryParse};
3333

3434
type Request<B> = http::Request<RequestExt<B>>;
3535

@@ -43,7 +43,7 @@ fn main() -> std::io::Result<()> {
4343
"/plaintext",
4444
get(fn_service(async |ctx: Ctx| {
4545
let (req, _) = ctx.into_parts();
46-
let mut res = req.into_response(const { Bytes::from_static(HELLO_BYTES) });
46+
let mut res = req.into_response(const { Bytes::from_static(HELLO.as_bytes()) });
4747
res.headers_mut().insert(CONTENT_TYPE, TEXT_UTF8);
4848
Ok(res)
4949
})),
@@ -52,7 +52,7 @@ fn main() -> std::io::Result<()> {
5252
"/json",
5353
get(fn_service(async |ctx: Ctx| {
5454
let (req, _) = ctx.into_parts();
55-
json_response(req, &Message::new())
55+
json_response(req, Message::HELLO)
5656
})),
5757
)
5858
.insert(
@@ -91,7 +91,7 @@ fn main() -> std::io::Result<()> {
9191
.enclosed(ContextBuilder::new(db_pool::Client::create))
9292
.enclosed_fn(async |service, req| {
9393
let mut res = service.call(req).await.unwrap_or_else(error_handler);
94-
res.headers_mut().insert(SERVER, SERVER_HEADER_VALUE);
94+
res.headers_mut().insert(SERVER, HeaderValue::from_static("x"));
9595
Ok::<_, core::convert::Infallible>(res)
9696
})
9797
.enclosed(HttpServiceBuilder::h1().io_uring());
@@ -118,16 +118,9 @@ fn error_handler(e: RouterError<util::Error>) -> Response {
118118
.unwrap()
119119
}
120120

121-
#[cfg(any(feature = "json", feature = "perf-json"))]
122121
fn json_response<Ext>(req: Request<Ext>, val: &impl serde_core::Serialize) -> HandleResult<Response> {
123-
let mut buf = xitca_http::bytes::BytesMut::new();
124-
#[cfg(all(feature = "json", not(feature = "perf-json")))]
125-
serde_json::to_writer(xitca_http::bytes::BufMutWriter(&mut buf), val)?;
126-
127-
#[cfg(all(feature = "perf-json", not(feature = "json")))]
128-
sonic_rs::to_writer(xitca_http::bytes::BufMut::writer(&mut buf), val)?;
129-
130-
let mut res = req.into_response(buf.freeze());
131-
res.headers_mut().insert(CONTENT_TYPE, http::const_header_value::JSON);
122+
let buf = ser::json_serialize(val)?;
123+
let mut res = req.into_response(Bytes::from(buf));
124+
res.headers_mut().insert(CONTENT_TYPE, JSON);
132125
Ok(res)
133126
}

frameworks/Rust/xitca-web/src/main_barebone.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ mod util;
1313
use std::io;
1414

1515
use xitca_http::{
16-
bytes::BufMut,
1716
h1::dispatcher_unreal::{Dispatcher, Request, Response},
1817
http::StatusCode,
1918
};
2019
use xitca_service::Service;
2120

2221
use self::{
23-
ser::{HELLO_BYTES, Message},
22+
ser::{HELLO, Message},
2423
util::QueryParse,
2524
};
2625

@@ -96,19 +95,9 @@ async fn handler<'h>(req: Request<'h, db_unrealistic::Client>, res: Response<'h>
9695
res.status(StatusCode::OK)
9796
.header("content-type", "text/plain")
9897
.header("server", "X")
99-
// unrealistic content length header.
100-
.header("content-length", "13")
101-
.body_writer(|buf| buf.extend_from_slice(HELLO_BYTES))
98+
.body(HELLO.as_bytes())
10299
}
103-
"/json" => res
104-
.status(StatusCode::OK)
105-
.header("content-type", "application/json")
106-
.header("server", "X")
107-
// unrealistic content length header.
108-
.header("content-length", "27")
109-
// snoic-rs crate is realistic approach to json serializer.
110-
// That said xitca-web by default utilize serde-json as serializer making it an unrealistic representation of framework performance
111-
.body_writer(|buf| sonic_rs::to_writer(buf.writer(), &Message::new()).unwrap()),
100+
"/json" => json_response(res, Message::HELLO),
112101

113102
// all database related categories are unrealistic. please reference db_unrealistic module for detail.
114103
"/fortunes" => {
@@ -138,12 +127,10 @@ async fn handler<'h>(req: Request<'h, db_unrealistic::Client>, res: Response<'h>
138127
}
139128
}
140129

141-
fn json_response<'r, T>(res: Response<'r>, val: &T) -> Response<'r, 3>
142-
where
143-
T: serde_core::Serialize,
144-
{
145-
let mut buf = xitca_http::bytes::BytesMut::new();
146-
sonic_rs::to_writer((&mut buf).writer(), val).unwrap();
130+
fn json_response<'r>(res: Response<'r>, val: &impl serde_core::Serialize) -> Response<'r, 3> {
131+
// snoic-rs crate is realistic approach to json serializer.
132+
// That said xitca-web by default utilize serde-json as serializer making it an unrealistic representation of framework performance
133+
let buf = ser::json_serialize(val).unwrap();
147134
res.status(StatusCode::OK)
148135
.header("content-type", "application/json")
149136
.header("server", "X")

frameworks/Rust/xitca-web/src/main_orm.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,23 @@ use xitca_web::{
1616
App,
1717
codegen::route,
1818
handler::{html::Html, json::Json, query::Query, state::StateRef},
19-
http::{WebResponse, header::SERVER},
19+
http::{HeaderValue, WebResponse, header::SERVER},
2020
};
2121

2222
use orm::Pool;
23-
use ser::{Num, World};
24-
use util::{HandleResult, SERVER_HEADER_VALUE};
23+
use ser::{Message, Num, World};
24+
use util::HandleResult;
2525

2626
fn main() -> std::io::Result<()> {
2727
App::new()
2828
.with_async_state(Pool::create)
29+
.at("/json", Json(Message::HELLO))
2930
.at_typed(db)
3031
.at_typed(fortunes)
3132
.at_typed(queries)
3233
.at_typed(updates)
3334
.map(|mut res: WebResponse| {
34-
res.headers_mut().insert(SERVER, SERVER_HEADER_VALUE);
35+
res.headers_mut().insert(SERVER, HeaderValue::from_static("xitca-web"));
3536
res
3637
})
3738
.serve()

0 commit comments

Comments
 (0)