summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-06 16:45:07 +0200
committerPaul Buetow <paul@buetow.org>2026-03-06 16:45:07 +0200
commita639ed055e540d57fc2de6a9e90eac30555515f8 (patch)
treec22bc37fceb0c869ae20747ef1ee60ca2bfbc248 /internal
parent3791264d60df194f144f8b55eeca8f7aa9ea0a6b (diff)
refactor: share trie insertion path logic (task 382)
Diffstat (limited to 'internal')
-rw-r--r--internal/flamegraph/livetrie.go18
-rw-r--r--internal/flamegraph/trie.go15
-rw-r--r--internal/flamegraph/trie_insert.go22
3 files changed, 24 insertions, 31 deletions
diff --git a/internal/flamegraph/livetrie.go b/internal/flamegraph/livetrie.go
index e76a806..794b790 100644
--- a/internal/flamegraph/livetrie.go
+++ b/internal/flamegraph/livetrie.go
@@ -56,23 +56,7 @@ func NewLiveTrie(fields []string, countField string) *LiveTrie {
}
func (lt *LiveTrie) addLocked(frames []string, value uint64) {
- node := lt.root
- for _, frame := range frames {
- if node.childMap == nil {
- node.childMap = make(map[string]*trieNode)
- }
- child, ok := node.childMap[frame]
- if !ok {
- child = &trieNode{
- name: frame,
- childMap: make(map[string]*trieNode),
- }
- node.children = append(node.children, child)
- node.childMap[frame] = child
- }
- node = child
- }
- node.value += value
+ insertTriePath(lt.root, frames, value)
if len(frames) > lt.maxDepth {
lt.maxDepth = len(frames)
}
diff --git a/internal/flamegraph/trie.go b/internal/flamegraph/trie.go
index dbd3de6..022b846 100644
--- a/internal/flamegraph/trie.go
+++ b/internal/flamegraph/trie.go
@@ -24,20 +24,7 @@ func newTrie() *trie {
}
func (t *trie) add(frames []string, value uint64) {
- node := t.root
- for _, frame := range frames {
- child, ok := node.childMap[frame]
- if !ok {
- child = &trieNode{
- name: frame,
- childMap: make(map[string]*trieNode),
- }
- node.children = append(node.children, child)
- node.childMap[frame] = child
- }
- node = child
- }
- node.value += value
+ insertTriePath(t.root, frames, value)
}
func (t *trie) computeTotals() {
diff --git a/internal/flamegraph/trie_insert.go b/internal/flamegraph/trie_insert.go
new file mode 100644
index 0000000..7748b4a
--- /dev/null
+++ b/internal/flamegraph/trie_insert.go
@@ -0,0 +1,22 @@
+package flamegraph
+
+// insertTriePath follows or creates nodes for frames and adds value at the leaf.
+func insertTriePath(root *trieNode, frames []string, value uint64) {
+ node := root
+ for _, frame := range frames {
+ if node.childMap == nil {
+ node.childMap = make(map[string]*trieNode)
+ }
+ child, ok := node.childMap[frame]
+ if !ok {
+ child = &trieNode{
+ name: frame,
+ childMap: make(map[string]*trieNode),
+ }
+ node.children = append(node.children, child)
+ node.childMap[frame] = child
+ }
+ node = child
+ }
+ node.value += value
+}