on main help sign in
perf(anim): preallocate strings.Builder capacities
commitcec27925c596a9e04d2e1674b4893e853f87efa1 authorChristian Rocha <christian@rocha.is> dateSat 7 Jun 2025, 09:00 (1 year ago) parentc519c8e
1 file changed+13−2
internal/tui/components/anim/anim.go+13−2
internal/tui/components/anim/anim.go+13−2
@@ -241,8 +241,19 @@ func (a *anim) updateChars(chars *[]cyclingChar) {
241241
242242// View renders the animation.
243243func (a anim) View() tea.View {
244− t := styles.CurrentTheme()
245− var b strings.Builder
244+ var (
245+ t = styles.CurrentTheme()
246+ b strings.Builder
247+ )
248+
249+ // Pre-allocate builder capacity to avoid reallocations.
250+ // Estimate: cycling chars + label chars + ellipsis + style overhead.
251+ const (
252+ bytesPerChar = 20 // ANSI styling
253+ bufferSize = 50 // ellipsis and safety margin
254+ )
255+ estimatedCap := len(a.cyclingChars)*bytesPerChar + len(a.labelChars)*bytesPerChar + bufferSize
256+ b.Grow(estimatedCap)
246257
247258 for i, c := range a.cyclingChars {
248259 if len(a.ramp) > i {