Skip to content

Commit

Permalink
⚡ perf: reduce the amount of clones, to_owneds & to_strings (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-mmd committed Mar 8, 2024
1 parent 0adbaec commit 6aa9992
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/bin/websurfx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 0 additions & 1 deletion src/cache/redis_cacher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConnectionManager>,
Expand Down
1 change: 0 additions & 1 deletion src/config/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ pub fn run(
) -> std::io::Result<Server> {
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 || {
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 1 addition & 3 deletions src/models/parser_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -40,15 +40,13 @@ 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.
pub random_delay: bool,
}

/// 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,
Expand Down
14 changes: 11 additions & 3 deletions src/results/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -98,13 +99,20 @@ pub async fn aggregate(
// create tasks for upstream result fetching
let tasks: FutureVec = FutureVec::new();

let query: Arc<String> = 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
}));
}
Expand Down
4 changes: 2 additions & 2 deletions src/templates/partials/settings_tabs/engines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn engines(engine_names: &HashMap<String, bool>) -> 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 {
Expand All @@ -64,7 +64,7 @@ pub fn engines(engine_names: &HashMap<String, bool>) -> 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..]))
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/templates/partials/settings_tabs/user_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/templates/views/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
}
}
Expand Down

0 comments on commit 6aa9992

Please sign in to comment.