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/util.go | 29 ----------------------------- 1 file changed, 29 deletions(-) (limited to 'src/util/util.go') diff --git a/src/util/util.go b/src/util/util.go index 4f3d409d..90cc28b4 100644 --- a/src/util/util.go +++ b/src/util/util.go @@ -7,7 +7,6 @@ import ( "os" "os/exec" "time" - "unicode/utf8" ) // Max returns the largest integer @@ -84,34 +83,6 @@ func IsTty() bool { return int(C.isatty(C.int(os.Stdin.Fd()))) != 0 } -// TrimRight returns rune array with trailing white spaces cut off -func TrimRight(runes []rune) []rune { - var i int - for i = len(runes) - 1; i >= 0; i-- { - char := runes[i] - if char != ' ' && char != '\t' { - break - } - } - return runes[0 : i+1] -} - -// BytesToRunes converts byte array into rune array -func BytesToRunes(bytea []byte) []rune { - runes := make([]rune, 0, len(bytea)) - for i := 0; i < len(bytea); { - if bytea[i] < utf8.RuneSelf { - runes = append(runes, rune(bytea[i])) - i++ - } else { - r, sz := utf8.DecodeRune(bytea[i:]) - i += sz - runes = append(runes, r) - } - } - return runes -} - // TrimLen returns the length of trimmed rune array func TrimLen(runes []rune) int { var i int -- cgit v1.2.3