on main help sign in
perf(anim): preallocate strings.Builder capacities
cec2792Christian Rocha
internal/tui/components/anim/anim.go+13−2
@@ -241,8 +241,19 @@ func (a *anim) updateChars(chars *[]cyclingChar) {
241
241
242// View renders the animation.
242// View renders the animation.
243func (a anim) View() tea.View {
243func (a anim) View() tea.View {
244+ var (
244− t := styles.CurrentTheme()
245+ t = styles.CurrentTheme()
245− var b strings.Builder
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)
246
257
247 for i, c := range a.cyclingChars {
258 for i, c := range a.cyclingChars {
248 if len(a.ramp) > i {
259 if len(a.ramp) > i {