From e03768f43d6db44164ca15e5a3d0aa3a796e93bb Mon Sep 17 00:00:00 2001 From: fakeshadow <24548779@qq.com> Date: Sun, 21 Dec 2025 01:33:41 +0800 Subject: [PATCH 1/3] [xitca-web] fix diesel update bench --- frameworks/Rust/xitca-web/Cargo.lock | 54 ++++++++++--------- frameworks/Rust/xitca-web/Cargo.toml | 16 +++--- .../Rust/xitca-web/benchmark_config.json | 1 + .../Rust/xitca-web/src/db_unrealistic.rs | 4 +- 4 files changed, 40 insertions(+), 35 deletions(-) diff --git a/frameworks/Rust/xitca-web/Cargo.lock b/frameworks/Rust/xitca-web/Cargo.lock index 4be7353d20f..5f482313bae 100755 --- a/frameworks/Rust/xitca-web/Cargo.lock +++ b/frameworks/Rust/xitca-web/Cargo.lock @@ -146,9 +146,9 @@ checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3" [[package]] name = "cc" -version = "1.2.49" +version = "1.2.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90583009037521a116abf44494efecd645ba48b6622457080f080b85544e2215" +checksum = "9f50d563227a1c37cc0a263f64eca3334388c01c5e4c4861a9def205c614383c" dependencies = [ "find-msvc-tools", "shlex", @@ -248,9 +248,9 @@ dependencies = [ [[package]] name = "diesel" -version = "2.3.4" +version = "2.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c415189028b232660655e4893e8bc25ca7aee8e96888db66d9edb400535456a" +checksum = "e130c806dccc85428c564f2dc5a96e05b6615a27c9a28776bd7761a9af4bb552" dependencies = [ "bitflags", "byteorder", @@ -274,9 +274,9 @@ dependencies = [ [[package]] name = "diesel_derives" -version = "2.3.5" +version = "2.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8587cbca3c929fb198e7950d761d31ca72b80aa6e07c1b7bec5879d187720436" +checksum = "c30b2969f923fa1f73744b92bb7df60b858df8832742d9a3aceb79236c0be1d2" dependencies = [ "diesel_table_macro_syntax", "dsl_auto_type", @@ -1111,9 +1111,9 @@ checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" [[package]] name = "ryu" -version = "1.0.20" +version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" +checksum = "62049b2877bf12821e8f9ad256ee38fdc31db7387ec2d3b3f403024de2034aea" [[package]] name = "sailfish" @@ -1298,7 +1298,7 @@ checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" [[package]] name = "std-util" version = "0.1.0" -source = "git+https://github.com/fakeshadow/toasty?branch=engine#4e95852bfc90b027a0bdfde8a413263f76581a9e" +source = "git+https://github.com/fakeshadow/toasty?branch=engine#51c8186aea9d07562c4079d420adc82ac898b0fb" dependencies = [ "heck", "pluralizer", @@ -1398,10 +1398,11 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "toasty" version = "0.1.0" -source = "git+https://github.com/fakeshadow/toasty?branch=engine#4e95852bfc90b027a0bdfde8a413263f76581a9e" +source = "git+https://github.com/fakeshadow/toasty?branch=engine#51c8186aea9d07562c4079d420adc82ac898b0fb" dependencies = [ "anyhow", "async-stream", + "bit-set", "by_address", "deadpool", "index_vec", @@ -1417,7 +1418,7 @@ dependencies = [ [[package]] name = "toasty-codegen" version = "0.1.0" -source = "git+https://github.com/fakeshadow/toasty?branch=engine#4e95852bfc90b027a0bdfde8a413263f76581a9e" +source = "git+https://github.com/fakeshadow/toasty?branch=engine#51c8186aea9d07562c4079d420adc82ac898b0fb" dependencies = [ "proc-macro2", "quote", @@ -1428,7 +1429,7 @@ dependencies = [ [[package]] name = "toasty-core" version = "0.1.0" -source = "git+https://github.com/fakeshadow/toasty?branch=engine#4e95852bfc90b027a0bdfde8a413263f76581a9e" +source = "git+https://github.com/fakeshadow/toasty?branch=engine#51c8186aea9d07562c4079d420adc82ac898b0fb" dependencies = [ "anyhow", "async-trait", @@ -1442,7 +1443,7 @@ dependencies = [ [[package]] name = "toasty-macros" version = "0.1.0" -source = "git+https://github.com/fakeshadow/toasty?branch=engine#4e95852bfc90b027a0bdfde8a413263f76581a9e" +source = "git+https://github.com/fakeshadow/toasty?branch=engine#51c8186aea9d07562c4079d420adc82ac898b0fb" dependencies = [ "proc-macro2", "quote", @@ -1454,7 +1455,7 @@ dependencies = [ [[package]] name = "toasty-sql" version = "0.1.0" -source = "git+https://github.com/fakeshadow/toasty?branch=engine#4e95852bfc90b027a0bdfde8a413263f76581a9e" +source = "git+https://github.com/fakeshadow/toasty?branch=engine#51c8186aea9d07562c4079d420adc82ac898b0fb" dependencies = [ "anyhow", "toasty-core", @@ -1514,9 +1515,9 @@ dependencies = [ [[package]] name = "tracing" -version = "0.1.43" +version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d15d90a0b5c19378952d479dc858407149d7bb45a14de0142f6c534b16fc647" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" dependencies = [ "pin-project-lite", "tracing-core", @@ -1524,9 +1525,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.35" +version = "0.1.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a04e24fab5c89c6a36eb8558c9656f30d81de51dfa4d3b45f26b21d61fa0a6c" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" [[package]] name = "typenum" @@ -1789,7 +1790,7 @@ checksum = "9edde0db4769d2dc68579893f2306b26c6ecfbe0ef499b013d731b7b9247e0b9" [[package]] name = "xitca-codegen" version = "0.4.0" -source = "git+http://github.com/HFQR/xitca-web?rev=faf1ae2#faf1ae24bf966a1dddccf02f923ecac28b387c03" +source = "git+http://github.com/HFQR/xitca-web?rev=6544494#6544494c28e61317e1423782f41a89bd908d4fff" dependencies = [ "quote", "syn", @@ -1798,7 +1799,7 @@ dependencies = [ [[package]] name = "xitca-http" version = "0.7.1" -source = "git+http://github.com/HFQR/xitca-web?rev=faf1ae2#faf1ae24bf966a1dddccf02f923ecac28b387c03" +source = "git+http://github.com/HFQR/xitca-web?rev=6544494#6544494c28e61317e1423782f41a89bd908d4fff" dependencies = [ "futures-core", "http", @@ -1831,7 +1832,7 @@ dependencies = [ [[package]] name = "xitca-postgres" version = "0.3.0" -source = "git+http://github.com/HFQR/xitca-web?rev=faf1ae2#faf1ae24bf966a1dddccf02f923ecac28b387c03" +source = "git+http://github.com/HFQR/xitca-web?rev=6544494#6544494c28e61317e1423782f41a89bd908d4fff" dependencies = [ "fallible-iterator", "futures-core", @@ -1847,7 +1848,7 @@ dependencies = [ [[package]] name = "xitca-postgres-diesel" version = "0.2.0" -source = "git+https://github.com/fakeshadow/xitca-postgres-diesel?rev=1bd39ac#1bd39ac23f8bb2f45d44b94c775a547ee92db494" +source = "git+https://github.com/fakeshadow/xitca-postgres-diesel?rev=ca6ae52#ca6ae52eeed567315030745bb4639a8b64c46eef" dependencies = [ "diesel", "diesel-async", @@ -1860,9 +1861,10 @@ dependencies = [ [[package]] name = "xitca-postgres-toasty" version = "0.1.0" -source = "git+https://github.com/fakeshadow/xitca-postgres-toasty?rev=270fe35#270fe35a3b9ac8b5cb3560e14c5a00e52d04eeeb" +source = "git+https://github.com/fakeshadow/xitca-postgres-toasty?rev=1ce7536#1ce7536cf72ca8aa13482e91ffc87459ec605e20" dependencies = [ "anyhow", + "async-stream", "futures-core", "pin-project-lite", "postgres-types", @@ -1885,7 +1887,7 @@ dependencies = [ [[package]] name = "xitca-server" version = "0.5.0" -source = "git+http://github.com/HFQR/xitca-web?rev=faf1ae2#faf1ae24bf966a1dddccf02f923ecac28b387c03" +source = "git+http://github.com/HFQR/xitca-web?rev=6544494#6544494c28e61317e1423782f41a89bd908d4fff" dependencies = [ "socket2", "tokio", @@ -1899,7 +1901,7 @@ dependencies = [ [[package]] name = "xitca-service" version = "0.3.0" -source = "git+http://github.com/HFQR/xitca-web?rev=faf1ae2#faf1ae24bf966a1dddccf02f923ecac28b387c03" +source = "git+http://github.com/HFQR/xitca-web?rev=6544494#6544494c28e61317e1423782f41a89bd908d4fff" [[package]] name = "xitca-unsafe-collection" @@ -1944,7 +1946,7 @@ dependencies = [ [[package]] name = "xitca-web" version = "0.7.1" -source = "git+http://github.com/HFQR/xitca-web?rev=faf1ae2#faf1ae24bf966a1dddccf02f923ecac28b387c03" +source = "git+http://github.com/HFQR/xitca-web?rev=6544494#6544494c28e61317e1423782f41a89bd908d4fff" dependencies = [ "futures-core", "pin-project-lite", diff --git a/frameworks/Rust/xitca-web/Cargo.toml b/frameworks/Rust/xitca-web/Cargo.toml index 7a2c0e4c1e7..4befc218583 100755 --- a/frameworks/Rust/xitca-web/Cargo.toml +++ b/frameworks/Rust/xitca-web/Cargo.toml @@ -98,8 +98,8 @@ codegen-units = 1 panic = "abort" [patch.crates-io] -xitca-postgres-diesel = { git = "https://github.com/fakeshadow/xitca-postgres-diesel", rev = "1bd39ac" } -xitca-postgres-toasty = { git = "https://github.com/fakeshadow/xitca-postgres-toasty", rev = "270fe35" } +xitca-postgres-diesel = { git = "https://github.com/fakeshadow/xitca-postgres-diesel", rev = "ca6ae52" } +xitca-postgres-toasty = { git = "https://github.com/fakeshadow/xitca-postgres-toasty", rev = "1ce7536" } toasty = { git = "https://github.com/fakeshadow/toasty", branch = "engine" } toasty-core = { git = "https://github.com/fakeshadow/toasty", branch = "engine" } @@ -108,9 +108,9 @@ toasty-sql = { git = "https://github.com/fakeshadow/toasty", branch = "engine" } # personal fork of tokio-uring with tokio local runtime enabled tokio-uring = { git = "http://github.com/fakeshadow/tokio-uring", rev = "c3d5887" } -xitca-codegen = { git = "http://github.com/HFQR/xitca-web", rev = "faf1ae2" } -xitca-http = { git = "http://github.com/HFQR/xitca-web", rev = "faf1ae2" } -xitca-postgres = { git = "http://github.com/HFQR/xitca-web", rev = "faf1ae2" } -xitca-server = { git = "http://github.com/HFQR/xitca-web", rev = "faf1ae2" } -xitca-service = { git = "http://github.com/HFQR/xitca-web", rev = "faf1ae2" } -xitca-web = { git = "http://github.com/HFQR/xitca-web", rev = "faf1ae2" } +xitca-codegen = { git = "http://github.com/HFQR/xitca-web", rev = "6544494" } +xitca-http = { git = "http://github.com/HFQR/xitca-web", rev = "6544494" } +xitca-postgres = { git = "http://github.com/HFQR/xitca-web", rev = "6544494" } +xitca-server = { git = "http://github.com/HFQR/xitca-web", rev = "6544494" } +xitca-service = { git = "http://github.com/HFQR/xitca-web", rev = "6544494" } +xitca-web = { git = "http://github.com/HFQR/xitca-web", rev = "6544494" } diff --git a/frameworks/Rust/xitca-web/benchmark_config.json b/frameworks/Rust/xitca-web/benchmark_config.json index 9562bf0a7a4..b1b96249711 100755 --- a/frameworks/Rust/xitca-web/benchmark_config.json +++ b/frameworks/Rust/xitca-web/benchmark_config.json @@ -50,6 +50,7 @@ "db_url": "/db", "fortune_url": "/fortunes", "query_url": "/queries?q=", + "update_url": "/updates?q=", "port": 8080, "approach": "realistic", "classification": "fullstack", diff --git a/frameworks/Rust/xitca-web/src/db_unrealistic.rs b/frameworks/Rust/xitca-web/src/db_unrealistic.rs index 00013cf3928..7af809089af 100755 --- a/frameworks/Rust/xitca-web/src/db_unrealistic.rs +++ b/frameworks/Rust/xitca-web/src/db_unrealistic.rs @@ -21,7 +21,9 @@ pub struct Client { impl Client { pub async fn create() -> HandleResult { - let (cli, mut drv) = xitca_postgres::Postgres::new(DB_URL).connect().await?; + let (cli, drv) = xitca_postgres::Postgres::new(DB_URL).connect().await?; + + let mut drv = drv.try_into_tcp().expect("raw tcp is used for database connection"); tokio::task::spawn(async move { while drv.try_next().await?.is_some() {} From c6110934f809dfa5760486ec6ade99d7bdda929b Mon Sep 17 00:00:00 2001 From: fakeshadow <24548779@qq.com> Date: Mon, 22 Dec 2025 02:44:24 +0800 Subject: [PATCH 2/3] module clean up. resolve majority of dead code --- frameworks/Rust/xitca-web/Cargo.lock | 20 +- frameworks/Rust/xitca-web/Cargo.toml | 14 +- frameworks/Rust/xitca-web/src/db.rs | 2 +- frameworks/Rust/xitca-web/src/main.rs | 59 +++- .../Rust/xitca-web/src/main_barebone.rs | 7 +- frameworks/Rust/xitca-web/src/ser.rs | 258 ++++++++---------- 6 files changed, 188 insertions(+), 172 deletions(-) diff --git a/frameworks/Rust/xitca-web/Cargo.lock b/frameworks/Rust/xitca-web/Cargo.lock index 5f482313bae..4cec6d56b9d 100755 --- a/frameworks/Rust/xitca-web/Cargo.lock +++ b/frameworks/Rust/xitca-web/Cargo.lock @@ -643,9 +643,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.15" +version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" +checksum = "7ee5b5339afb4c41626dde77b7a611bd4f2c202b897852b4bcf5d03eddc61010" [[package]] name = "itoap" @@ -1790,7 +1790,7 @@ checksum = "9edde0db4769d2dc68579893f2306b26c6ecfbe0ef499b013d731b7b9247e0b9" [[package]] name = "xitca-codegen" version = "0.4.0" -source = "git+http://github.com/HFQR/xitca-web?rev=6544494#6544494c28e61317e1423782f41a89bd908d4fff" +source = "git+http://github.com/HFQR/xitca-web?rev=7ef2cc4#7ef2cc423b799702b5801288d49ff7fbfe01113e" dependencies = [ "quote", "syn", @@ -1799,7 +1799,7 @@ dependencies = [ [[package]] name = "xitca-http" version = "0.7.1" -source = "git+http://github.com/HFQR/xitca-web?rev=6544494#6544494c28e61317e1423782f41a89bd908d4fff" +source = "git+http://github.com/HFQR/xitca-web?rev=7ef2cc4#7ef2cc423b799702b5801288d49ff7fbfe01113e" dependencies = [ "futures-core", "http", @@ -1832,7 +1832,7 @@ dependencies = [ [[package]] name = "xitca-postgres" version = "0.3.0" -source = "git+http://github.com/HFQR/xitca-web?rev=6544494#6544494c28e61317e1423782f41a89bd908d4fff" +source = "git+http://github.com/HFQR/xitca-web?rev=7ef2cc4#7ef2cc423b799702b5801288d49ff7fbfe01113e" dependencies = [ "fallible-iterator", "futures-core", @@ -1848,11 +1848,11 @@ dependencies = [ [[package]] name = "xitca-postgres-diesel" version = "0.2.0" -source = "git+https://github.com/fakeshadow/xitca-postgres-diesel?rev=ca6ae52#ca6ae52eeed567315030745bb4639a8b64c46eef" +source = "git+https://github.com/fakeshadow/xitca-postgres-diesel?rev=b6d1922#b6d19224ebddcdeb54cc184d86085e74f0deb807" dependencies = [ "diesel", "diesel-async", - "futures-util", + "futures-core", "scoped-futures", "tokio", "xitca-postgres", @@ -1887,7 +1887,7 @@ dependencies = [ [[package]] name = "xitca-server" version = "0.5.0" -source = "git+http://github.com/HFQR/xitca-web?rev=6544494#6544494c28e61317e1423782f41a89bd908d4fff" +source = "git+http://github.com/HFQR/xitca-web?rev=7ef2cc4#7ef2cc423b799702b5801288d49ff7fbfe01113e" dependencies = [ "socket2", "tokio", @@ -1901,7 +1901,7 @@ dependencies = [ [[package]] name = "xitca-service" version = "0.3.0" -source = "git+http://github.com/HFQR/xitca-web?rev=6544494#6544494c28e61317e1423782f41a89bd908d4fff" +source = "git+http://github.com/HFQR/xitca-web?rev=7ef2cc4#7ef2cc423b799702b5801288d49ff7fbfe01113e" [[package]] name = "xitca-unsafe-collection" @@ -1946,7 +1946,7 @@ dependencies = [ [[package]] name = "xitca-web" version = "0.7.1" -source = "git+http://github.com/HFQR/xitca-web?rev=6544494#6544494c28e61317e1423782f41a89bd908d4fff" +source = "git+http://github.com/HFQR/xitca-web?rev=7ef2cc4#7ef2cc423b799702b5801288d49ff7fbfe01113e" dependencies = [ "futures-core", "pin-project-lite", diff --git a/frameworks/Rust/xitca-web/Cargo.toml b/frameworks/Rust/xitca-web/Cargo.toml index 4befc218583..a1322b5b269 100755 --- a/frameworks/Rust/xitca-web/Cargo.toml +++ b/frameworks/Rust/xitca-web/Cargo.toml @@ -98,7 +98,7 @@ codegen-units = 1 panic = "abort" [patch.crates-io] -xitca-postgres-diesel = { git = "https://github.com/fakeshadow/xitca-postgres-diesel", rev = "ca6ae52" } +xitca-postgres-diesel = { git = "https://github.com/fakeshadow/xitca-postgres-diesel", rev = "b6d1922" } xitca-postgres-toasty = { git = "https://github.com/fakeshadow/xitca-postgres-toasty", rev = "1ce7536" } toasty = { git = "https://github.com/fakeshadow/toasty", branch = "engine" } @@ -108,9 +108,9 @@ toasty-sql = { git = "https://github.com/fakeshadow/toasty", branch = "engine" } # personal fork of tokio-uring with tokio local runtime enabled tokio-uring = { git = "http://github.com/fakeshadow/tokio-uring", rev = "c3d5887" } -xitca-codegen = { git = "http://github.com/HFQR/xitca-web", rev = "6544494" } -xitca-http = { git = "http://github.com/HFQR/xitca-web", rev = "6544494" } -xitca-postgres = { git = "http://github.com/HFQR/xitca-web", rev = "6544494" } -xitca-server = { git = "http://github.com/HFQR/xitca-web", rev = "6544494" } -xitca-service = { git = "http://github.com/HFQR/xitca-web", rev = "6544494" } -xitca-web = { git = "http://github.com/HFQR/xitca-web", rev = "6544494" } +xitca-codegen = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" } +xitca-http = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" } +xitca-postgres = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" } +xitca-server = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" } +xitca-service = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" } +xitca-web = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" } diff --git a/frameworks/Rust/xitca-web/src/db.rs b/frameworks/Rust/xitca-web/src/db.rs index 582f3d3e17a..f3c50267d55 100755 --- a/frameworks/Rust/xitca-web/src/db.rs +++ b/frameworks/Rust/xitca-web/src/db.rs @@ -119,7 +119,7 @@ impl Exec { let mut fortunes = Vec::with_capacity(16); while let Some(row) = res.try_next().await? { - fortunes.push(Fortune::new(row.get(0), row.get::(1))); + fortunes.push(Fortune::new(row.get(0), row.get_zc(1))); } Ok(Fortunes::new(fortunes)) diff --git a/frameworks/Rust/xitca-web/src/main.rs b/frameworks/Rust/xitca-web/src/main.rs index f14061f536c..345ac34df82 100755 --- a/frameworks/Rust/xitca-web/src/main.rs +++ b/frameworks/Rust/xitca-web/src/main.rs @@ -10,8 +10,14 @@ mod util; use xitca_http::{ HttpServiceBuilder, + body::Once, + bytes::Bytes, h1::RequestBody, - http::{StatusCode, header::SERVER}, + http::{ + self, IntoResponse as _, RequestExt, StatusCode, + const_header_value::{TEXT_HTML_UTF8, TEXT_UTF8}, + header::{CONTENT_TYPE, SERVER}, + }, util::{ middleware::context::{Context, ContextBuilder}, service::{ @@ -22,8 +28,12 @@ use xitca_http::{ }; use xitca_service::{Service, ServiceExt, fn_service}; -use ser::{IntoResponse, Message, Request, Response, error_response}; -use util::{QueryParse, SERVER_HEADER_VALUE}; +use ser::{HELLO_BYTES, Message}; +use util::{HandleResult, QueryParse, SERVER_HEADER_VALUE}; + +type Request = http::Request>; + +type Response = http::Response>; type Ctx<'a> = Context<'a, Request, db_pool::Client>; @@ -31,26 +41,35 @@ fn main() -> std::io::Result<()> { let service = Router::new() .insert( "/plaintext", - get(fn_service(async |ctx: Ctx| Ok(ctx.into_parts().0.text_response()))), + get(fn_service(async |ctx: Ctx| { + let (req, _) = ctx.into_parts(); + let mut res = req.into_response(const { Bytes::from_static(HELLO_BYTES) }); + res.headers_mut().insert(CONTENT_TYPE, TEXT_UTF8); + Ok(res) + })), ) .insert( "/json", get(fn_service(async |ctx: Ctx| { - ctx.into_parts().0.json_response(&Message::new()) + let (req, _) = ctx.into_parts(); + json_response(req, &Message::new()) })), ) .insert( "/db", get(fn_service(async |ctx: Ctx| { let (req, cli) = ctx.into_parts(); - cli.db().await.and_then(|w| req.json_response(&w)) + cli.db().await.and_then(|w| json_response(req, &w)) })), ) .insert( "/fortunes", get(fn_service(async |ctx: Ctx| { let (req, cli) = ctx.into_parts(); - cli.fortunes().await?.render_once().map(|f| req.html_response(f)) + let fortunes = cli.fortunes().await?.render_once()?; + let mut res = req.into_response(Bytes::from(fortunes)); + res.headers_mut().insert(CONTENT_TYPE, TEXT_HTML_UTF8); + Ok(res) })), ) .insert( @@ -58,7 +77,7 @@ fn main() -> std::io::Result<()> { get(fn_service(async |ctx: Ctx| { let (req, cli) = ctx.into_parts(); let num = req.uri().query().parse_query(); - cli.queries(num).await.and_then(|w| req.json_response(&w)) + cli.queries(num).await.and_then(|w| json_response(req, &w)) })), ) .insert( @@ -66,7 +85,7 @@ fn main() -> std::io::Result<()> { get(fn_service(async |ctx: Ctx| { let (req, cli) = ctx.into_parts(); let num = req.uri().query().parse_query(); - cli.updates(num).await.and_then(|w| req.json_response(&w)) + cli.updates(num).await.and_then(|w| json_response(req, &w)) })), ) .enclosed(ContextBuilder::new(db_pool::Client::create)) @@ -85,12 +104,30 @@ fn main() -> std::io::Result<()> { #[cold] #[inline(never)] fn error_handler(e: RouterError) -> Response { - error_response(match e { + let status = match e { RouterError::Match(_) => StatusCode::NOT_FOUND, RouterError::NotAllowed(_) => StatusCode::METHOD_NOT_ALLOWED, RouterError::Service(e) => { eprintln!("Internal Error: {e}"); StatusCode::INTERNAL_SERVER_ERROR } - }) + }; + http::Response::builder() + .status(status) + .body(Once::new(Bytes::new())) + .unwrap() +} + +#[cfg(any(feature = "json", feature = "perf-json"))] +fn json_response(req: Request, val: &impl serde_core::Serialize) -> HandleResult { + let mut buf = xitca_http::bytes::BytesMut::new(); + #[cfg(all(feature = "json", not(feature = "perf-json")))] + serde_json::to_writer(xitca_http::bytes::BufMutWriter(&mut buf), val)?; + + #[cfg(all(feature = "perf-json", not(feature = "json")))] + sonic_rs::to_writer(xitca_http::bytes::BufMut::writer(&mut buf), val)?; + + let mut res = req.into_response(buf.freeze()); + res.headers_mut().insert(CONTENT_TYPE, http::const_header_value::JSON); + Ok(res) } diff --git a/frameworks/Rust/xitca-web/src/main_barebone.rs b/frameworks/Rust/xitca-web/src/main_barebone.rs index 2b9a5b17f2f..5474121dc9b 100644 --- a/frameworks/Rust/xitca-web/src/main_barebone.rs +++ b/frameworks/Rust/xitca-web/src/main_barebone.rs @@ -19,7 +19,10 @@ use xitca_http::{ }; use xitca_service::Service; -use self::{ser::Message, util::QueryParse}; +use self::{ + ser::{HELLO_BYTES, Message}, + util::QueryParse, +}; fn main() -> io::Result<()> { let addr = "0.0.0.0:8080".parse().unwrap(); @@ -95,7 +98,7 @@ async fn handler<'h>(req: Request<'h, db_unrealistic::Client>, res: Response<'h> .header("server", "X") // unrealistic content length header. .header("content-length", "13") - .body_writer(|buf| buf.extend_from_slice(b"Hello, World!")) + .body_writer(|buf| buf.extend_from_slice(HELLO_BYTES)) } "/json" => res .status(StatusCode::OK) diff --git a/frameworks/Rust/xitca-web/src/ser.rs b/frameworks/Rust/xitca-web/src/ser.rs index 6ea529b9b78..2a25cb24a43 100755 --- a/frameworks/Rust/xitca-web/src/ser.rs +++ b/frameworks/Rust/xitca-web/src/ser.rs @@ -1,37 +1,10 @@ -#![allow(dead_code)] +use serde_core::{Serialize, Serializer, ser::SerializeStruct}; -use std::borrow::Cow; - -use serde_core::{Deserialize, Deserializer, Serialize, Serializer, ser::SerializeStruct}; -use xitca_http::{ - body::Once, - bytes::Bytes, - http::{ - self, IntoResponse as _, RequestExt, StatusCode, - const_header_value::{TEXT_HTML_UTF8, TEXT_UTF8}, - header::CONTENT_TYPE, - }, -}; +#[cfg(feature = "pg")] +use xitca_unsafe_collection::bytes::BytesStr; use crate::util::HandleResult; -const HELLO: &str = "Hello, World!"; -const HELLO_BYTES: &[u8] = HELLO.as_bytes(); - -#[derive(Clone)] -pub struct Message { - message: &'static str, -} - -impl Message { - #[inline] - pub const fn new() -> Self { - Self { message: HELLO } - } -} - -pub struct Num(pub u16); - #[cfg_attr(feature = "diesel", derive(diesel::Queryable))] #[cfg_attr(feature = "toasty", derive(toasty::Model))] #[cfg_attr(feature = "toasty", table = "world")] @@ -41,6 +14,7 @@ pub struct World { pub randomnumber: i32, } +#[allow(dead_code)] impl World { #[inline] pub const fn new(id: i32, randomnumber: i32) -> Self { @@ -54,16 +28,32 @@ impl World { pub struct Fortune { #[cfg_attr(feature = "toasty", key)] pub id: i32, - pub message: Cow<'static, str>, + pub message: FortuneMessage, } +#[cfg(feature = "pg")] +type FortuneMessage = BytesStr; + +#[cfg(not(feature = "pg"))] +type FortuneMessage = String; + impl Fortune { + const RUNTIME: &str = "Additional fortune added at request time."; + + #[cfg(feature = "pg")] + fn runtime() -> Self { + Self::new(0, const { BytesStr::from_static(Self::RUNTIME) }) + } + + #[cfg(not(feature = "pg"))] #[inline] - pub fn new(id: i32, message: impl Into>) -> Self { - Self { - id, - message: message.into(), - } + fn runtime() -> Self { + Self::new(0, String::from(Self::RUNTIME)) + } + + #[inline] + pub const fn new(id: i32, message: FortuneMessage) -> Self { + Self { id, message } } } @@ -74,7 +64,7 @@ pub struct Fortunes { impl Fortunes { #[inline] pub fn new(mut items: Vec) -> Self { - items.push(Fortune::new(0, "Additional fortune added at request time.")); + items.push(Fortune::runtime()); items.sort_by(|a, b| a.message.cmp(&b.message)); Self { items } } @@ -96,7 +86,7 @@ impl Fortunes { buf.push_str(""); Render::render_escaped(&item.id, &mut buf)?; buf.push_str(""); - Render::render_escaped(&item.message, &mut buf)?; + Render::render_escaped(item.message.as_str(), &mut buf)?; buf.push_str(""); } buf.push_str(SUFFIX); @@ -105,78 +95,112 @@ impl Fortunes { } } -impl<'de> Deserialize<'de> for Num { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - use core::fmt; +#[cfg(feature = "pg")] +pub use message::{HELLO_BYTES, Message}; - use serde_core::de::{Error, MapAccess, Visitor}; +#[cfg(feature = "pg")] +mod message { + use serde_core::{Serialize, Serializer, ser::SerializeStruct}; - const FIELDS: &[&str] = &["q"]; + const HELLO: &str = "Hello, World!"; - struct Field; + pub const HELLO_BYTES: &[u8] = HELLO.as_bytes(); - impl<'de> Deserialize<'de> for Field { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - struct FieldVisitor; + #[derive(Clone)] + pub struct Message { + message: &'static str, + } - impl Visitor<'_> for FieldVisitor { - type Value = Field; + impl Message { + #[inline] + pub const fn new() -> Self { + Self { message: HELLO } + } + } - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("`q`") - } + impl Serialize for Message { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let mut res = serializer.serialize_struct("Message", 1)?; + res.serialize_field("message", self.message)?; + res.end() + } + } +} + +#[cfg(feature = "web-codegen")] +pub use num::Num; + +#[cfg(feature = "web-codegen")] +mod num { + use serde_core::{Deserialize, Deserializer}; + + pub struct Num(pub u16); + + impl<'de> Deserialize<'de> for Num { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + use core::fmt; + + use serde_core::de::{Error, MapAccess, Visitor}; + + const FIELDS: &[&str] = &["q"]; + + struct Field; + + impl<'de> Deserialize<'de> for Field { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + struct FieldVisitor; - fn visit_str(self, value: &str) -> Result - where - E: Error, - { - match value { - "q" => Ok(Field), - _ => Err(Error::unknown_field(value, FIELDS)), + impl Visitor<'_> for FieldVisitor { + type Value = Field; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("`q`") + } + + fn visit_str(self, value: &str) -> Result + where + E: Error, + { + match value { + "q" => Ok(Field), + _ => Err(Error::unknown_field(value, FIELDS)), + } } } - } - deserializer.deserialize_identifier(FieldVisitor) + deserializer.deserialize_identifier(FieldVisitor) + } } - } - struct NumVisitor; + struct NumVisitor; - impl<'de> Visitor<'de> for NumVisitor { - type Value = Num; + impl<'de> Visitor<'de> for NumVisitor { + type Value = Num; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("struct Num") - } + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("struct Num") + } - fn visit_map(self, mut map: V) -> Result - where - V: MapAccess<'de>, - { - map.next_key::()?.ok_or_else(|| Error::missing_field("q"))?; - Ok(Num(map.next_value().unwrap_or(1).clamp(1, 500))) + fn visit_map(self, mut map: V) -> Result + where + V: MapAccess<'de>, + { + map.next_key::()?.ok_or_else(|| Error::missing_field("q"))?; + Ok(Num(map.next_value().unwrap_or(1).clamp(1, 500))) + } } - } - deserializer.deserialize_struct("Num", FIELDS, NumVisitor) - } -} - -impl Serialize for Message { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut res = serializer.serialize_struct("Message", 1)?; - res.serialize_field("message", self.message)?; - res.end() + deserializer.deserialize_struct("Num", FIELDS, NumVisitor) + } } } @@ -191,51 +215,3 @@ impl Serialize for World { res.end() } } - -pub type Request = http::Request>; -pub type Response = http::Response>; - -pub trait IntoResponse: Sized { - #[cfg(any(feature = "json", feature = "perf-json"))] - fn json_response(self, val: &impl Serialize) -> HandleResult; - - fn text_response(self) -> Response; - - fn html_response(self, val: String) -> Response; -} - -impl IntoResponse for Request { - #[cfg(any(feature = "json", feature = "perf-json"))] - fn json_response(self, val: &impl Serialize) -> HandleResult { - let mut buf = xitca_http::bytes::BytesMut::new(); - #[cfg(all(feature = "json", not(feature = "perf-json")))] - serde_json::to_writer(xitca_http::bytes::BufMutWriter(&mut buf), val)?; - - #[cfg(all(feature = "perf-json", not(feature = "json")))] - sonic_rs::to_writer(xitca_http::bytes::BufMut::writer(&mut buf), val)?; - - let mut res = self.into_response(buf.freeze()); - res.headers_mut() - .insert(CONTENT_TYPE, xitca_http::http::const_header_value::JSON); - Ok(res) - } - - fn text_response(self) -> Response { - let mut res = self.into_response(const { Bytes::from_static(HELLO_BYTES) }); - res.headers_mut().insert(CONTENT_TYPE, TEXT_UTF8); - res - } - - fn html_response(self, val: String) -> Response { - let mut res = self.into_response(Bytes::from(val)); - res.headers_mut().insert(CONTENT_TYPE, TEXT_HTML_UTF8); - res - } -} - -pub fn error_response(status: StatusCode) -> Response { - http::Response::builder() - .status(status) - .body(Once::new(Bytes::new())) - .unwrap() -} From 5c4ba35f84f99f89d681ba8b60d113c18870f090 Mon Sep 17 00:00:00 2001 From: fakeshadow <24548779@qq.com> Date: Mon, 22 Dec 2025 16:49:34 +0800 Subject: [PATCH 3/3] improve toasty perf --- frameworks/Rust/xitca-web/Cargo.lock | 14 +++++++------- frameworks/Rust/xitca-web/Cargo.toml | 14 +++++++------- frameworks/Rust/xitca-web/src/db.rs | 6 +++--- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/frameworks/Rust/xitca-web/Cargo.lock b/frameworks/Rust/xitca-web/Cargo.lock index 4cec6d56b9d..3d52cf36709 100755 --- a/frameworks/Rust/xitca-web/Cargo.lock +++ b/frameworks/Rust/xitca-web/Cargo.lock @@ -1790,7 +1790,7 @@ checksum = "9edde0db4769d2dc68579893f2306b26c6ecfbe0ef499b013d731b7b9247e0b9" [[package]] name = "xitca-codegen" version = "0.4.0" -source = "git+http://github.com/HFQR/xitca-web?rev=7ef2cc4#7ef2cc423b799702b5801288d49ff7fbfe01113e" +source = "git+http://github.com/HFQR/xitca-web?rev=3f38156#3f381568a2a7b1d854d09e2acedd739f6a420388" dependencies = [ "quote", "syn", @@ -1799,7 +1799,7 @@ dependencies = [ [[package]] name = "xitca-http" version = "0.7.1" -source = "git+http://github.com/HFQR/xitca-web?rev=7ef2cc4#7ef2cc423b799702b5801288d49ff7fbfe01113e" +source = "git+http://github.com/HFQR/xitca-web?rev=3f38156#3f381568a2a7b1d854d09e2acedd739f6a420388" dependencies = [ "futures-core", "http", @@ -1832,7 +1832,7 @@ dependencies = [ [[package]] name = "xitca-postgres" version = "0.3.0" -source = "git+http://github.com/HFQR/xitca-web?rev=7ef2cc4#7ef2cc423b799702b5801288d49ff7fbfe01113e" +source = "git+http://github.com/HFQR/xitca-web?rev=3f38156#3f381568a2a7b1d854d09e2acedd739f6a420388" dependencies = [ "fallible-iterator", "futures-core", @@ -1861,7 +1861,7 @@ dependencies = [ [[package]] name = "xitca-postgres-toasty" version = "0.1.0" -source = "git+https://github.com/fakeshadow/xitca-postgres-toasty?rev=1ce7536#1ce7536cf72ca8aa13482e91ffc87459ec605e20" +source = "git+https://github.com/fakeshadow/xitca-postgres-toasty?rev=6034a22#6034a220a76214655b3a74b8a99786954034b2a4" dependencies = [ "anyhow", "async-stream", @@ -1887,7 +1887,7 @@ dependencies = [ [[package]] name = "xitca-server" version = "0.5.0" -source = "git+http://github.com/HFQR/xitca-web?rev=7ef2cc4#7ef2cc423b799702b5801288d49ff7fbfe01113e" +source = "git+http://github.com/HFQR/xitca-web?rev=3f38156#3f381568a2a7b1d854d09e2acedd739f6a420388" dependencies = [ "socket2", "tokio", @@ -1901,7 +1901,7 @@ dependencies = [ [[package]] name = "xitca-service" version = "0.3.0" -source = "git+http://github.com/HFQR/xitca-web?rev=7ef2cc4#7ef2cc423b799702b5801288d49ff7fbfe01113e" +source = "git+http://github.com/HFQR/xitca-web?rev=3f38156#3f381568a2a7b1d854d09e2acedd739f6a420388" [[package]] name = "xitca-unsafe-collection" @@ -1946,7 +1946,7 @@ dependencies = [ [[package]] name = "xitca-web" version = "0.7.1" -source = "git+http://github.com/HFQR/xitca-web?rev=7ef2cc4#7ef2cc423b799702b5801288d49ff7fbfe01113e" +source = "git+http://github.com/HFQR/xitca-web?rev=3f38156#3f381568a2a7b1d854d09e2acedd739f6a420388" dependencies = [ "futures-core", "pin-project-lite", diff --git a/frameworks/Rust/xitca-web/Cargo.toml b/frameworks/Rust/xitca-web/Cargo.toml index a1322b5b269..ccb54fe3e4f 100755 --- a/frameworks/Rust/xitca-web/Cargo.toml +++ b/frameworks/Rust/xitca-web/Cargo.toml @@ -99,7 +99,7 @@ panic = "abort" [patch.crates-io] xitca-postgres-diesel = { git = "https://github.com/fakeshadow/xitca-postgres-diesel", rev = "b6d1922" } -xitca-postgres-toasty = { git = "https://github.com/fakeshadow/xitca-postgres-toasty", rev = "1ce7536" } +xitca-postgres-toasty = { git = "https://github.com/fakeshadow/xitca-postgres-toasty", rev = "6034a22" } toasty = { git = "https://github.com/fakeshadow/toasty", branch = "engine" } toasty-core = { git = "https://github.com/fakeshadow/toasty", branch = "engine" } @@ -108,9 +108,9 @@ toasty-sql = { git = "https://github.com/fakeshadow/toasty", branch = "engine" } # personal fork of tokio-uring with tokio local runtime enabled tokio-uring = { git = "http://github.com/fakeshadow/tokio-uring", rev = "c3d5887" } -xitca-codegen = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" } -xitca-http = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" } -xitca-postgres = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" } -xitca-server = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" } -xitca-service = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" } -xitca-web = { git = "http://github.com/HFQR/xitca-web", rev = "7ef2cc4" } +xitca-codegen = { git = "http://github.com/HFQR/xitca-web", rev = "3f38156" } +xitca-http = { git = "http://github.com/HFQR/xitca-web", rev = "3f38156" } +xitca-postgres = { git = "http://github.com/HFQR/xitca-web", rev = "3f38156" } +xitca-server = { git = "http://github.com/HFQR/xitca-web", rev = "3f38156" } +xitca-service = { git = "http://github.com/HFQR/xitca-web", rev = "3f38156" } +xitca-web = { git = "http://github.com/HFQR/xitca-web", rev = "3f38156" } diff --git a/frameworks/Rust/xitca-web/src/db.rs b/frameworks/Rust/xitca-web/src/db.rs index f3c50267d55..3b4dbb2408e 100755 --- a/frameworks/Rust/xitca-web/src/db.rs +++ b/frameworks/Rust/xitca-web/src/db.rs @@ -25,9 +25,9 @@ pub struct Exec { impl Exec { pub const FORTUNE_STMT: StatementNamed<'_> = Statement::named("SELECT id,message FROM fortune", &[]); pub const WORLD_STMT: StatementNamed<'_> = - Statement::named("SELECT id,randomnumber FROM world WHERE id=$1", &[Type::INT4]); + Statement::named("SELECT id,randomNumber FROM world WHERE id=$1", &[Type::INT4]); pub const UPDATE_STMT: StatementNamed<'_> = Statement::named( - "UPDATE world SET randomnumber=w.r FROM (SELECT unnest($1) as i,unnest($2) as r) w WHERE world.id=w.i", + "UPDATE world SET randomNumber=w.r FROM (SELECT unnest($1) as i,unnest($2) as r) w WHERE world.id=w.i", &[Type::INT4_ARRAY, Type::INT4_ARRAY], ); @@ -92,7 +92,7 @@ impl Exec { }) .collect::<(Vec<_>, Vec<_>, Vec<_>)>(); - let update = update_stmt.bind([&ids, &rngs]).query(&conn); + let update = update_stmt.bind([&ids, &rngs]).execute(&conn); drop(conn);