Skip to content

Commit ae10076

Browse files
authored
Improve startup time (#5934)
This PR has two separate improvements for the startup time, in particular for the time until the Files panel shows the modified files: - avoid walking the `.git/workspaces` dir recursively, looking for the gitdir files of linked worktrees. This code was not used to populate the worktrees panel, but only for the decision in the Files panel whether to show an entry with the worktree icon; it was doing unnecessary work, because walking the `.git/workspaces` dir recursively is pointless (git stores the worktree gitdir files only at the top level, so a flat read of that directory would have been enough), and can take significant time in large repos, especially when they have many submodules. Instead of fixing that code, remove it entirely and rearrange the refresh code so that we can use the regular worktree model for this Files panel decision. - at startup we were doing two full refreshes at the same time: the regular one that we always do after loading a repo for the first time, and then also a focus-in refresh. I didn't realize that a terminal will send us a focus-in event right at the moment we request these events. Nothing bad happens from doing those two refreshes at the same time, but it slows things down a bit when we have two "git status" calls running concurrently. Both of these together reduce the time it takes for the Files panel to show its files at startup (in my regular work repo with three worktrees and ~30 submodules) from 860ms to 440ms, so almost a factor of two. If you want to measure this in your own repo, here's a small throwaway patch that you can use for that: ```diff diff --git a/pkg/gui/controllers/helpers/refresh_helper.go b/pkg/gui/controllers/helpers/refresh_helper.go index 40ce86e..fbb482f39 100644 --- a/pkg/gui/controllers/helpers/refresh_helper.go +++ b/pkg/gui/controllers/helpers/refresh_helper.go @@ -26,6 +26,11 @@ import ( "github.com/sasha-s/go-deadlock" ) +var ( + applicationStartTime = time.Now() + applicationStartTimeOnce sync.Once +) + type RefreshHelper struct { c *HelperCommon refsHelper *RefsHelper @@ -1213,6 +1218,10 @@ func (self *RefreshHelper) refreshFilesAndSubmodules(captured capturedFilesState self.refreshView(self.c.Contexts().Submodules, env) self.refreshView(self.c.Contexts().Files, env) + applicationStartTimeOnce.Do(func() { + self.c.Log.Infof("Time until first files refresh: %s", time.Since(applicationStartTime)) + }) + return nil } ``` To use it, run `./lazygit -l | grep "first files refresh"` in one terminal, and `lazygit -d` in the one that you want to test. I'm curious about your before/after measurements, feel free to post them below in the comments.
2 parents 1f1fd99 + 312a5f2 commit ae10076

8 files changed

Lines changed: 224 additions & 118 deletions

File tree

‎pkg/commands/git_commands/file_loader.go‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package git_commands
22

33
import (
44
"fmt"
5-
"path/filepath"
65
"strconv"
76
"strings"
87

@@ -91,26 +90,6 @@ func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File
9190

9291
self.setConflictMarkerSizes(files)
9392

94-
// Go through the files to see if any of these files are actually worktrees
95-
// so that we can render them correctly
96-
worktreePaths := linkedWortkreePaths(self.Fs, self.repoPaths.RepoGitDirPath())
97-
for _, file := range files {
98-
for _, worktreePath := range worktreePaths {
99-
absFilePath, err := filepath.Abs(file.Path)
100-
if err != nil {
101-
self.Log.Error(err)
102-
continue
103-
}
104-
if absFilePath == worktreePath {
105-
file.IsWorktree = true
106-
// `git status` renders this worktree as a folder with a trailing slash but we'll represent it as a singular worktree
107-
// If we include the slash, it will be rendered as a folder with a null file inside.
108-
file.Path = strings.TrimSuffix(file.Path, "/")
109-
break
110-
}
111-
}
112-
}
113-
11493
return files
11594
}
11695

‎pkg/commands/git_commands/repo_paths.go‎

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package git_commands
22

33
import (
4-
ioFs "io/fs"
54
"os"
65
"path/filepath"
76
"strings"
@@ -10,7 +9,6 @@ import (
109
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
1110
"github.com/jesseduffield/lazygit/pkg/env"
1211
"github.com/jesseduffield/lazygit/pkg/utils"
13-
"github.com/spf13/afero"
1412
)
1513

1614
type RepoPaths struct {
@@ -302,41 +300,3 @@ func runGitRevParse(gitCmd *oscommands.CmdObj) (string, error) {
302300
}
303301
return strings.TrimSpace(res), nil
304302
}
305-
306-
// Returns the paths of linked worktrees
307-
func linkedWortkreePaths(fs afero.Fs, repoGitDirPath string) []string {
308-
result := []string{}
309-
// For each directory in this path we're going to cat the `gitdir` file and append its contents to our result
310-
// That file points us to the `.git` file in the worktree.
311-
worktreeGitDirsPath := filepath.Join(repoGitDirPath, "worktrees")
312-
313-
// ensure the directory exists
314-
_, err := fs.Stat(worktreeGitDirsPath)
315-
if err != nil {
316-
return result
317-
}
318-
319-
_ = afero.Walk(fs, worktreeGitDirsPath, func(currPath string, info ioFs.FileInfo, err error) error {
320-
if err != nil {
321-
return err
322-
}
323-
324-
if !info.IsDir() {
325-
return nil
326-
}
327-
328-
gitDirPath := filepath.Join(currPath, "gitdir")
329-
gitDirBytes, err := afero.ReadFile(fs, gitDirPath)
330-
if err != nil {
331-
// ignoring error
332-
return nil
333-
}
334-
trimmedGitDir := strings.TrimSpace(string(gitDirBytes))
335-
// removing the .git part
336-
worktreeDir := filepath.Dir(trimmedGitDir)
337-
result = append(result, worktreeDir)
338-
return nil
339-
})
340-
341-
return result
342-
}

‎pkg/gocui/gui.go‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ type Gui struct {
222222
// worker goroutines, so it's atomic.
223223
uiThreadID atomic.Int64
224224

225+
// focused says whether the terminal we're running in has focus, as far as
226+
// its focus reports tell us (see IsFocused). Written by the event loop,
227+
// readable from anywhere, so it's atomic.
228+
focused atomic.Bool
229+
225230
// blockInputCount, when greater than zero, withholds keyboard input from
226231
// the handlers: key events are buffered into bufferedKeyEvents and replayed
227232
// once the count drops back to zero, while mouse clicks and hover are
@@ -306,6 +311,12 @@ func NewGui(opts NewGuiOpts) (*Gui, error) {
306311
// runs during startup, before we reach MainLoop.
307312
g.uiThreadID.Store(goid.Get())
308313

314+
// Assume we start out focused: a terminal that supports focus reports sends
315+
// one for the state it is already in when we turn reporting on in MainLoop,
316+
// and passing that on as a change would have the app react to a change that
317+
// never happened.
318+
g.focused.Store(true)
319+
309320
return g, nil
310321
}
311322

@@ -2009,7 +2020,21 @@ func (g *Gui) execKeybinding(v *View, kb *keybinding) error {
20092020
return nil
20102021
}
20112022

2023+
// IsFocused reports whether the terminal we're running in has focus. Terminals
2024+
// that don't report focus at all leave this true for good.
2025+
func (g *Gui) IsFocused() bool {
2026+
return g.focused.Load()
2027+
}
2028+
20122029
func (g *Gui) onFocus(ev *GocuiEvent) error {
2030+
// Terminals report their focus state when we turn focus reporting on, and
2031+
// some report it again when their window is activated, so only pass on the
2032+
// reports that actually change it.
2033+
if ev.Focused == g.focused.Load() {
2034+
return nil
2035+
}
2036+
g.focused.Store(ev.Focused)
2037+
20132038
if g.focusHandler != nil {
20142039
return g.focusHandler(ev.Focused)
20152040
}

0 commit comments

Comments
 (0)