From dc4d9618da911c1ea80cfec6babb0acf1ac08d2e Mon Sep 17 00:00:00 2001 From: Anton Kolesnikov Date: Fri, 15 Mar 2024 17:46:25 +0800 Subject: [PATCH] perf: optimize tree node insertion --- pkg/model/tree.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/pkg/model/tree.go b/pkg/model/tree.go index 0f13dd3564..9077c7e6e7 100644 --- a/pkg/model/tree.go +++ b/pkg/model/tree.go @@ -63,9 +63,28 @@ func (t *Tree) InsertStack(v int64, stack ...string) { } r := &node{children: t.root} n := r - for j := range stack { + for s := range stack { + name := stack[s] n.total += v - n = n.insert(stack[j]) + // Inlined node.insert + i, j := 0, len(n.children) + for i < j { + h := int(uint(i+j) >> 1) + if n.children[h].name < name { + i = h + 1 + } else { + j = h + } + } + if i < len(n.children) && n.children[i].name == name { + n = n.children[i] + } else { + child := &node{parent: n, name: name} + n.children = append(n.children, child) + copy(n.children[i+1:], n.children[i:]) + n.children[i] = child + n = child + } } // Leaf. n.total += v