From 028463bb362df5a6b78b9a0880249aea45fb36f9 Mon Sep 17 00:00:00 2001 From: neon_arch Date: Tue, 29 Aug 2023 20:07:28 +0300 Subject: [PATCH 1/7] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20refactor:=20replace=20?= =?UTF-8?q?jemalloc=20with=20mimalloc=20(#178)(#180)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bin/websurfx.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/bin/websurfx.rs b/src/bin/websurfx.rs index 9aa5b69..bc8e7ce 100644 --- a/src/bin/websurfx.rs +++ b/src/bin/websurfx.rs @@ -3,6 +3,7 @@ //! This module contains the main function which handles the logging of the application to the //! stdout and handles the command line arguments provided and launches the `websurfx` server. +use mimalloc::MiMalloc; use std::net::TcpListener; use websurfx::{config::parser::Config, run}; @@ -11,6 +12,10 @@ use websurfx::{config::parser::Config, run}; #[global_allocator] static ALLOC: dhat::Alloc = dhat::Alloc; +#[cfg(not(feature = "dhat-heap"))] +#[global_allocator] +static GLOBAL: MiMalloc = MiMalloc; + /// The function that launches the main server and registers all the routes of the website. /// /// # Error From 1de52decd39ee46f69ea36ef7665c21d643b2338 Mon Sep 17 00:00:00 2001 From: neon_arch Date: Tue, 29 Aug 2023 20:10:32 +0300 Subject: [PATCH 2/7] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20refactor:=20add=20clon?= =?UTF-8?q?e=20trait=20to=20`RedisCache`=20struct=20(#180)(#178)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cache/cacher.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cache/cacher.rs b/src/cache/cacher.rs index 6932dea..b2508b5 100644 --- a/src/cache/cacher.rs +++ b/src/cache/cacher.rs @@ -17,6 +17,7 @@ use super::error::PoolError; /// * `pool_size` - It stores the size of the connection pool (in other words the number of /// connections that should be stored in the pool). /// * `current_connection` - It stores the index of which connection is being used at the moment. +#[derive(Clone)] pub struct RedisCache { connection_pool: Vec, pool_size: u8, From 7b1f93b232f7ed8a1fb82a3cbe9fea4c6181910b Mon Sep 17 00:00:00 2001 From: neon_arch Date: Tue, 29 Aug 2023 20:12:15 +0300 Subject: [PATCH 3/7] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20refactor:=20add=20comp?= =?UTF-8?q?ress=20middleware=20to=20reduce=20memory=20usage=20(#180)(#178)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index e76344b..97bff01 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,12 @@ use crate::server::routes; use actix_cors::Cors; use actix_files as fs; -use actix_web::{dev::Server, http::header, middleware::Logger, web, App, HttpServer}; +use actix_web::{ + dev::Server, + http::header, + middleware::{Compress, Logger}, + web, App, HttpServer, +}; use config::parser::Config; use handlebars::Handlebars; use handler::paths::{file_path, FileType}; @@ -68,6 +73,7 @@ pub fn run(listener: TcpListener, config: Config) -> std::io::Result { .app_data(web::Data::new(config.clone())) .wrap(cors) .wrap(Logger::default()) // added logging middleware for logging. + .wrap(Compress::default()) // compress request headers to reduce memory usage. // Serve images and static files (css and js files). .service( fs::Files::new("/static", format!("{}/static", public_folder_path)) From 4c298ce18c3aeed7b6a8b9fade8ad7dbd931531e Mon Sep 17 00:00:00 2001 From: neon_arch Date: Tue, 29 Aug 2023 20:15:06 +0300 Subject: [PATCH 4/7] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20refactor:=20add=20code?= =?UTF-8?q?=20to=20initialize=20redis=20cache=20struct=20only=20once=20(#1?= =?UTF-8?q?80)(#178)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/routes.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/server/routes.rs b/src/server/routes.rs index 2fca2bc..8b4a523 100644 --- a/src/server/routes.rs +++ b/src/server/routes.rs @@ -16,6 +16,10 @@ use handlebars::Handlebars; use serde::Deserialize; use tokio::join; +// ---- Constants ---- +/// Initialize redis cache connection once and store it on the heap. +const REDIS_CACHE: async_once_cell::OnceCell = async_once_cell::OnceCell::new(); + /// A named struct which deserializes all the user provided search parameters and stores them. /// /// # Fields @@ -158,10 +162,17 @@ async fn results( page: u32, req: &HttpRequest, ) -> Result> { - //Initialize redis cache connection struct - let mut redis_cache = RedisCache::new(&config.redis_url, 5).await?; + let redis_cache: RedisCache = REDIS_CACHE + .get_or_init(async { + // Initialize redis cache connection pool only one and store it in the heap. + RedisCache::new(&config.redis_url, 5).await.unwrap() + }) + .await + .clone(); + // fetch the cached results json. - let cached_results_json = redis_cache.cached_json(&url).await; + let cached_results_json: Result> = + redis_cache.clone().cached_json(&url).await; // check if fetched cache results was indeed fetched or it was an error and if so // handle the data accordingly. match cached_results_json { @@ -205,6 +216,7 @@ async fn results( }; results.add_style(&config.style); redis_cache + .clone() .cache_results(&serde_json::to_string(&results)?, &url) .await?; Ok(results) From 4157ba8b7fe9039696f6d0f1e7f114b0f5976536 Mon Sep 17 00:00:00 2001 From: neon_arch Date: Tue, 29 Aug 2023 20:16:51 +0300 Subject: [PATCH 5/7] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20refactor:=20add=20new?= =?UTF-8?q?=20crates=20(#180)(#178)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 27 +++++++++++++++++++++++++++ Cargo.toml | 4 +++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 90092b4..5389dea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -312,6 +312,12 @@ version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341" +[[package]] +name = "async-once-cell" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9338790e78aa95a416786ec8389546c4b6a1dfc3dc36071ed9518a9413a542eb" + [[package]] name = "async-trait" version = "0.1.73" @@ -1613,6 +1619,16 @@ version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" +[[package]] +name = "libmimalloc-sys" +version = "0.1.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25d058a81af0d1c22d7a1c948576bee6d673f7af3c0f35564abd6c81122f513d" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "linux-raw-sys" version = "0.4.5" @@ -1741,6 +1757,15 @@ dependencies = [ "autocfg 1.1.0", ] +[[package]] +name = "mimalloc" +version = "0.1.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "972e5f23f6716f62665760b0f4cbf592576a80c7b879ba9beaafc0e558894127" +dependencies = [ + "libmimalloc-sys", +] + [[package]] name = "mime" version = "0.3.17" @@ -3688,6 +3713,7 @@ dependencies = [ "actix-cors", "actix-files", "actix-web", + "async-once-cell", "async-trait", "criterion", "dhat", @@ -3698,6 +3724,7 @@ dependencies = [ "handlebars", "log", "md5", + "mimalloc", "mlua", "rand 0.8.5", "redis", diff --git a/Cargo.toml b/Cargo.toml index 4ed6927..aa075b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,10 +25,12 @@ md5 = {version="0.7.0"} rand={version="0.8.5"} error-stack = {version="0.4.0"} async-trait = {version="0.1.73"} -regex = {version="1.9.3", features=["perf"]} +regex = {version="1.9.4", features=["perf"]} smallvec = {version="1.11.0", features=["union", "serde"]} futures = {version="0.3.28"} dhat = {version="0.3.2", optional = true} +mimalloc = { version = "0.1.38", default-features = false } +async-once-cell = {version="0.5.3"} [dev-dependencies] rusty-hook = "^0.11.2" From b508e6009f5b6165a99505aac6e87404831de91a Mon Sep 17 00:00:00 2001 From: neon_arch Date: Wed, 30 Aug 2023 13:14:53 +0300 Subject: [PATCH 6/7] =?UTF-8?q?=F0=9F=9A=80=20chore:=20bump=20the=20app=20?= =?UTF-8?q?version=20(#180)(#178)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From 55a7125ceae8a17e0ad4359bf205bf8f7f62b28b Mon Sep 17 00:00:00 2001 From: neon_arch Date: Wed, 30 Aug 2023 19:05:38 +0300 Subject: [PATCH 7/7] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20refactor:=20add=20buil?= =?UTF-8?q?d=20time=20binary=20file=20optimization=20(#180)(#178)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 2 +- Cargo.toml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 348340a..b642f3f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,7 +67,7 @@ dependencies = [ "actix-rt", "actix-service", "actix-utils", - "ahash 0.8.3", + "ahash", "base64 0.21.3", "bitflags 2.4.0", "brotli", diff --git a/Cargo.toml b/Cargo.toml index 321891b..4c67969 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,10 +56,10 @@ debug = false # This should only be commented when testing with dhat profiler split-debuginfo = '...' debug-assertions = false overflow-checks = false -lto = 'thin' +lto = true panic = 'abort' incremental = false -codegen-units = 16 +codegen-units = 1 rpath = false strip = "debuginfo"