forked from TrueCloudLab/restic
ui/termstatus: Optimize Truncate
x/text/width.LookupRune has to re-encode its argument as UTF-8, while LookupString operates on the UTF-8 directly. The uint casts get rid of a bounds check. Benchmark results, with b.ResetTimer introduced first: name old time/op new time/op delta TruncateASCII-8 69.7ns ± 1% 55.2ns ± 1% -20.90% (p=0.000 n=20+18) TruncateUnicode-8 350ns ± 1% 171ns ± 1% -51.05% (p=0.000 n=20+19)
This commit is contained in:
parent
49fa8fe6dd
commit
49e32f3f8a
2 changed files with 24 additions and 11 deletions
|
@ -302,26 +302,35 @@ func Truncate(s string, w int) string {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, r := range s {
|
for i := uint(0); i < uint(len(s)); {
|
||||||
|
utfsize := uint(1) // UTF-8 encoding size of first rune in s.
|
||||||
w--
|
w--
|
||||||
if r > unicode.MaxASCII && wideRune(r) {
|
|
||||||
|
if s[i] > unicode.MaxASCII {
|
||||||
|
var wide bool
|
||||||
|
if wide, utfsize = wideRune(s[i:]); wide {
|
||||||
w--
|
w--
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if w < 0 {
|
if w < 0 {
|
||||||
return s[:i]
|
return s[:i]
|
||||||
}
|
}
|
||||||
|
i += utfsize
|
||||||
}
|
}
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// Guess whether r would occupy two terminal cells instead of one.
|
// Guess whether the first rune in s would occupy two terminal cells
|
||||||
// This cannot be determined exactly without knowing the terminal font,
|
// instead of one. This cannot be determined exactly without knowing
|
||||||
// so we treat all ambigous runes as full-width, i.e., two cells.
|
// the terminal font, so we treat all ambigous runes as full-width,
|
||||||
func wideRune(r rune) bool {
|
// i.e., two cells.
|
||||||
kind := width.LookupRune(r).Kind()
|
func wideRune(s string) (wide bool, utfsize uint) {
|
||||||
return kind != width.Neutral && kind != width.EastAsianNarrow
|
prop, size := width.LookupString(s)
|
||||||
|
kind := prop.Kind()
|
||||||
|
wide = kind != width.Neutral && kind != width.EastAsianNarrow
|
||||||
|
return wide, uint(size)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetStatus updates the status lines.
|
// SetStatus updates the status lines.
|
||||||
|
|
|
@ -49,11 +49,15 @@ func BenchmarkTruncateASCII(b *testing.B) {
|
||||||
func BenchmarkTruncateUnicode(b *testing.B) {
|
func BenchmarkTruncateUnicode(b *testing.B) {
|
||||||
s := "Hello World or Καλημέρα κόσμε or こんにちは 世界"
|
s := "Hello World or Καλημέρα κόσμε or こんにちは 世界"
|
||||||
w := 0
|
w := 0
|
||||||
for _, r := range s {
|
for i := 0; i < len(s); {
|
||||||
w++
|
w++
|
||||||
if wideRune(r) {
|
wide, utfsize := wideRune(s[i:])
|
||||||
|
if wide {
|
||||||
w++
|
w++
|
||||||
}
|
}
|
||||||
|
i += int(utfsize)
|
||||||
}
|
}
|
||||||
|
b.ResetTimer()
|
||||||
|
|
||||||
benchmarkTruncate(b, s, w-1)
|
benchmarkTruncate(b, s, w-1)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue