mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-31 14:24:25 +03:00
Use utils.StringWidth to optimize rendering performance
runewidth.StringWidth is an expensive call, even if the input string is pure ASCII. Improve this by providing a wrapper that short-circuits the call to len if the input is ASCII. Benchmark results show that for non-ASCII strings it makes no noticable difference, but for ASCII strings it provides a more than 200x speedup. BenchmarkStringWidthAsciiOriginal-10 718135 1637 ns/op BenchmarkStringWidthAsciiOptimized-10 159197538 7.545 ns/op BenchmarkStringWidthNonAsciiOriginal-10 486290 2391 ns/op BenchmarkStringWidthNonAsciiOptimized-10 502286 2383 ns/op
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/mattn/go-runewidth"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -250,3 +251,27 @@ func TestRenderDisplayStrings(t *testing.T) {
|
||||
assert.EqualValues(t, test.expectedColumnPositions, columnPositions)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStringWidthAsciiOriginal(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
runewidth.StringWidth("some ASCII string")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStringWidthAsciiOptimized(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
StringWidth("some ASCII string")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStringWidthNonAsciiOriginal(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
runewidth.StringWidth("some non-ASCII string 🍉")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStringWidthNonAsciiOptimized(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
StringWidth("some non-ASCII string 🍉")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user