From 98eccc9eb800c63292507d5bf5a6d539980f96b9 Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Sun, 20 Oct 2024 20:05:29 -0400 Subject: [PATCH] perf: make `LeptosOptions` lighter-weight to clone (closes #3036) (#3136) --- examples/counter_isomorphic/src/main.rs | 2 +- examples/hackernews/src/main.rs | 2 +- examples/ssr_modes/src/main.rs | 2 +- examples/suspense_tests/src/main.rs | 2 +- examples/tailwind_actix/src/main.rs | 2 +- examples/todo_app_sqlite/src/main.rs | 2 +- leptos/src/hydration/mod.rs | 2 +- leptos_config/Cargo.toml | 2 +- leptos_config/src/lib.rs | 46 ++++++++++++++----------- leptos_config/src/tests.rs | 6 ++-- leptos_config/tests/config.rs | 46 ++++++++++++------------- meta/src/stylesheet.rs | 2 +- 12 files changed, 60 insertions(+), 56 deletions(-) diff --git a/examples/counter_isomorphic/src/main.rs b/examples/counter_isomorphic/src/main.rs index 1a08d51d1..0097dd04f 100644 --- a/examples/counter_isomorphic/src/main.rs +++ b/examples/counter_isomorphic/src/main.rs @@ -63,7 +63,7 @@ async fn main() -> std::io::Result<()> { } }}) - .service(Files::new("/", site_root)) + .service(Files::new("/", site_root.as_ref())) }) .bind(&addr)? .run() diff --git a/examples/hackernews/src/main.rs b/examples/hackernews/src/main.rs index c02b7994e..361e1e0d4 100644 --- a/examples/hackernews/src/main.rs +++ b/examples/hackernews/src/main.rs @@ -56,7 +56,7 @@ async fn main() -> std::io::Result<()> { } }}) - .service(Files::new("/", site_root)) + .service(Files::new("/", site_root.as_ref())) //.wrap(middleware::Compress::default()) }) .bind(&addr)? diff --git a/examples/ssr_modes/src/main.rs b/examples/ssr_modes/src/main.rs index 7a24ec2e4..fc4619162 100644 --- a/examples/ssr_modes/src/main.rs +++ b/examples/ssr_modes/src/main.rs @@ -39,7 +39,7 @@ async fn main() -> std::io::Result<()> { } }}) - .service(Files::new("/", site_root)) + .service(Files::new("/", site_root.as_ref())) //.wrap(middleware::Compress::default()) }) .bind(&addr)? diff --git a/examples/suspense_tests/src/main.rs b/examples/suspense_tests/src/main.rs index 0cd0067bb..72da90961 100644 --- a/examples/suspense_tests/src/main.rs +++ b/examples/suspense_tests/src/main.rs @@ -41,7 +41,7 @@ async fn main() -> std::io::Result<()> { } }}) - .service(Files::new("/", site_root)) + .service(Files::new("/", site_root.as_ref())) }) .bind(addr)? .workers(1) diff --git a/examples/tailwind_actix/src/main.rs b/examples/tailwind_actix/src/main.rs index 3d6e67180..207128053 100644 --- a/examples/tailwind_actix/src/main.rs +++ b/examples/tailwind_actix/src/main.rs @@ -40,7 +40,7 @@ async fn main() -> std::io::Result<()> { } }}) - .service(Files::new("/", site_root)) + .service(Files::new("/", site_root.as_ref())) .wrap(middleware::Compress::default()) }) .bind(&addr)? diff --git a/examples/todo_app_sqlite/src/main.rs b/examples/todo_app_sqlite/src/main.rs index 61953f90d..11a094b6b 100644 --- a/examples/todo_app_sqlite/src/main.rs +++ b/examples/todo_app_sqlite/src/main.rs @@ -59,7 +59,7 @@ async fn main() -> std::io::Result<()> { } }}) - .service(Files::new("/", site_root)) + .service(Files::new("/", site_root.as_ref())) //.wrap(middleware::Compress::default()) }) .bind(&addr)? diff --git a/leptos/src/hydration/mod.rs b/leptos/src/hydration/mod.rs index 075645c82..1a9810095 100644 --- a/leptos/src/hydration/mod.rs +++ b/leptos/src/hydration/mod.rs @@ -50,7 +50,7 @@ pub fn HydrationScripts( path.parent().map(|p| p.to_path_buf()).unwrap_or_default() }) .unwrap_or_default() - .join(&options.hash_file); + .join(options.hash_file.as_ref()); if hash_path.exists() { let hashes = std::fs::read_to_string(&hash_path) .expect("failed to read hash file"); diff --git a/leptos_config/Cargo.toml b/leptos_config/Cargo.toml index 03e6eac18..d7b762323 100644 --- a/leptos_config/Cargo.toml +++ b/leptos_config/Cargo.toml @@ -15,7 +15,7 @@ config = { version = "0.14.0", default-features = false, features = [ "convert-case", ] } regex = "1.10" -serde = { version = "1.0", features = ["derive"] } +serde = { version = "1.0", features = ["derive", "rc"] } thiserror = "1.0" typed-builder = "0.19.1" diff --git a/leptos_config/src/lib.rs b/leptos_config/src/lib.rs index d57cde40c..026d050dc 100644 --- a/leptos_config/src/lib.rs +++ b/leptos_config/src/lib.rs @@ -5,7 +5,9 @@ pub mod errors; use crate::errors::LeptosConfigError; use config::{Case, Config, File, FileFormat}; use regex::Regex; -use std::{env::VarError, fs, net::SocketAddr, path::Path, str::FromStr}; +use std::{ + env::VarError, fs, net::SocketAddr, path::Path, str::FromStr, sync::Arc, +}; use typed_builder::TypedBuilder; /// A Struct to allow us to parse LeptosOptions from the file. Not really needed, most interactions should @@ -25,17 +27,17 @@ pub struct ConfFile { pub struct LeptosOptions { /// The name of the WASM and JS files generated by wasm-bindgen. Defaults to the crate name with underscores instead of dashes #[builder(setter(into), default=default_output_name())] - pub output_name: String, + pub output_name: Arc, /// The path of the all the files generated by cargo-leptos. This defaults to '.' for convenience when integrating with other /// tools. #[builder(setter(into), default=default_site_root())] #[serde(default = "default_site_root")] - pub site_root: String, + pub site_root: Arc, /// The path of the WASM and JS files generated by wasm-bindgen from the root of your app /// By default, wasm-bindgen puts them in `pkg`. #[builder(setter(into), default=default_site_pkg_dir())] #[serde(default = "default_site_pkg_dir")] - pub site_pkg_dir: String, + pub site_pkg_dir: Arc, /// Used to configure the running environment of Leptos. Can be used to load dev constants and keys v prod, or change /// things based on the deployment environment /// I recommend passing in the result of `env::var("LEPTOS_ENV")` @@ -66,11 +68,11 @@ pub struct LeptosOptions { /// The path of a custom 404 Not Found page to display when statically serving content, defaults to `site_root/404.html` #[builder(default = default_not_found_path())] #[serde(default = "default_not_found_path")] - pub not_found_path: String, + pub not_found_path: Arc, /// The file name of the hash text file generated by cargo-leptos. Defaults to `hash.txt`. #[builder(default = default_hash_file_name())] #[serde(default = "default_hash_file_name")] - pub hash_file: String, + pub hash_file: Arc, /// If true, hashes will be generated for all files in the site_root and added to their file names. /// Defaults to `true`. #[builder(default = default_hash_files())] @@ -96,9 +98,9 @@ impl LeptosOptions { ); } Ok(LeptosOptions { - output_name, - site_root: env_w_default("LEPTOS_SITE_ROOT", "target/site")?, - site_pkg_dir: env_w_default("LEPTOS_SITE_PKG_DIR", "pkg")?, + output_name: output_name.into(), + site_root: env_w_default("LEPTOS_SITE_ROOT", "target/site")?.into(), + site_pkg_dir: env_w_default("LEPTOS_SITE_PKG_DIR", "pkg")?.into(), env: env_from_str(env_w_default("LEPTOS_ENV", "DEV")?.as_str())?, site_addr: env_w_default("LEPTOS_SITE_ADDR", "127.0.0.1:3000")? .parse()?, @@ -113,8 +115,10 @@ impl LeptosOptions { reload_ws_protocol: ws_from_str( env_w_default("LEPTOS_RELOAD_WS_PROTOCOL", "ws")?.as_str(), )?, - not_found_path: env_w_default("LEPTOS_NOT_FOUND_PATH", "/404")?, - hash_file: env_w_default("LEPTOS_HASH_FILE_NAME", "hash.txt")?, + not_found_path: env_w_default("LEPTOS_NOT_FOUND_PATH", "/404")? + .into(), + hash_file: env_w_default("LEPTOS_HASH_FILE_NAME", "hash.txt")? + .into(), hash_files: env_w_default("LEPTOS_HASH_FILES", "false")?.parse()?, }) } @@ -126,16 +130,16 @@ impl Default for LeptosOptions { } } -fn default_output_name() -> String { - env!("CARGO_CRATE_NAME").replace('-', "_") +fn default_output_name() -> Arc { + env!("CARGO_CRATE_NAME").replace('-', "_").into() } -fn default_site_root() -> String { - ".".to_string() +fn default_site_root() -> Arc { + ".".into() } -fn default_site_pkg_dir() -> String { - "pkg".to_string() +fn default_site_pkg_dir() -> Arc { + "pkg".into() } fn default_env() -> Env { @@ -150,12 +154,12 @@ fn default_reload_port() -> u32 { 3001 } -fn default_not_found_path() -> String { - "/404".to_string() +fn default_not_found_path() -> Arc { + "/404".into() } -fn default_hash_file_name() -> String { - "hash.txt".to_string() +fn default_hash_file_name() -> Arc { + "hash.txt".into() } fn default_hash_files() -> bool { diff --git a/leptos_config/src/tests.rs b/leptos_config/src/tests.rs index e40a854c5..21d3ddfc9 100644 --- a/leptos_config/src/tests.rs +++ b/leptos_config/src/tests.rs @@ -76,9 +76,9 @@ fn try_from_env_test() { || LeptosOptions::try_from_env().unwrap(), ); - assert_eq!(config.output_name, "app_test"); - assert_eq!(config.site_root, "my_target/site"); - assert_eq!(config.site_pkg_dir, "my_pkg"); + assert_eq!(config.output_name.as_ref(), "app_test"); + assert_eq!(config.site_root.as_ref(), "my_target/site"); + assert_eq!(config.site_pkg_dir.as_ref(), "my_pkg"); assert_eq!( config.site_addr, SocketAddr::from_str("0.0.0.0:80").unwrap() diff --git a/leptos_config/tests/config.rs b/leptos_config/tests/config.rs index 69c9630f4..5198c7b2a 100644 --- a/leptos_config/tests/config.rs +++ b/leptos_config/tests/config.rs @@ -50,9 +50,9 @@ async fn get_configuration_from_file_ok() { ) .await; - assert_eq!(config.output_name, "app-test"); - assert_eq!(config.site_root, "my_target/site"); - assert_eq!(config.site_pkg_dir, "my_pkg"); + assert_eq!(config.output_name.as_ref(), "app-test"); + assert_eq!(config.site_root.as_ref(), "my_target/site"); + assert_eq!(config.site_pkg_dir.as_ref(), "my_pkg"); assert_eq!( config.site_addr, SocketAddr::from_str("0.0.0.0:80").unwrap() @@ -106,9 +106,9 @@ async fn get_config_from_file_ok() { ) .await; - assert_eq!(config.output_name, "app-test"); - assert_eq!(config.site_root, "my_target/site"); - assert_eq!(config.site_pkg_dir, "my_pkg"); + assert_eq!(config.output_name.as_ref(), "app-test"); + assert_eq!(config.site_root.as_ref(), "my_target/site"); + assert_eq!(config.site_pkg_dir.as_ref(), "my_pkg"); assert_eq!( config.site_addr, SocketAddr::from_str("0.0.0.0:80").unwrap() @@ -151,9 +151,9 @@ fn get_config_from_str_content() { || get_config_from_str(CARGO_TOML_CONTENT_OK).unwrap(), ); - assert_eq!(config.output_name, "app-test"); - assert_eq!(config.site_root, "my_target/site"); - assert_eq!(config.site_pkg_dir, "my_pkg"); + assert_eq!(config.output_name.as_ref(), "app-test"); + assert_eq!(config.site_root.as_ref(), "my_target/site"); + assert_eq!(config.site_pkg_dir.as_ref(), "my_pkg"); assert_eq!( config.site_addr, SocketAddr::from_str("0.0.0.0:80").unwrap() @@ -178,9 +178,9 @@ async fn get_config_from_env() { ) .await; - assert_eq!(config.output_name, "app-test"); - assert_eq!(config.site_root, "my_target/site"); - assert_eq!(config.site_pkg_dir, "my_pkg"); + assert_eq!(config.output_name.as_ref(), "app-test"); + assert_eq!(config.site_root.as_ref(), "my_target/site"); + assert_eq!(config.site_pkg_dir.as_ref(), "my_pkg"); assert_eq!( config.site_addr, SocketAddr::from_str("0.0.0.0:80").unwrap() @@ -202,8 +202,8 @@ async fn get_config_from_env() { ) .await; - assert_eq!(config.site_root, "target/site"); - assert_eq!(config.site_pkg_dir, "pkg"); + assert_eq!(config.site_root.as_ref(), "target/site"); + assert_eq!(config.site_pkg_dir.as_ref(), "pkg"); assert_eq!( config.site_addr, SocketAddr::from_str("127.0.0.1:3000").unwrap() @@ -215,10 +215,10 @@ async fn get_config_from_env() { #[test] fn leptos_options_builder_default() { let conf = LeptosOptions::builder().output_name("app-test").build(); - assert_eq!(conf.output_name, "app-test"); + assert_eq!(conf.output_name.as_ref(), "app-test"); assert!(matches!(conf.env, Env::DEV)); - assert_eq!(conf.site_pkg_dir, "pkg"); - assert_eq!(conf.site_root, "."); + assert_eq!(conf.site_pkg_dir.as_ref(), "pkg"); + assert_eq!(conf.site_root.as_ref(), "."); assert_eq!( conf.site_addr, SocketAddr::from_str("127.0.0.1:3000").unwrap() @@ -242,9 +242,9 @@ fn environment_variable_override() { || get_config_from_str(CARGO_TOML_CONTENT_OK).unwrap(), ); - assert_eq!(config.output_name, "app-test"); - assert_eq!(config.site_root, "my_target/site"); - assert_eq!(config.site_pkg_dir, "my_pkg"); + assert_eq!(config.output_name.as_ref(), "app-test"); + assert_eq!(config.site_root.as_ref(), "my_target/site"); + assert_eq!(config.site_pkg_dir.as_ref(), "my_pkg"); assert_eq!( config.site_addr, SocketAddr::from_str("0.0.0.0:80").unwrap() @@ -265,9 +265,9 @@ fn environment_variable_override() { || get_config_from_str(CARGO_TOML_CONTENT_OK).unwrap(), ); - assert_eq!(config.output_name, "app-test2"); - assert_eq!(config.site_root, "my_target/site2"); - assert_eq!(config.site_pkg_dir, "my_pkg2"); + assert_eq!(config.output_name.as_ref(), "app-test2"); + assert_eq!(config.site_root.as_ref(), "my_target/site2"); + assert_eq!(config.site_pkg_dir.as_ref(), "my_pkg2"); assert_eq!( config.site_addr, SocketAddr::from_str("0.0.0.0:82").unwrap() diff --git a/meta/src/stylesheet.rs b/meta/src/stylesheet.rs index 2bb69c07c..caa9ea222 100644 --- a/meta/src/stylesheet.rs +++ b/meta/src/stylesheet.rs @@ -54,7 +54,7 @@ pub fn HashedStylesheet( path.parent().map(|p| p.to_path_buf()).unwrap_or_default() }) .unwrap_or_default() - .join(&options.hash_file); + .join(options.hash_file.as_ref()); if hash_path.exists() { let hashes = std::fs::read_to_string(&hash_path) .expect("failed to read hash file");