Skip to content

Improve performance of showDivergenceFromBaseBranch#5536

Merged
stefanhaller merged 1 commit into
jesseduffield:masterfrom
AntoineGS:ahead-behind
Apr 27, 2026
Merged

Improve performance of showDivergenceFromBaseBranch#5536
stefanhaller merged 1 commit into
jesseduffield:masterfrom
AntoineGS:ahead-behind

Conversation

@AntoineGS

@AntoineGS AntoineGS commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

PR Description

Instead of manually figuring out how much ahead each branch is from it's upstream branch, use for-each-ref ahead-behind if supported by the git binary.
On my Windows machine, using the lazygit repo with all 61 branches created locally, it goes from ~8.5s to ~200ms.

Before:
image

After:
image

It was also able to find a base branch for assets which the previous iteration did not.

Fixes #5532

Please check if the PR fulfills these requirements

  • Cheatsheets are up-to-date (run go generate ./...)
  • Code has been formatted (see here)
  • Tests have been added/updated (see here for the integration test guide)
  • Text is internationalised (see here)
  • If a new UserConfig entry was added, make sure it can be hot-reloaded (see here)
  • Docs have been updated if necessary
  • You've read through your own file changes for silly mistakes etc

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 2 duplication

Metric Results
Duplication 2

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@stefanhaller stefanhaller left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, this is a welcome improvement (although on my machine the difference isn't nearly as big; 700ms before vs. 300ms after).

I pushed a whole bunch of fixup commits for minor style things; they all have commit messages, please have a look.

The assets branch is an interesting case: it has no common history with master, so it's a bit questionable to show a behind value that is the entire length of master. On the other hand, showing a behind of 0 like we did before makes it look like it was up to date with master, which is also misleading. So maybe the new behavior is a little bit better, but I think the scenario is uncommon enough that it probably doesn't matter much.

Comment thread pkg/commands/git_commands/branch_loader.go Outdated
Comment thread pkg/commands/git_commands/branch_loader_test.go Outdated
@AntoineGS

AntoineGS commented Apr 26, 2026

Copy link
Copy Markdown
Contributor Author

Nice work, this is a welcome improvement (although on my machine the difference isn't nearly as big; 700ms before vs. 300ms after).

I pushed a whole bunch of fixup commits for minor style things; they all have commit messages, please have a look.

The assets branch is an interesting case: it has no common history with master, so it's a bit questionable to show a behind value that is the entire length of master. On the other hand, showing a behind of 0 like we did before makes it look like it was up to date with master, which is also misleading. So maybe the new behavior is a little bit better, but I think the scenario is uncommon enough that it probably doesn't matter much.

That's a good point, the only better indicator I can think of is putting something like NA when that happens. Though as you say it is a pretty specific edge case.

My guess is the performance difference is due to Windows being slower and getting slower with each version 😂
Thank you for the commits! I've glanced over them and will look at them in more detail, there are some patterns I tend to use that don't fit in this repo and I had not given it more thought so I can adjust future work (like continue in a loop, which I know is often not something like to see).

@AntoineGS

Copy link
Copy Markdown
Contributor Author

Changes done! though I was not able to replicate the issue I had initially found, it was likely caused by manipulations I had done to the original code when prototyping.

@stefanhaller stefanhaller left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like continue in a loop

I was actually torn on that one, and it's a bit of a matter of taste in this particular case. Many go devs follow the rule that the happy path should always have the least indentation, and my fixup violates that. (Shrug)

Anyway, looks great now, thanks.

Comment on lines +313 to +312
branchByRef := lo.KeyBy(branches, func(b *models.Branch) string {
return b.FullRefName()
})
branchByRef := lo.KeyBy(branches, (*models.Branch).FullRefName)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, thanks for fixing this up. 😊

Comment on lines +322 to +325
// Branches not in parse are default to 0
for _, branch := range branchByRef {
branch.BehindBaseBranch.Store(0)
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this turned out easier than I thought.

Greatly improves performance for showDivergenceFromBaseBranch config
Fixes jesseduffield#5532
@stefanhaller stefanhaller changed the title improve performance of showDivergenceFromBaseBranch Improve performance of showDivergenceFromBaseBranch Apr 27, 2026
@stefanhaller stefanhaller enabled auto-merge April 27, 2026 05:11
@stefanhaller stefanhaller merged commit b4f7985 into jesseduffield:master Apr 27, 2026
13 checks passed
@AntoineGS AntoineGS deleted the ahead-behind branch April 28, 2026 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance of showDivergenceFromBaseBranch

2 participants