From 31250f36db73c52dc8702906222a8bb3d74c16ac Mon Sep 17 00:00:00 2001 From: b84330808 Date: Sat, 7 Nov 2026 21:33:49 +0900 Subject: [PATCH] Speed up file tree cursor moves on large layers renderStringTreeBetween walks the tree from the top down to the last visible row. It kept the nodes left to visit in a slice and put each node's children in front of it, so every row copied the whole list of pending siblings. With thousands of entries in one directory (a node_modules or site-packages layer) a page halfway down took seconds and gigabytes of allocations, and the file tree pane renders on every cursor move. Use the slice as a stack instead: push the children in reverse and pop the next node from the end. The visiting order is unchanged. VisibleSize, which CursorDown calls on every key press, now counts the visible nodes without sorting each directory's children first. --- .../cli/internal/ui/tui/filetree/file_tree.go | 34 ++++++++++++------- .../ui/tui/filetree/file_tree_test.go | 30 ++++++++++++++++ 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/cmd/dive/cli/internal/ui/tui/filetree/file_tree.go b/cmd/dive/cli/internal/ui/tui/filetree/file_tree.go index 3f86756..88d8970 100644 --- a/cmd/dive/cli/internal/ui/tui/filetree/file_tree.go +++ b/cmd/dive/cli/internal/ui/tui/filetree/file_tree.go @@ -54,12 +54,12 @@ func (tree *FileTree) renderStringTreeBetween(startRow, stopRow int, showAttribu var params = make([]renderParams, 0) var result string - // visit from the front of the list + // a stack of nodes to visit, the next node to visit is at the end var paramsToVisit = []renderParams{{node: tree.Root, spaces: []bool{}, showCollapsed: false, isLast: false}} for currentRow := 0; len(paramsToVisit) > 0 && currentRow <= stopRow; currentRow++ { - // pop the first node - var currentParams renderParams - currentParams, paramsToVisit = paramsToVisit[0], paramsToVisit[1:] + // pop the next node + currentParams := paramsToVisit[len(paramsToVisit)-1] + paramsToVisit = paramsToVisit[:len(paramsToVisit)-1] // take note of the next nodes to visit later sorter := GetSortOrderStrategy(tree.SortOrder) @@ -93,8 +93,10 @@ func (tree *FileTree) renderStringTreeBetween(startRow, stopRow int, showAttribu isLast: isLast, }) } - // keep the child nodes to visit later - paramsToVisit = append(childParams, paramsToVisit...) + // keep the child nodes to visit later, in reverse so the first child is visited next + for idx := len(childParams) - 1; idx >= 0; idx-- { + paramsToVisit = append(paramsToVisit, childParams[idx]) + } // never process the root node if currentParams.node == tree.Root { @@ -124,10 +126,6 @@ func (tree *FileTree) renderStringTreeBetween(startRow, stopRow int, showAttribu func (tree *FileTree) VisibleSize() int { var size int - visitor := func(node *FileNode) error { - size++ - return nil - } visitEvaluator := func(node *FileNode) bool { if node.Data.FileInfo.IsDir { // we won't visit a collapsed dir, but we need to count it @@ -138,10 +136,20 @@ func (tree *FileTree) VisibleSize() int { } return !node.Data.ViewInfo.Hidden } - err := tree.VisitDepthParentFirst(visitor, visitEvaluator) - if err != nil { - log.WithFields("error", err).Debug("unable to determine visible tree size") + // the count doesn't depend on the order, so walk the children without sorting them at every level + var visit func(node *FileNode) + visit = func(node *FileNode) { + if !visitEvaluator(node) { + return + } + if node != tree.Root { + size++ + } + for _, child := range node.Children { + visit(child) + } } + visit(tree.Root) // don't include root size-- diff --git a/cmd/dive/cli/internal/ui/tui/filetree/file_tree_test.go b/cmd/dive/cli/internal/ui/tui/filetree/file_tree_test.go index fcaf021..2a31f9a 100644 --- a/cmd/dive/cli/internal/ui/tui/filetree/file_tree_test.go +++ b/cmd/dive/cli/internal/ui/tui/filetree/file_tree_test.go @@ -2,6 +2,7 @@ package filetree import ( "fmt" + "strings" "testing" ) @@ -125,6 +126,35 @@ func TestStringBetween(t *testing.T) { } +func TestVisibleSize(t *testing.T) { + tree := NewFileTree() + for _, p := range []string{"/etc/nginx/nginx.conf", "/etc/hosts", "/var/run/systemd", "/var/log/a.log", "/tmp/nonsense"} { + if _, _, err := tree.AddPath(p, FileInfo{}); err != nil { + t.Fatalf("could not setup test: %v", err) + } + } + for _, p := range []string{"/etc", "/etc/nginx", "/var", "/var/run", "/var/log", "/tmp"} { + node, err := tree.GetNode(p) + if err != nil { + t.Fatalf("could not setup test: %v", err) + } + node.Data.FileInfo.IsDir = true + } + nginx, _ := tree.GetNode("/etc/nginx") + nginx.Data.ViewInfo.Collapsed = true + varLog, _ := tree.GetNode("/var/log") + varLog.Data.ViewInfo.Hidden = true + + // the index of the last row: a collapsed dir is a row (its contents are not), a hidden node and its contents are not + rows := strings.Count(tree.String(false), "\n") + if rows != 8 { + t.Fatalf("expected 8 rows, got %d:\n%s", rows, tree.String(false)) + } + if actual := tree.VisibleSize(); actual != rows-1 { + t.Errorf("expected visible size %d, got %d", rows-1, actual) + } +} + func TestRejectPurelyRelativePath(t *testing.T) { tree := NewFileTree() _, _, err := tree.AddPath("./etc/nginx/nginx.conf", FileInfo{})