You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi! I have a bubbletea TUI for LLM chat that spends ~7% of its CPU time in ansi.foregroundColorString while rendering the streamed Markdown responses. Since this may be an important use-case (crush would benefit?), I figured it's worth discussing.
I've made this fork (code+benchmarks) and added multiple alternative implementations to the slowest part of ansi.foregroundColorString. Here is the benchmark of the original vs. alternate implementations:
I realize that most of these are ugly/unidiomatic, but using the fastest implementation brings ansi.foregroundColorString to ~2% CPU time for my TUI.
I can only seriously suggest the LUT (lookup table) implementation. This creates a global [256]string{} array and stores ASCII strings for integers 0-255, computed in init() at startup. It would remove the need for FormatUint(), integer casting, and 3 heap allocations in the *ColorString() funcs. It's much more readable than the other, albeit slightly faster implementations, with very few changes to the file. It's downsides are that it uses ~5kb to store ASCII representations of 0-255 for the life of the program, and there's a negligible startup cost to precompute them. I believe that since style.go:shift() takes care of the 0-255 bounds that this has no impact on behavior.
Let me know if this is something you'd consider and I'd be happy to make a PR.
Cheers
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi! I have a bubbletea TUI for LLM chat that spends ~7% of its CPU time in
ansi.foregroundColorStringwhile rendering the streamed Markdown responses. Since this may be an important use-case (crush would benefit?), I figured it's worth discussing.I've made this fork (code+benchmarks) and added multiple alternative implementations to the slowest part of
ansi.foregroundColorString. Here is the benchmark of the original vs. alternate implementations:I realize that most of these are ugly/unidiomatic, but using the fastest implementation brings
ansi.foregroundColorStringto ~2% CPU time for my TUI.I can only seriously suggest the LUT (lookup table) implementation. This creates a global
[256]string{}array and stores ASCII strings for integers 0-255, computed ininit()at startup. It would remove the need forFormatUint(), integer casting, and 3 heap allocations in the*ColorString()funcs. It's much more readable than the other, albeit slightly faster implementations, with very few changes to the file. It's downsides are that it uses ~5kb to store ASCII representations of 0-255 for the life of the program, and there's a negligible startup cost to precompute them. I believe that sincestyle.go:shift()takes care of the 0-255 bounds that this has no impact on behavior.Let me know if this is something you'd consider and I'd be happy to make a PR.
Cheers
Beta Was this translation helpful? Give feedback.
All reactions