From cec27925c596a9e04d2e1674b4893e853f87efa1 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Sat, 7 Jun 2025 09:00:33 -0400 Subject: [PATCH] perf(anim): preallocate strings.Builder capacities --- internal/tui/components/anim/anim.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/internal/tui/components/anim/anim.go b/internal/tui/components/anim/anim.go index 3a7b615f6b09a7b8c3a6fac5909cccc6eccb4bb3..c39de0d899a1b4eaf3896ea32b02883374af1195 100644 --- a/internal/tui/components/anim/anim.go +++ b/internal/tui/components/anim/anim.go @@ -241,8 +241,19 @@ func (a *anim) updateChars(chars *[]cyclingChar) { // View renders the animation. func (a anim) View() tea.View { - t := styles.CurrentTheme() - var b strings.Builder + var ( + t = styles.CurrentTheme() + b strings.Builder + ) + + // Pre-allocate builder capacity to avoid reallocations. + // Estimate: cycling chars + label chars + ellipsis + style overhead. + const ( + bytesPerChar = 20 // ANSI styling + bufferSize = 50 // ellipsis and safety margin + ) + estimatedCap := len(a.cyclingChars)*bytesPerChar + len(a.labelChars)*bytesPerChar + bufferSize + b.Grow(estimatedCap) for i, c := range a.cyclingChars { if len(a.ramp) > i {