From ed35470096ab56222b613ee2c5138d05557e1902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 07:49:53 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20perf:=20a=20tree's=20last-commit=20?= =?UTF-8?q?walk=20is=20cached=20per=20commit;=20the=20code=20page=20render?= =?UTF-8?q?s=20in=205=20ms=20instead=20of=2038=20ms=20after=20its=20first?= =?UTF-8?q?=20view?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/forge-web-pages/src/pages/tree.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/forge-web-pages/src/pages/tree.rs b/crates/forge-web-pages/src/pages/tree.rs index a42992c..0eea46a 100644 --- a/crates/forge-web-pages/src/pages/tree.rs +++ b/crates/forge-web-pages/src/pages/tree.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; use std::path::{Path, PathBuf}; +use std::sync::{Mutex, OnceLock}; use std::time::Duration; use forge_core::Repo as RepoRow; @@ -66,6 +67,13 @@ pub async fn read_file(path: PathBuf, commit: String, file: String) -> Result)>>; + +fn last_commit_cache() -> &'static LastCommitCache { + static CACHE: OnceLock = OnceLock::new(); + CACHE.get_or_init(|| Mutex::new(HashMap::new())) +} + pub async fn last_commits( state: &AppState, path: &Path, @@ -73,9 +81,22 @@ pub async fn last_commits( dir: &str, names: &[String], ) -> HashMap { + let key = (path.to_path_buf(), dir.to_owned()); + if let Some((at, map)) = last_commit_cache().lock().expect("last-commit cache").get(&key) + && at == commit + { + return map.clone(); + } let walk = history::last_commits(&state.git, path, commit, dir, names, LAST_COMMIT_WALK); match tokio::time::timeout(LAST_COMMIT_TIMEOUT, walk).await { - Ok(Ok(map)) => map, + Ok(Ok(map)) => { + let mut cache = last_commit_cache().lock().expect("last-commit cache"); + if cache.len() >= 1024 { + cache.clear(); + } + cache.insert(key, (commit.to_owned(), map.clone())); + map + } Ok(Err(e)) => { tracing::warn!(error = %e, "last-commit walk failed"); HashMap::new()