v0.2.3 - cache micro-optimization

This commit is contained in:
minish 2024-07-12 18:35:09 -04:00
parent 51c4b72626
commit 9109f822eb
Signed by: min
GPG Key ID: FEECFF24EF0CE9E9
5 changed files with 33 additions and 16 deletions

2
Cargo.lock generated
View File

@ -246,7 +246,7 @@ checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
[[package]] [[package]]
name = "breeze" name = "breeze"
version = "0.2.2" version = "0.2.3"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-recursion", "async-recursion",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "breeze" name = "breeze"
version = "0.2.2" version = "0.2.3"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@ -178,9 +178,10 @@ impl Cache {
// drop the reference so we don't deadlock // drop the reference so we don't deadlock
drop(e); drop(e);
// remove it and say we never had it // remove it
self.remove(key); self.remove(key);
// and say we never had it
return None; return None;
} }
@ -230,14 +231,29 @@ impl Cache {
// If we don't do this it'll be a LOT of system api calls // If we don't do this it'll be a LOT of system api calls
let now = SystemTime::now(); let now = SystemTime::now();
// Drop every expired entry // Collect a list of all the expired keys
// If we fail to compare the times, we drop the entry // If we fail to compare the times, it gets added to the list anyways
self.map.retain(|_, e| { let expired: Vec<_> = self
let elapsed = now.duration_since(e.last_used()).unwrap_or(Duration::MAX); .map
let is_expired = elapsed >= e.lifetime; .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))
}
} }
} }
} }

View File

@ -8,14 +8,17 @@ pub async fn index(State(engine): State<Arc<crate::engine::Engine>>) -> String {
let motd = engine.cfg.motd.clone(); let motd = engine.cfg.motd.clone();
motd motd.replace("%version%", env!("CARGO_PKG_VERSION"))
.replace("%version%", env!("CARGO_PKG_VERSION"))
.replace("%uplcount%", &count.to_string()) .replace("%uplcount%", &count.to_string())
} }
pub async fn robots_txt() -> &'static str { pub async fn robots_txt() -> &'static str {
/// robots.txt that tells web crawlers not to list uploads /// 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 ROBOTS_TXT
} }

View File

@ -1,6 +1,4 @@
use std::{ use std::{ffi::OsStr, path::PathBuf, sync::Arc};
ffi::OsStr, path::PathBuf, sync::Arc
};
use axum::{ use axum::{
body::Body, body::Body,