From e056e33da5c2a4db1f77cfbf01eac1775dfa13f0 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 8 Aug 2025 19:55:16 +0200 Subject: [PATCH] Improve CPU usage when flicking through large diffs The previous commit already fixed the user-visible lag, but there's still a problem with multiple background git processes consuming resources calculating diffs that we are never going to show. Improve this by terminating those processes (by sending them a TERM signal). Unfortunately this is only possible on Linux and Mac, so Windows users will have to live with the higher CPU usage. The recommended workaround is to not use "diff.algorithm = histogram". --- pkg/commands/oscommands/os_default_platform.go | 10 ++++++++++ pkg/commands/oscommands/os_windows.go | 6 ++++++ pkg/tasks/tasks.go | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/pkg/commands/oscommands/os_default_platform.go b/pkg/commands/oscommands/os_default_platform.go index 5ea872c8c..3d7eae481 100644 --- a/pkg/commands/oscommands/os_default_platform.go +++ b/pkg/commands/oscommands/os_default_platform.go @@ -5,8 +5,10 @@ package oscommands import ( "os" + "os/exec" "runtime" "strings" + "syscall" ) func GetPlatform() *Platform { @@ -38,3 +40,11 @@ func getUserShell() string { func (c *OSCommand) UpdateWindowTitle() error { return nil } + +func TerminateProcessGracefully(cmd *exec.Cmd) error { + if cmd.Process == nil { + return nil + } + + return cmd.Process.Signal(syscall.SIGTERM) +} diff --git a/pkg/commands/oscommands/os_windows.go b/pkg/commands/oscommands/os_windows.go index 0c69ca685..605ed7682 100644 --- a/pkg/commands/oscommands/os_windows.go +++ b/pkg/commands/oscommands/os_windows.go @@ -3,6 +3,7 @@ package oscommands import ( "fmt" "os" + "os/exec" "path/filepath" ) @@ -22,3 +23,8 @@ func (c *OSCommand) UpdateWindowTitle() error { argString := fmt.Sprint("title ", filepath.Base(path), " - Lazygit") return c.Cmd.NewShell(argString, c.UserConfig().OS.ShellFunctionsFile).Run() } + +func TerminateProcessGracefully(cmd *exec.Cmd) error { + // Signals other than SIGKILL are not supported on Windows + return nil +} diff --git a/pkg/tasks/tasks.go b/pkg/tasks/tasks.go index 27fcf817b..5c5875fa8 100644 --- a/pkg/tasks/tasks.go +++ b/pkg/tasks/tasks.go @@ -9,6 +9,7 @@ import ( "time" "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/sasha-s/go-deadlock" "github.com/sirupsen/logrus" @@ -165,6 +166,17 @@ func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), p // and the user is flicking through a bunch of items. self.throttle = time.Since(startTime) < THROTTLE_TIME && timeToStart > COMMAND_START_THRESHOLD + // Kill the still-running command. The only reason to do this is to save CPU usage + // when flicking through several very long diffs when diff.algorithm = histogram is + // being used, in which case multiple git processes continue to calculate expensive + // diffs in the background even though they have been stopped already. + // + // Unfortunately this will do nothing on Windows, so Windows users will have to live + // with the higher CPU usage. + if err := oscommands.TerminateProcessGracefully(cmd); err != nil { + self.Log.Errorf("error when trying to terminate cmd task: %v; Command: %v %v", err, cmd.Path, cmd.Args) + } + // close the task's stdout pipe (or the pty if we're using one) to make the command terminate onDone() }