mirror of
https://github.com/junegunn/fzf.git
synced 2025-08-09 00:22:02 -07:00
[perf] Avoid allocating rune array for ascii string
In the best case (all ascii), this reduces the memory footprint by 60% and the response time by 15% to 20%. In the worst case (every line has non-ascii characters), 3 to 4% overhead is observed.
This commit is contained in:
36
src/util/chars_test.go
Normal file
36
src/util/chars_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package util
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestToCharsNil(t *testing.T) {
|
||||
bs := Chars{bytes: []byte{}}
|
||||
if bs.bytes == nil || bs.runes != nil {
|
||||
t.Error()
|
||||
}
|
||||
rs := RunesToChars([]rune{})
|
||||
if rs.bytes != nil || rs.runes == nil {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestToCharsAscii(t *testing.T) {
|
||||
chars := ToChars([]byte("foobar"))
|
||||
if chars.ToString() != "foobar" || chars.runes != nil {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestCharsLength(t *testing.T) {
|
||||
chars := ToChars([]byte("\tabc한글 "))
|
||||
if chars.Length() != 8 || chars.TrimLength() != 5 {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestCharsToString(t *testing.T) {
|
||||
text := "\tabc한글 "
|
||||
chars := ToChars([]byte(text))
|
||||
if chars.ToString() != text {
|
||||
t.Error()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user