From 1d4057c20907b7d263d6f2b8cb4350a024859dfe Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sun, 14 Aug 2016 00:39:44 +0900 Subject: [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. --- src/util/chars_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/util/chars_test.go (limited to 'src/util/chars_test.go') diff --git a/src/util/chars_test.go b/src/util/chars_test.go new file mode 100644 index 00000000..e42cfb7c --- /dev/null +++ b/src/util/chars_test.go @@ -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() + } +} -- cgit v1.2.3