From b18f12ca0f0341df2717691a30880f7ba14df9f9 Mon Sep 17 00:00:00 2001 From: "partho.kunda" Date: Wed, 18 Sep 2024 03:11:54 +0600 Subject: [PATCH] Use map to quickly find children in BuildTreeFromFiles --- pkg/gui/filetree/build_tree.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/pkg/gui/filetree/build_tree.go b/pkg/gui/filetree/build_tree.go index c7c465e28e8..0193ba45c01 100644 --- a/pkg/gui/filetree/build_tree.go +++ b/pkg/gui/filetree/build_tree.go @@ -10,6 +10,8 @@ import ( func BuildTreeFromFiles(files []*models.File) *Node[models.File] { root := &Node[models.File]{} + childrenMapsByNode := make(map[*Node[models.File]]map[string]*Node[models.File]) + var curr *Node[models.File] for _, file := range files { splitPath := split(file.Name) @@ -23,11 +25,19 @@ func BuildTreeFromFiles(files []*models.File) *Node[models.File] { } path := join(splitPath[:i+1]) - for _, existingChild := range curr.Children { - if existingChild.Path == path { - curr = existingChild - continue outer - } + + var currNodeChildrenMap map[string]*Node[models.File] + var isCurrNodeMapped bool + + if currNodeChildrenMap, isCurrNodeMapped = childrenMapsByNode[curr]; !isCurrNodeMapped { + currNodeChildrenMap = make(map[string]*Node[models.File]) + childrenMapsByNode[curr] = currNodeChildrenMap + } + + child, doesCurrNodeHaveChildAlready := currNodeChildrenMap[path] + if doesCurrNodeHaveChildAlready { + curr = child + continue outer } newChild := &Node[models.File]{ @@ -36,6 +46,8 @@ func BuildTreeFromFiles(files []*models.File) *Node[models.File] { } curr.Children = append(curr.Children, newChild) + currNodeChildrenMap[path] = newChild + curr = newChild } }