Browse Source

Add [email protected] in ntex-db bench (#5733)

* Add [email protected] in ntex-db bench

* Remove ntex dependency `markup`

* Update ntex dependency [email protected]

It has been optimized by eliminating unnecessary checks and working directly with `memcopy`
Juan Aguilar 5 years ago
parent
commit
c95dfd4bfa

+ 1 - 3
frameworks/Rust/ntex/Cargo.toml

@@ -18,8 +18,7 @@ path = "src/main_raw.rs"
 [dependencies]
 [dependencies]
 ntex = "0.1.18"
 ntex = "0.1.18"
 snmalloc-rs = "0.2.6"
 snmalloc-rs = "0.2.6"
-markup = "0.4.1"
-yarte = "0.9.1"
+yarte = { version = "0.9.8", features = ["fixed"] }
 serde = "1.0"
 serde = "1.0"
 serde_derive = "1.0"
 serde_derive = "1.0"
 env_logger = "0.7"
 env_logger = "0.7"
@@ -32,7 +31,6 @@ http = "0.2"
 simd-json = "0.3.9"
 simd-json = "0.3.9"
 simd-json-derive = { git = "https://github.com/simd-lite/simd-json-derive.git" }
 simd-json-derive = { git = "https://github.com/simd-lite/simd-json-derive.git" }
 log = { version = "0.4", features = ["release_max_level_off"] }
 log = { version = "0.4", features = ["release_max_level_off"] }
-v_htmlescape = "0.4.5"
 tokio = "=0.2.6"
 tokio = "=0.2.6"
 tokio-postgres = { git="https://github.com/fafhrd91/postgres.git" }
 tokio-postgres = { git="https://github.com/fafhrd91/postgres.git" }
 
 

+ 11 - 4
frameworks/Rust/ntex/src/main_db.rs

@@ -2,7 +2,6 @@
 static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
 static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
 
 
 use std::future::Future;
 use std::future::Future;
-use std::io::Write;
 use std::pin::Pin;
 use std::pin::Pin;
 use std::task::{Context, Poll};
 use std::task::{Context, Poll};
 
 
@@ -14,12 +13,13 @@ use ntex::http::{HttpService, KeepAlive, Request, Response, StatusCode};
 use ntex::service::{Service, ServiceFactory};
 use ntex::service::{Service, ServiceFactory};
 use ntex::web::Error;
 use ntex::web::Error;
 use simd_json_derive::Serialize;
 use simd_json_derive::Serialize;
+use yarte::TemplateFixed;
 
 
 mod db;
 mod db;
 mod utils;
 mod utils;
 
 
 use crate::db::PgConnection;
 use crate::db::PgConnection;
-use crate::utils::{FortunesTemplate, Writer};
+use crate::utils::{FortunesYarteTemplate, Writer};
 
 
 struct App {
 struct App {
     db: PgConnection,
     db: PgConnection,
@@ -64,8 +64,15 @@ impl Service for App {
                 Box::pin(async move {
                 Box::pin(async move {
                     let fortunes = fut.await?;
                     let fortunes = fut.await?;
                     let mut body = BytesMut::with_capacity(2048);
                     let mut body = BytesMut::with_capacity(2048);
-                    let mut writer = Writer(&mut body);
-                    let _ = write!(writer, "{}", FortunesTemplate { fortunes });
+                    unsafe {
+                        // Maybe uninit
+                        body.set_len(2048);
+                        // Before buffer overruns return `None`
+                        let size = FortunesYarteTemplate { fortunes }.call(body.as_mut()).unwrap();
+                        // Bound to init data
+                        body.set_len(size)
+                    };
+
                     let mut res =
                     let mut res =
                         Response::with_body(StatusCode::OK, Body::Bytes(body.freeze()));
                         Response::with_body(StatusCode::OK, Body::Bytes(body.freeze()));
                     let hdrs = res.headers_mut();
                     let hdrs = res.headers_mut();

+ 2 - 24
frameworks/Rust/ntex/src/utils.rs

@@ -4,7 +4,7 @@ use std::{cmp, io};
 use atoi::FromRadix10;
 use atoi::FromRadix10;
 use bytes::BytesMut;
 use bytes::BytesMut;
 use serde_derive::Serialize;
 use serde_derive::Serialize;
-use yarte::Template;
+use yarte::TemplateFixed;
 
 
 #[allow(non_snake_case)]
 #[allow(non_snake_case)]
 #[derive(Serialize, Debug)]
 #[derive(Serialize, Debug)]
@@ -36,29 +36,7 @@ pub fn get_query_param(query: &str) -> u16 {
     cmp::min(500, cmp::max(1, q))
     cmp::min(500, cmp::max(1, q))
 }
 }
 
 
-markup::define! {
-    FortunesTemplate(fortunes: Vec<Fortune>) {
-        {markup::doctype()}
-        html {
-            head {
-                title { "Fortunes" }
-            }
-            body {
-                table {
-                    tr { th { "id" } th { "message" } }
-                    @for item in {fortunes} {
-                        tr {
-                            td { {item.id} }
-                            td { {markup::raw(v_htmlescape::escape(&item.message))} }
-                        }
-                    }
-                }
-            }
-        }
-    }
-}
-
-#[derive(Template)]
+#[derive(TemplateFixed)]
 #[template(path = "fortune.hbs")]
 #[template(path = "fortune.hbs")]
 pub struct FortunesYarteTemplate {
 pub struct FortunesYarteTemplate {
     pub fortunes: Vec<Fortune>,
     pub fortunes: Vec<Fortune>,