From 9109f822eb19179bcad4c5e4c78fb7c2288e3077 Mon Sep 17 00:00:00 2001 From: minish Date: Fri, 12 Jul 2024 18:35:09 -0400 Subject: [PATCH] v0.2.3 - cache micro-optimization --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cache.rs | 32 ++++++++++++++++++++++++-------- src/index.rs | 9 ++++++--- src/view.rs | 4 +--- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 178ee26..d585118 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -246,7 +246,7 @@ checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" [[package]] name = "breeze" -version = "0.2.2" +version = "0.2.3" dependencies = [ "anyhow", "async-recursion", diff --git a/Cargo.toml b/Cargo.toml index 44267df..aa2a8e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "breeze" -version = "0.2.2" +version = "0.2.3" edition = "2021" [dependencies] diff --git a/src/cache.rs b/src/cache.rs index b6f54c3..20b1567 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -178,9 +178,10 @@ impl Cache { // drop the reference so we don't deadlock drop(e); - // remove it and say we never had it + // remove it self.remove(key); + // and say we never had it return None; } @@ -230,14 +231,29 @@ impl Cache { // If we don't do this it'll be a LOT of system api calls let now = SystemTime::now(); - // Drop every expired entry - // If we fail to compare the times, we drop the entry - self.map.retain(|_, e| { - let elapsed = now.duration_since(e.last_used()).unwrap_or(Duration::MAX); - let is_expired = elapsed >= e.lifetime; + // Collect a list of all the expired keys + // If we fail to compare the times, it gets added to the list anyways + let expired: Vec<_> = self + .map + .par_iter() + .filter_map(|e| { + let elapsed = now.duration_since(e.last_used()).unwrap_or(Duration::MAX); + let is_expired = elapsed >= e.lifetime; - !is_expired - }) + if is_expired { + Some(e.key().clone()) + } else { + None + } + }) + .collect(); + + // If we have any, lock the map and drop all of them + if !expired.is_empty() { + // Use a retain call, should be less locks that way + // (instead of many remove calls) + self.map.retain(|k, _| !expired.contains(k)) + } } } } diff --git a/src/index.rs b/src/index.rs index fe46a10..1441b48 100644 --- a/src/index.rs +++ b/src/index.rs @@ -8,14 +8,17 @@ pub async fn index(State(engine): State>) -> String { let motd = engine.cfg.motd.clone(); - motd - .replace("%version%", env!("CARGO_PKG_VERSION")) + motd.replace("%version%", env!("CARGO_PKG_VERSION")) .replace("%uplcount%", &count.to_string()) } pub async fn robots_txt() -> &'static str { /// robots.txt that tells web crawlers not to list uploads - const ROBOTS_TXT: &str = concat!("User-Agent: *\n", "Disallow: /p/*\n", "Allow: /\n"); + const ROBOTS_TXT: &str = concat!( + "User-Agent: *\n", + "Disallow: /p/*\n", + "Allow: /\n" + ); ROBOTS_TXT } diff --git a/src/view.rs b/src/view.rs index 866f16c..3691c23 100644 --- a/src/view.rs +++ b/src/view.rs @@ -1,6 +1,4 @@ -use std::{ - ffi::OsStr, path::PathBuf, sync::Arc -}; +use std::{ffi::OsStr, path::PathBuf, sync::Arc}; use axum::{ body::Body,