From 6aa99922a6abc6657288439c8d6c9a384ec8aa65 Mon Sep 17 00:00:00 2001 From: neon_arch Date: Fri, 8 Mar 2024 21:45:03 +0300 Subject: [PATCH] :zap: perf: reduce the amount of clones, to_owneds & to_strings (#486) --- src/bin/websurfx.rs | 2 +- src/cache/redis_cacher.rs | 1 - src/config/parser.rs | 1 - src/lib.rs | 4 +--- src/models/parser_models.rs | 4 +--- src/results/aggregator.rs | 14 +++++++++++--- src/templates/partials/settings_tabs/engines.rs | 4 ++-- .../partials/settings_tabs/user_interface.rs | 8 +++++--- src/templates/views/search.rs | 2 +- 9 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/bin/websurfx.rs b/src/bin/websurfx.rs index c3d8c38b..5abf2ccc 100644 --- a/src/bin/websurfx.rs +++ b/src/bin/websurfx.rs @@ -48,7 +48,7 @@ async fn main() -> std::io::Result<()> { config.port, ); - let listener = TcpListener::bind((config.binding_ip.clone(), config.port))?; + let listener = TcpListener::bind((config.binding_ip.as_str(), config.port))?; run(listener, config, cache)?.await } diff --git a/src/cache/redis_cacher.rs b/src/cache/redis_cacher.rs index a3349964..1dd7cb82 100644 --- a/src/cache/redis_cacher.rs +++ b/src/cache/redis_cacher.rs @@ -11,7 +11,6 @@ const REDIS_PIPELINE_SIZE: usize = 3; /// A named struct which stores the redis Connection url address to which the client will /// connect to. -#[derive(Clone)] pub struct RedisCache { /// It stores a pool of connections ready to be used. connection_pool: Vec, diff --git a/src/config/parser.rs b/src/config/parser.rs index 4494db3f..fa61ce00 100644 --- a/src/config/parser.rs +++ b/src/config/parser.rs @@ -9,7 +9,6 @@ use mlua::Lua; use std::{collections::HashMap, fs, thread::available_parallelism}; /// A named struct which stores the parsed config file options. -#[derive(Clone)] pub struct Config { /// It stores the parsed port number option on which the server should launch. pub port: u16, diff --git a/src/lib.rs b/src/lib.rs index 0979265a..1642d694 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,8 +69,6 @@ pub fn run( ) -> std::io::Result { let public_folder_path: &str = file_path(FileType::Theme)?; - let cloned_config_threads_opt: u8 = config.threads; - let cache = SHARED_CACHE.get_or_init(|| SharedCache::new(cache)); let server = HttpServer::new(move || { @@ -114,7 +112,7 @@ pub fn run( .service(router::settings) // settings page .default_service(web::route().to(router::not_found)) // error page }) - .workers(cloned_config_threads_opt as usize) + .workers(config.threads as usize) // Start server on 127.0.0.1 with the user provided port number. for example 127.0.0.1:8080. .listen(listener)? .run(); diff --git a/src/models/parser_models.rs b/src/models/parser_models.rs index 75a4c138..24b3fd81 100644 --- a/src/models/parser_models.rs +++ b/src/models/parser_models.rs @@ -10,7 +10,7 @@ /// order to allow the deserializing the json back to struct in aggregate function in /// aggregator.rs and create a new struct out of it and then serialize it back to json and pass /// it to the template files. -#[derive(Clone, Default)] +#[derive(Default)] pub struct Style { /// It stores the parsed theme option used to set a theme for the website. pub theme: String, @@ -40,7 +40,6 @@ impl Style { } /// Configuration options for the aggregator. -#[derive(Clone)] pub struct AggregatorConfig { /// It stores the option to whether enable or disable random delays between /// requests. @@ -48,7 +47,6 @@ pub struct AggregatorConfig { } /// Configuration options for the rate limiter middleware. -#[derive(Clone)] pub struct RateLimiter { /// The number of request that are allowed within a provided time limit. pub number_of_requests: u8, diff --git a/src/results/aggregator.rs b/src/results/aggregator.rs index 2759d9b4..657cbbcb 100644 --- a/src/results/aggregator.rs +++ b/src/results/aggregator.rs @@ -12,6 +12,7 @@ use error_stack::Report; use futures::stream::FuturesUnordered; use regex::Regex; use reqwest::{Client, ClientBuilder}; +use std::sync::Arc; use std::time::{SystemTime, UNIX_EPOCH}; use std::{fs::File, io::BufRead}; use std::{ @@ -98,13 +99,20 @@ pub async fn aggregate( // create tasks for upstream result fetching let tasks: FutureVec = FutureVec::new(); + let query: Arc = Arc::new(query.to_string()); for engine_handler in upstream_search_engines { - let (name, search_engine) = engine_handler.to_owned().into_name_engine(); + let (name, search_engine) = engine_handler.clone().into_name_engine(); names.push(name); - let query: String = query.to_owned(); + let query_partially_cloned = query.clone(); tasks.push(tokio::spawn(async move { search_engine - .results(&query, page, user_agent, client, safe_search) + .results( + &query_partially_cloned, + page, + user_agent, + client, + safe_search, + ) .await })); } diff --git a/src/templates/partials/settings_tabs/engines.rs b/src/templates/partials/settings_tabs/engines.rs index 748cc07d..de67ac70 100644 --- a/src/templates/partials/settings_tabs/engines.rs +++ b/src/templates/partials/settings_tabs/engines.rs @@ -55,7 +55,7 @@ pub fn engines(engine_names: &HashMap) -> Markup { input type="checkbox" class="engine" checked; span class="slider round"{} } - (format!("{}{}",engine_name[..1].to_uppercase().to_owned(), engine_name[1..].to_owned())) + (format!("{}{}",&engine_name[..1].to_uppercase(), &engine_name[1..])) } } @else { @@ -64,7 +64,7 @@ pub fn engines(engine_names: &HashMap) -> Markup { input type="checkbox" class="engine"; span class="slider round"{} } - (format!("{}{}",engine_name[..1].to_uppercase().to_owned(), engine_name[1..].to_owned())) + (format!("{}{}",&engine_name[..1], &engine_name[1..])) } } } diff --git a/src/templates/partials/settings_tabs/user_interface.rs b/src/templates/partials/settings_tabs/user_interface.rs index 6916b262..8f685bec 100644 --- a/src/templates/partials/settings_tabs/user_interface.rs +++ b/src/templates/partials/settings_tabs/user_interface.rs @@ -36,7 +36,7 @@ fn style_option_list( } if style_type == "animations" { - style_option_names.push(("".to_owned(), "none".to_owned())) + style_option_names.push((String::default(), "none".to_owned())) } Ok(style_option_names) @@ -83,9 +83,11 @@ pub fn user_interface( "Select the animation for your theme to be used in user interface" } select name="animations"{ + @let default_animation = &String::default(); + @let animation = animation.as_ref().unwrap_or(default_animation); // Sets the user selected animation name from the config file as the first option in the selection list. - option value=(animation.as_ref().unwrap_or(&"".to_owned())){(animation.as_ref().unwrap_or(&"".to_owned()).replace('-'," "))} - @for (k,v) in style_option_list("animations", animation.as_ref().unwrap_or(&"".to_owned()))?{ + option value=(animation){(animation.replace('-'," "))} + @for (k,v) in style_option_list("animations", animation)?{ option value=(k){(v)} } } diff --git a/src/templates/views/search.rs b/src/templates/views/search.rs index 07b8b425..c5ab4564 100644 --- a/src/templates/views/search.rs +++ b/src/templates/views/search.rs @@ -38,7 +38,7 @@ pub fn search( small{(result.url)} p{(PreEscaped(&result.description))} .upstream_engines{ - @for name in result.clone().engine{ + @for name in &result.engine { span{(name)} } }