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 {
2727 pub full_name: String,
2828 pub target: String, // peeled commit oid
2929 pub kind: RefKind,
30+ pub committer_time: i64, // committer time of the target commit (unix seconds)
3031 }
3132
3233 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -151,11 +152,21 @@ pub fn list_refs(repo: &Path) -> Result<Vec<RefInfo>> {
151152 if !is_commit(&repo, id)? {
152153 continue;
153154 }
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;
154164 out.push(RefInfo {
155165 name,
156166 full_name,
157167 target: id.to_hex().to_string(),
158168 kind,
169+ committer_time,
159170 });
160171 }
161172 }
crates/forge-web/src/pages/ci_runs.rsmodified+3 -0
@@ -2413,18 +2413,21 @@ mod tests {
24132413 full_name: "refs/heads/dev".into(),
24142414 target: "a".into(),
24152415 kind: RefKind::Branch,
2416+ committer_time: 0,
24162417 },
24172418 RefInfo {
24182419 name: "main".into(),
24192420 full_name: "refs/heads/main".into(),
24202421 target: "b".into(),
24212422 kind: RefKind::Branch,
2423+ committer_time: 0,
24222424 },
24232425 RefInfo {
24242426 name: "v1".into(),
24252427 full_name: "refs/tags/v1".into(),
24262428 target: "c".into(),
24272429 kind: RefKind::Tag,
2430+ committer_time: 0,
24282431 },
24292432 ];
24302433 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>> {
3030
3131 /// Branch and tag links for the ref switcher, each with the commit time of its tip. The default
3232 /// branch sorts first, the rest newest first.
33-pub async fn ref_links(
34- path: PathBuf,
35- refs: Vec<RefInfo>,
33+pub fn ref_links(
34+ refs: &[RefInfo],
3635 current: &str,
3736 default_branch: &str,
3837 repo: &RepoRow,
39-) -> Result<(Vec<RefLink>, Vec<RefLink>)> {
40- let mut timed = tokio::task::spawn_blocking(move || -> forge_git::Result<_> {
41- refs.into_iter()
42- .map(|r| {
43- let updated =
44- browse::read_commit(&path, &r.target)?.map_or(0, |c| c.committer_time);
45- Ok((r, updated))
46- })
47- .collect::<forge_git::Result<Vec<(RefInfo, i64)>>>()
48- })
49- .await??;
50- timed.sort_by(|(a, ta), (b, tb)| {
51- (a.name != default_branch, Reverse(*ta), a.name.as_str()).cmp(&(
52- b.name != default_branch,
53- Reverse(*tb),
54- b.name.as_str(),
55- ))
38+) -> (Vec<RefLink>, Vec<RefLink>) {
39+ let mut sorted: Vec<&RefInfo> = refs.iter().collect();
40+ sorted.sort_by(|a, b| {
41+ (
42+ a.name != default_branch,
43+ Reverse(a.committer_time),
44+ a.name.as_str(),
45+ )
46+ .cmp(&(
47+ b.name != default_branch,
48+ Reverse(b.committer_time),
49+ b.name.as_str(),
50+ ))
5651 });
57- let link = |(r, updated): &(RefInfo, i64)| RefLink {
52+ let link = |r: &RefInfo| RefLink {
5853 name: r.name.clone(),
5954 href: repo_href(&repo.owner_name, &repo.name, "tree", &r.name),
6055 current: r.name == current,
61- updated: *updated,
56+ updated: r.committer_time,
6257 };
63- let branches = timed
58+ let branches = sorted
6459 .iter()
65- .filter(|(r, _)| r.kind == RefKind::Branch)
66- .map(link)
60+ .filter(|r| r.kind == RefKind::Branch)
61+ .map(|r| link(r))
6762 .collect();
68- let tags = timed
63+ let tags = sorted
6964 .iter()
70- .filter(|(r, _)| r.kind == RefKind::Tag)
71- .map(link)
65+ .filter(|r| r.kind == RefKind::Tag)
66+ .map(|r| link(r))
7267 .collect();
73- Ok((branches, tags))
68+ (branches, tags)
7469 }
7570
7671 /// 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> {
419419 let branch = repo.default_branch.clone();
420420
421421 let refs = load_refs(ctx.path.clone()).await?;
422- let (branches, tags) =
423- ref_links(ctx.path.clone(), refs.clone(), &branch, &branch, repo).await?;
422+ let (branches, tags) = ref_links(&refs, &branch, &branch, repo);
424423 let mut recent = branches.clone();
425424 recent.sort_by_key(|link| std::cmp::Reverse(link.updated));
426425 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> {
548548 now_unix(),
549549 ctx.viewer_tz(),
550550 );
551- let (branches, tags) = ref_links(
552- ctx.path.clone(),
553- refs.clone(),
554- &ref_name,
555- &repo.default_branch,
556- repo,
557- )
558- .await?;
551+ let (branches, tags) = ref_links(&refs, &ref_name, &repo.default_branch, repo);
559552 let branches_href = repo_href(&repo.owner_name, &repo.name, "branches", "");
560553 let tags_href = repo_href(&repo.owner_name, &repo.name, "tags", "");
561554 let live = follows_branch(&refs, &ref_name)