From 33dbb917be1e30d3de9640bec18d0c619a1a7f67 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 5 Mar 2026 23:30:38 +0200 Subject: Reduce flamegraph hot-path allocations --- internal/tui/flamegraph/renderer.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'internal/tui/flamegraph/renderer.go') diff --git a/internal/tui/flamegraph/renderer.go b/internal/tui/flamegraph/renderer.go index 6fba0b6..ad74173 100644 --- a/internal/tui/flamegraph/renderer.go +++ b/internal/tui/flamegraph/renderer.go @@ -14,6 +14,7 @@ import ( ) const pathSeparator = "\x1f" +const pathSeparatorByte = '\x1f' const minFlameWidth = 60 // BuildTerminalLayout converts a live trie snapshot into terminal frame cells. @@ -224,7 +225,7 @@ func renderRow(frames []indexedFrame, width int, selectedPath string, subtreeSet continue } label := padOrTrim(frame.Name, cellWidth) - style := styleForFrame(item.idx, frame, selectedPath, subtreeSet, matchSet, selectedIdx, isDark, searchActive).Width(cellWidth) + style := styleForFrame(item.idx, frame, selectedPath, subtreeSet, matchSet, selectedIdx, isDark, searchActive) cell := style.Render(label) b.WriteString(cell) cursor = frame.Col + cellWidth @@ -255,14 +256,24 @@ func computeSubtreeSetInto(frames []tuiFrame, selectedIdx int, subtree map[int]b for idx, frame := range frames { path := frame.Path if path == selectedPath || - strings.HasPrefix(path, selectedPath+pathSeparator) || - strings.HasPrefix(selectedPath, path+pathSeparator) { + hasPathBoundaryPrefix(path, selectedPath) || + hasPathBoundaryPrefix(selectedPath, path) { subtree[idx] = true } } return subtree } +func hasPathBoundaryPrefix(value, prefix string) bool { + if len(value) <= len(prefix) { + return false + } + if !strings.HasPrefix(value, prefix) { + return false + } + return value[len(prefix)] == pathSeparatorByte +} + func styleForFrame(idx int, frame tuiFrame, selectedPath string, subtreeSet, matchSet map[int]bool, selectedIdx int, isDark, searchActive bool) lipgloss.Style { base := lipgloss.NewStyle(). Foreground(common.ColorBackground). -- cgit v1.2.3