forge
Explore
Log in

Don't you dare go Hollow

A tree's last-commit walk is cached per commit

Browse files at this commit

ed35470096ab56222b613ee2c5138d05557e1902Ahmet Kaan GümüşParent: e910f7e
All checks have passed
Show checks
  • perf
    • a tree's last-commit walk is cached per commit
    • the code page renders in 5 ms instead of 38 ms after its first view
Show full message
⚡ perf: a tree's last-commit walk is cached per commit; the code page renders in 5 ms instead of 38 ms after its first view
crates/forge-web-pages/src/pages/tree.rsmodified+22 -1
@@ -1,5 +1,6 @@
11 use std::collections::HashMap;
22 use std::path::{Path, PathBuf};
3+use std::sync::{Mutex, OnceLock};
34 use std::time::Duration;
45
56 use forge_core::Repo as RepoRow;
@@ -66,6 +67,13 @@ pub async fn read_file(path: PathBuf, commit: String, file: String) -> Result<Op
6667 .await??)
6768 }
6869
70+type LastCommitCache = Mutex<HashMap<(PathBuf, String), (String, HashMap<String, CommitInfo>)>>;
71+
72+fn last_commit_cache() -> &'static LastCommitCache {
73+ static CACHE: OnceLock<LastCommitCache> = OnceLock::new();
74+ CACHE.get_or_init(|| Mutex::new(HashMap::new()))
75+}
76+
6977 pub async fn last_commits(
7078 state: &AppState,
7179 path: &Path,
@@ -73,9 +81,22 @@ pub async fn last_commits(
7381 dir: &str,
7482 names: &[String],
7583 ) -> HashMap<String, CommitInfo> {
84+ let key = (path.to_path_buf(), dir.to_owned());
85+ if let Some((at, map)) = last_commit_cache().lock().expect("last-commit cache").get(&key)
86+ && at == commit
87+ {
88+ return map.clone();
89+ }
7690 let walk = history::last_commits(&state.git, path, commit, dir, names, LAST_COMMIT_WALK);
7791 match tokio::time::timeout(LAST_COMMIT_TIMEOUT, walk).await {
78- Ok(Ok(map)) => map,
92+ Ok(Ok(map)) => {
93+ let mut cache = last_commit_cache().lock().expect("last-commit cache");
94+ if cache.len() >= 1024 {
95+ cache.clear();
96+ }
97+ cache.insert(key, (commit.to_owned(), map.clone()));
98+ map
99+ }
79100 Ok(Err(e)) => {
80101 tracing::warn!(error = %e, "last-commit walk failed");
81102 HashMap::new()