From 70f77f35011a76645b59f6922b749e7872d841bf Mon Sep 17 00:00:00 2001 From: javalsai Date: Tue, 24 Mar 2026 22:31:12 +0100 Subject: [PATCH] perf: use `const-str`'s `include_asset!` --- Cargo.lock | 59 ++++++++++++++++++++++++++--------- Cargo.toml | 10 ++++-- src/consts.rs | 5 +-- src/server/services/images.rs | 25 ++++++++------- 4 files changed, 67 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 891e1b4..f005ec6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -457,6 +457,23 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "const-macros" +version = "0.1.1" +source = "git+https://git.javalsai.tuxcord.net/rust/const-macros.git#6a37f26eaa6349c18ac7c74b1d3938d287013942" +dependencies = [ + "const-macros-proc", +] + +[[package]] +name = "const-macros-proc" +version = "0.1.0" +source = "git+https://git.javalsai.tuxcord.net/rust/const-macros.git#6a37f26eaa6349c18ac7c74b1d3938d287013942" +dependencies = [ + "magic", + "sha2", +] + [[package]] name = "const-str" version = "1.1.0" @@ -1075,7 +1092,7 @@ checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" [[package]] name = "magic" version = "0.16.7" -source = "git+https://github.com/javalsai/rust-magic.git?branch=dbpaths-clone#c5a357bd482b7625e407c39f5f5af5f2d425a2fb" +source = "git+https://github.com/javalsai/rust-magic.git?branch=dbpaths-clone#211e458a706c728aebffb1a48175b5c6edef3493" dependencies = [ "bitflags", "magic-sys", @@ -1083,9 +1100,9 @@ dependencies = [ [[package]] name = "magic-sys" -version = "0.4.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82b275c342ee5dd5b19e51905dcc092b8fd1bb1187abbc600ac8910bbb15840e" +checksum = "9a7813c89073ddea0d1979d29786cfb5bd5c8bfe19fdede15a9e6e27fe919b23" dependencies = [ "pkg-config", "vcpkg", @@ -1175,6 +1192,7 @@ dependencies = [ "anstyle", "anyhow", "clap", + "const-macros", "const-str", "futures-util", "libc", @@ -1519,9 +1537,9 @@ dependencies = [ [[package]] name = "serde_spanned" -version = "1.0.4" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8bbf91e5a4d6315eee45e704372590b30e260ee83af6639d64557f51b067776" +checksum = "876ac351060d4f882bb1032b6369eb0aef79ad9df1ea8bc404874d8cc3d0cd98" dependencies = [ "serde_core", ] @@ -1549,6 +1567,17 @@ dependencies = [ "digest", ] +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "shlex" version = "1.3.0" @@ -1758,9 +1787,9 @@ dependencies = [ [[package]] name = "toml" -version = "1.0.7+spec-1.1.0" +version = "1.1.0+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd28d57d8a6f6e458bc0b8784f8fdcc4b99a437936056fa122cb234f18656a96" +checksum = "f8195ca05e4eb728f4ba94f3e3291661320af739c4e43779cbdfae82ab239fcc" dependencies = [ "indexmap", "serde_core", @@ -1773,27 +1802,27 @@ dependencies = [ [[package]] name = "toml_datetime" -version = "1.0.1+spec-1.1.0" +version = "1.1.0+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b320e741db58cac564e26c607d3cc1fdc4a88fd36c879568c07856ed83ff3e9" +checksum = "97251a7c317e03ad83774a8752a7e81fb6067740609f75ea2b585b569a59198f" dependencies = [ "serde_core", ] [[package]] name = "toml_parser" -version = "1.0.10+spec-1.1.0" +version = "1.1.0+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7df25b4befd31c4816df190124375d5a20c6b6921e2cad937316de3fccd63420" +checksum = "2334f11ee363607eb04df9b8fc8a13ca1715a72ba8662a26ac285c98aabb4011" dependencies = [ "winnow", ] [[package]] name = "toml_writer" -version = "1.0.7+spec-1.1.0" +version = "1.1.0+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f17aaa1c6e3dc22b1da4b6bba97d066e354c7945cac2f7852d4e4e7ca7a6b56d" +checksum = "d282ade6016312faf3e41e57ebbba0c073e4056dab1232ab1cb624199648f8ed" [[package]] name = "tracing" @@ -1841,9 +1870,9 @@ checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" [[package]] name = "unicode-segmentation" -version = "1.12.0" +version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" +checksum = "da36089a805484bcccfffe0739803392c8298778a2d2f09febf76fac5ad9025b" [[package]] name = "unicode-xid" diff --git a/Cargo.toml b/Cargo.toml index 4f0e049..136fe08 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,11 +25,11 @@ actix-web = "4.13" anstyle = "1.0" anyhow = "1.0" clap = { version = "4.5", features = ["derive"] } +const-macros = { git = "https://git.javalsai.tuxcord.net/rust/const-macros.git", version = "0.1.1" } const-str = { version = "1.1", features = ["proc"] } futures-util = "0.3" libc = "0.2" -# https://github.com/robo9k/rust-magic/issues/434 -magic = { version = "0.16", git = "https://github.com/javalsai/rust-magic.git", branch = "dbpaths-clone" } +magic = "0.16" moka = { version = "0.12", features = ["async-lock", "future"] } serde = { version = "1.0", features = ["derive"] } thiserror = "2.0" @@ -37,13 +37,17 @@ tokio = { version = "1.49", features = ["full"] } toml = "1.0" users = "0.11" +[patch.crates-io] +# https://github.com/robo9k/rust-magic/issues/434 +magic = { git = "https://github.com/javalsai/rust-magic.git", branch = "dbpaths-clone" } + [lints.clippy] cargo = { level = "warn", priority = -1 } multiple_crate_versions = { level = "allow" } # otherwise there's too much correctness = { level = "deny", priority = -1 } nursery = { level = "deny", priority = -1 } -option_if_let_else = { level = "allow" } # I personally sometimes prefer what this prevents +option_if_let_else = { level = "allow" } # I personally sometimes prefer what this prevents pedantic = { level = "deny", priority = -1 } perf = { level = "deny", priority = -1 } diff --git a/src/consts.rs b/src/consts.rs index 788ed25..701e7f9 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -2,6 +2,8 @@ use std::time::Duration; +use const_macros::{file::FileAsset, include_asset}; + /// Max [`moka`] pfp cache capacity pub const MAX_PFP_CACHE_CAPACITY: u64 = 1024; @@ -9,8 +11,7 @@ pub const MAX_PFP_CACHE_CAPACITY: u64 = 1024; pub const MAX_PFP_SIZE: u64 = 8 * 1024 * 1024; // 8 MiB; TODO: might lower, high for prototyping /// Default user image to use if users have none -pub const DEFAULT_USER_PFP: &[u8] = include_bytes!("../assets/default-pfp.png"); -pub const DEFAULT_USER_PFP_MIME: &str = "image/png; charset=binary"; +pub const DEFAULT_USER_PFP: FileAsset = include_asset!("assets/default-pfp.png"); /// Basically [`USER_CACHES_HEADER`] but much longer for the [`DEFAULT_USER_PFP`] pub const DEFAULT_USER_PFP_CACHES_HEADER: &str = crate::utils::web::make_static_cache_header!(Duration::from_hours(2), Duration::from_days(1)); diff --git a/src/server/services/images.rs b/src/server/services/images.rs index 412c5aa..7d620a1 100644 --- a/src/server/services/images.rs +++ b/src/server/services/images.rs @@ -1,6 +1,6 @@ //! Scope for the image get backend. -//! - `/`: Gives the default image. -//! - `/{username}`: Gives username's pfp or redirects to the default image's path (for better +//! - `/default`: Gives the default image. +//! - `/user/{username}`: Gives username's pfp or redirects to the default image's path (for better //! cache control) if there's no image. //! //! Must be scoped at [`ws::IMAGES`] @@ -15,7 +15,7 @@ use actix_web::{ use crate::{ consts::{self, web_scopes as ws}, - server::{self, AppData}, + server::AppData, }; #[must_use] @@ -25,21 +25,18 @@ pub fn make_scope(path: &str) -> actix_web::Scope { .service(get_image) } -#[get("/")] -async fn get_default_image( - data: AppData, - _username: web::Path, -) -> HttpResponse { +#[get("/default")] +async fn get_default_image() -> HttpResponse { HttpResponse::Ok() .insert_header(( header::CACHE_CONTROL, consts::DEFAULT_USER_PFP_CACHES_HEADER, )) - .content_type(consts::DEFAULT_USER_PFP_MIME) - .body(web::Bytes::from_static(consts::DEFAULT_USER_PFP)) + .content_type(consts::DEFAULT_USER_PFP.mime) + .body(web::Bytes::from_static(consts::DEFAULT_USER_PFP.bytes)) } -#[get("/{username}")] +#[get("/user/{username}")] async fn get_image( data: AppData, username: web::Path, @@ -47,7 +44,11 @@ async fn get_image( let cached_pfp = data.cache.get_pfp(username.to_string()).await; cached_pfp.as_ref().map_or_else( - || web::Either::Left(web::Redirect::to(ws::IMAGES).temporary()), + || { + web::Either::Left( + web::Redirect::to(const_str::concat!(ws::IMAGES, "/default")).temporary(), + ) + }, |img| { web::Either::Right( HttpResponse::Ok()