diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-13 14:41:18 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-13 14:41:18 +0300 |
| commit | d392eebe5bd127e1573734321b0cabaad4182d7c (patch) | |
| tree | e6e0b38ba26110411d80e00b224640c26b8110ae /internal/tui/dashboard | |
| parent | de6b9c4741dea87ce66e0309bac580030490dc30 (diff) | |
perf: replace string += concatenation with strings.Builder in TUI render hot paths
Swap out ad-hoc += string concatenation in the flamegraph toolbar/status
lines, dashboard filter summary, bubble/treemap status lines, eventstream
view, processes tab, and probes list for strings.Builder, eliminating
redundant allocations on every render tick. Also update dashboard/model_test.go
fake SnapshotSource implementations to match the updated interface signature.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/tui/dashboard')
| -rw-r--r-- | internal/tui/dashboard/bubbles.go | 13 | ||||
| -rw-r--r-- | internal/tui/dashboard/model.go | 27 | ||||
| -rw-r--r-- | internal/tui/dashboard/model_test.go | 8 | ||||
| -rw-r--r-- | internal/tui/dashboard/processes.go | 6 | ||||
| -rw-r--r-- | internal/tui/dashboard/tabs.go | 7 | ||||
| -rw-r--r-- | internal/tui/dashboard/treemap.go | 12 |
6 files changed, 51 insertions, 22 deletions
diff --git a/internal/tui/dashboard/bubbles.go b/internal/tui/dashboard/bubbles.go index f4fa6d5..8f9b745 100644 --- a/internal/tui/dashboard/bubbles.go +++ b/internal/tui/dashboard/bubbles.go @@ -542,14 +542,19 @@ func (c *bubbleChart) statusLine(width int) string { } node := c.nodes[c.selected] metricText := fmt.Sprintf("%s=%s", c.metricLabel(), c.formatMetricValue(node)) - base := fmt.Sprintf("sel:%d/%d %s | %s | bytes=%s", c.selected+1, len(c.nodes), node.Label, metricText, formatBytes(float64(node.Bytes))) + // Use a Builder to avoid extra allocations for the optional hint/detail suffixes + // that are appended conditionally on every render. + var b strings.Builder + b.WriteString(fmt.Sprintf("sel:%d/%d %s | %s | bytes=%s", c.selected+1, len(c.nodes), node.Label, metricText, formatBytes(float64(node.Bytes)))) if c.statusHint != "" { - base += " | " + c.statusHint + b.WriteString(" | ") + b.WriteString(c.statusHint) } if node.Detail != "" { - base += " | " + node.Detail + b.WriteString(" | ") + b.WriteString(node.Detail) } - return padOrTrim(base, width) + return padOrTrim(b.String(), width) } func (c *bubbleChart) metricLabel() string { diff --git a/internal/tui/dashboard/model.go b/internal/tui/dashboard/model.go index 2535a90..8a3c5d4 100644 --- a/internal/tui/dashboard/model.go +++ b/internal/tui/dashboard/model.go @@ -27,14 +27,18 @@ const dashboardHelpHintRows = 1 const dashboardExpandedHelpRows = 2 const dashboardTabBarRows = 1 -// SnapshotSource is the dashboard data source. +// SnapshotSource is the dashboard data source. Snapshot returns nil, nil when +// the engine is nil. A non-nil error indicates that snapshot construction +// failed and the caller should discard the result. type SnapshotSource interface { - Snapshot() *statsengine.Snapshot + Snapshot() (*statsengine.Snapshot, error) } +// resettableSnapshotSource extends SnapshotSource with a Reset method that +// clears accumulated state and restarts the series baselines. type resettableSnapshotSource interface { Reset() - Snapshot() *statsengine.Snapshot + Snapshot() (*statsengine.Snapshot, error) } type refreshTickMsg struct{} @@ -1105,15 +1109,22 @@ func (m Model) View() tea.View { } func (m Model) filterSummary() string { - summary := "filter: " + presenter.FilterSummary(m.globalFilter) + // Use a Builder to avoid repeated string copies for the optional suffix segments + // (filter stack, recording status, auto-reset label) on every render tick. + var b strings.Builder + b.WriteString("filter: ") + b.WriteString(presenter.FilterSummary(m.globalFilter)) if len(m.filterStack) > 0 { - summary += " | stack: " + strings.Join(m.filterStack, " | ") + b.WriteString(" | stack: ") + b.WriteString(strings.Join(m.filterStack, " | ")) } if m.recordingStatus != "" { - summary += " | " + m.recordingStatus + b.WriteString(" | ") + b.WriteString(m.recordingStatus) } - summary += " | " + m.autoResetStatus() - return summary + b.WriteString(" | ") + b.WriteString(m.autoResetStatus()) + return b.String() } // autoResetStatus is the human-readable label for the current diff --git a/internal/tui/dashboard/model_test.go b/internal/tui/dashboard/model_test.go index 4ca10c9..59c2155 100644 --- a/internal/tui/dashboard/model_test.go +++ b/internal/tui/dashboard/model_test.go @@ -24,9 +24,9 @@ type fakeSnapshotSource struct { snap *statsengine.Snapshot } -func (f *fakeSnapshotSource) Snapshot() *statsengine.Snapshot { +func (f *fakeSnapshotSource) Snapshot() (*statsengine.Snapshot, error) { f.snapshots++ - return f.snap + return f.snap, nil } type fakeResettableSnapshotSource struct { @@ -39,9 +39,9 @@ func (f *fakeResettableSnapshotSource) Reset() { f.resetCount++ } -func (f *fakeResettableSnapshotSource) Snapshot() *statsengine.Snapshot { +func (f *fakeResettableSnapshotSource) Snapshot() (*statsengine.Snapshot, error) { f.snapCount++ - return f.snap + return f.snap, nil } func stripANSIEscape(value string) string { diff --git a/internal/tui/dashboard/processes.go b/internal/tui/dashboard/processes.go index 34fdbc8..f4eedec 100644 --- a/internal/tui/dashboard/processes.go +++ b/internal/tui/dashboard/processes.go @@ -42,7 +42,11 @@ func renderProcessesWithSort(snap *statsengine.Snapshot, width, height, offset, columns := processColumns() out := renderSelectableTable(columns, rows, height, offset, selectedCol, "enter:filter", "s/S:sort", processSortHint(sortState), "v:mode", "b:metric") if pidFilter > 0 { - out += "\n" + "Note: this tab is most useful with All PIDs." + // Use a Builder to avoid an extra allocation for the PID-filter note suffix. + var b strings.Builder + b.WriteString(out) + b.WriteString("\nNote: this tab is most useful with All PIDs.") + return b.String() } return out } diff --git a/internal/tui/dashboard/tabs.go b/internal/tui/dashboard/tabs.go index 0e9d924..8fc5132 100644 --- a/internal/tui/dashboard/tabs.go +++ b/internal/tui/dashboard/tabs.go @@ -228,7 +228,12 @@ func renderTabBarPlain(active Tab, width int) string { text = truncatePlain(text, width) padding := width - utf8.RuneCountInString(text) if padding > 0 { - text += strings.Repeat(" ", padding) + // Use a Builder to avoid a redundant allocation when right-padding to width. + var b strings.Builder + b.Grow(len(text) + padding) + b.WriteString(text) + b.WriteString(strings.Repeat(" ", padding)) + return b.String() } } return text diff --git a/internal/tui/dashboard/treemap.go b/internal/tui/dashboard/treemap.go index 03c2917..4d5486a 100644 --- a/internal/tui/dashboard/treemap.go +++ b/internal/tui/dashboard/treemap.go @@ -438,7 +438,10 @@ func treemapStatusLine(items []syscallTreemapItem, selected int, metric bubbleMe default: metricText = fmt.Sprintf("%d", item.Count) } - status := fmt.Sprintf( + // Use a Builder to avoid a redundant allocation for the optional detail suffix + // appended conditionally on every render call. + var b strings.Builder + b.WriteString(fmt.Sprintf( "sel:%d/%d %s | %s=%s | bytes=%s", selected+1, len(items), @@ -446,11 +449,12 @@ func treemapStatusLine(items []syscallTreemapItem, selected int, metric bubbleMe treemapMetricLabel(metric), metricText, formatBytes(float64(item.Bytes)), - ) + )) if detail := strings.TrimSpace(item.Detail); detail != "" { - status += " | " + detail + b.WriteString(" | ") + b.WriteString(detail) } - return status + return b.String() } func treemapMetricLabel(metric bubbleMetric) string { |
