Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I have a bubbletea app that uses Glamour to render Markdown many times per second, just like in mods. While profiling I noticed that
RGBColor.Sequence()was taking up 27% of the CPU time during Markdown rendering. Most of this was due togo-colorful.Hex(), but this has recently been optimized in the v1.3.0 release, which I created an issue for here #192 .But even after updating
go-colorfulin my termenv fork,RGBColor.Sequence()was still taking up 11% of CPU time. Manually creating the ANSI sequence led to a great speed-up with less allocations. Since the other twoSequence()functions also used the slowfmt.SprintfI rewrote those as well.RGBColor.Sequence(): 2.7x faster, half the allocationsANSI256Color.Sequence(): 5.7x faster, half the allocationsANSIColor.Sequence(): 12.8x faster, 0 allocationsThe benchmarks below show up to three function variants. The originals are labeled
fmt.Sprintf. The functions in this PR are labeled[]byteorstrconv.FormatIntafter their mechanisms of performance gain. There are also results labeledstrings.Builder, which are included in case that style of code is preferred, even if less performant. All implementations in this benchmark can be found here. The benchmark can be found here. If thestrings.Builderstyle is preferred I'd be happy to make a new PR with those implementations.Since the
strings.Builderand the[]byteversions read so similar, I advocate for the[]byteimplementations in this PR, but I understand if none of these fit the code-style. In either case, I think theANSIColor.Sequence()rewrite makes the most sense due to it being a small change and large speed-up.Programs using Glamour would benefit greatly from this PR but surely there are other programs that call
Sequence()in a hot loop.