forge
Explore
Log in

Don't you dare go Hollow

Ref switcher reads tip times in the same pass that lists refs

Browse files at this commit

4c7677f7483da6a943b0925419f565ca30511561Ahmet Kaan GümüşParent: 2c14701
  • perf
    • ref switcher reads tip times in the same pass that lists refs
Show full message
⚡ perf: ref switcher reads tip times in the same pass that lists refs

list_refs already holds the open repo and the peeled commit id, so it
now decodes the committer time there and stores it on RefInfo. The ref
switcher sorts from that field and no longer reopens the repo per ref.
crates/forge-git/src/browse.rsmodified+11 -0
@@ -27,6 +27,7 @@ pub struct RefInfo {
27 pub full_name: String,27 pub full_name: String,
28 pub target: String, // peeled commit oid28 pub target: String, // peeled commit oid
29 pub kind: RefKind,29 pub kind: RefKind,
30+ pub committer_time: i64, // committer time of the target commit (unix seconds)
30 }31 }
31 32
32 #[derive(Debug, Clone, Copy, PartialEq, Eq)]33 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -151,11 +152,21 @@ pub fn list_refs(repo: &Path) -> Result<Vec<RefInfo>> {
151 if !is_commit(&repo, id)? {152 if !is_commit(&repo, id)? {
152 continue;153 continue;
153 }154 }
155+ let commit = repo.find_commit(id).map_err(gerr)?;
156+ let committer_time = commit
157+ .decode()
158+ .map_err(gerr)?
159+ .committer()
160+ .map_err(gerr)?
161+ .time()
162+ .map_err(gerr)?
163+ .seconds;
154 out.push(RefInfo {164 out.push(RefInfo {
155 name,165 name,
156 full_name,166 full_name,
157 target: id.to_hex().to_string(),167 target: id.to_hex().to_string(),
158 kind,168 kind,
169+ committer_time,
159 });170 });
160 }171 }
161 }172 }
crates/forge-web/src/pages/ci_runs.rsmodified+3 -0
@@ -2413,18 +2413,21 @@ mod tests {
2413 full_name: "refs/heads/dev".into(),2413 full_name: "refs/heads/dev".into(),
2414 target: "a".into(),2414 target: "a".into(),
2415 kind: RefKind::Branch,2415 kind: RefKind::Branch,
2416+ committer_time: 0,
2416 },2417 },
2417 RefInfo {2418 RefInfo {
2418 name: "main".into(),2419 name: "main".into(),
2419 full_name: "refs/heads/main".into(),2420 full_name: "refs/heads/main".into(),
2420 target: "b".into(),2421 target: "b".into(),
2421 kind: RefKind::Branch,2422 kind: RefKind::Branch,
2423+ committer_time: 0,
2422 },2424 },
2423 RefInfo {2425 RefInfo {
2424 name: "v1".into(),2426 name: "v1".into(),
2425 full_name: "refs/tags/v1".into(),2427 full_name: "refs/tags/v1".into(),
2426 target: "c".into(),2428 target: "c".into(),
2427 kind: RefKind::Tag,2429 kind: RefKind::Tag,
2430+ committer_time: 0,
2428 },2431 },
2429 ];2432 ];
2430 assert_eq!(branch_choices(&refs, "main"), vec!["main", "dev"]);2433 assert_eq!(branch_choices(&refs, "main"), vec!["main", "dev"]);
crates/forge-web/src/pages/refs.rsmodified+24 -29
@@ -30,47 +30,42 @@ pub async fn load_refs(path: PathBuf) -> Result<Vec<RefInfo>> {
30 30
31 /// Branch and tag links for the ref switcher, each with the commit time of its tip. The default31 /// Branch and tag links for the ref switcher, each with the commit time of its tip. The default
32 /// branch sorts first, the rest newest first.32 /// branch sorts first, the rest newest first.
33-pub async fn ref_links(33+pub fn ref_links(
34- path: PathBuf,34+ refs: &[RefInfo],
35- refs: Vec<RefInfo>,
36 current: &str,35 current: &str,
37 default_branch: &str,36 default_branch: &str,
38 repo: &RepoRow,37 repo: &RepoRow,
39-) -> Result<(Vec<RefLink>, Vec<RefLink>)> {38+) -> (Vec<RefLink>, Vec<RefLink>) {
40- let mut timed = tokio::task::spawn_blocking(move || -> forge_git::Result<_> {39+ let mut sorted: Vec<&RefInfo> = refs.iter().collect();
41- refs.into_iter()40+ sorted.sort_by(|a, b| {
42- .map(|r| {41+ (
43- let updated =42+ a.name != default_branch,
44- browse::read_commit(&path, &r.target)?.map_or(0, |c| c.committer_time);43+ Reverse(a.committer_time),
45- Ok((r, updated))44+ a.name.as_str(),
46- })45+ )
47- .collect::<forge_git::Result<Vec<(RefInfo, i64)>>>()46+ .cmp(&(
48- })47+ b.name != default_branch,
49- .await??;48+ Reverse(b.committer_time),
50- timed.sort_by(|(a, ta), (b, tb)| {49+ b.name.as_str(),
51- (a.name != default_branch, Reverse(*ta), a.name.as_str()).cmp(&(50+ ))
52- b.name != default_branch,
53- Reverse(*tb),
54- b.name.as_str(),
55- ))
56 });51 });
57- let link = |(r, updated): &(RefInfo, i64)| RefLink {52+ let link = |r: &RefInfo| RefLink {
58 name: r.name.clone(),53 name: r.name.clone(),
59 href: repo_href(&repo.owner_name, &repo.name, "tree", &r.name),54 href: repo_href(&repo.owner_name, &repo.name, "tree", &r.name),
60 current: r.name == current,55 current: r.name == current,
61- updated: *updated,56+ updated: r.committer_time,
62 };57 };
63- let branches = timed58+ let branches = sorted
64 .iter()59 .iter()
65- .filter(|(r, _)| r.kind == RefKind::Branch)60+ .filter(|r| r.kind == RefKind::Branch)
66- .map(link)61+ .map(|r| link(r))
67 .collect();62 .collect();
68- let tags = timed63+ let tags = sorted
69 .iter()64 .iter()
70- .filter(|(r, _)| r.kind == RefKind::Tag)65+ .filter(|r| r.kind == RefKind::Tag)
71- .map(link)66+ .map(|r| link(r))
72 .collect();67 .collect();
73- Ok((branches, tags))68+ (branches, tags)
74 }69 }
75 70
76 /// One row of the branch or tag table.71 /// One row of the branch or tag table.
crates/forge-web/src/pages/repo.rsmodified+1 -2
@@ -419,8 +419,7 @@ async fn repo_page(cx: &Cx) -> Result<impl View> {
419 let branch = repo.default_branch.clone();419 let branch = repo.default_branch.clone();
420 420
421 let refs = load_refs(ctx.path.clone()).await?;421 let refs = load_refs(ctx.path.clone()).await?;
422- let (branches, tags) =422+ let (branches, tags) = ref_links(&refs, &branch, &branch, repo);
423- ref_links(ctx.path.clone(), refs.clone(), &branch, &branch, repo).await?;
424 let mut recent = branches.clone();423 let mut recent = branches.clone();
425 recent.sort_by_key(|link| std::cmp::Reverse(link.updated));424 recent.sort_by_key(|link| std::cmp::Reverse(link.updated));
426 recent.truncate(RECENT_BRANCHES);425 recent.truncate(RECENT_BRANCHES);
crates/forge-web/src/pages/tree.rsmodified+1 -8
@@ -548,14 +548,7 @@ async fn tree_page(cx: &Cx) -> Result<impl View> {
548 now_unix(),548 now_unix(),
549 ctx.viewer_tz(),549 ctx.viewer_tz(),
550 );550 );
551- let (branches, tags) = ref_links(551+ let (branches, tags) = ref_links(&refs, &ref_name, &repo.default_branch, repo);
552- ctx.path.clone(),
553- refs.clone(),
554- &ref_name,
555- &repo.default_branch,
556- repo,
557- )
558- .await?;
559 let branches_href = repo_href(&repo.owner_name, &repo.name, "branches", "");552 let branches_href = repo_href(&repo.owner_name, &repo.name, "branches", "");
560 let tags_href = repo_href(&repo.owner_name, &repo.name, "tags", "");553 let tags_href = repo_href(&repo.owner_name, &repo.name, "tags", "");
561 let live = follows_branch(&refs, &ref_name)554 let live = follows_branch(&refs, &ref_name)