mirror of
https://github.com/restic/restic.git
synced 2025-10-10 06:21:22 +00:00
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:
@@ -49,11 +49,15 @@ func BenchmarkTruncateASCII(b *testing.B) {
|
||||
func BenchmarkTruncateUnicode(b *testing.B) {
|
||||
s := "Hello World or Καλημέρα κόσμε or こんにちは 世界"
|
||||
w := 0
|
||||
for _, r := range s {
|
||||
for i := 0; i < len(s); {
|
||||
w++
|
||||
if wideRune(r) {
|
||||
wide, utfsize := wideRune(s[i:])
|
||||
if wide {
|
||||
w++
|
||||
}
|
||||
i += int(utfsize)
|
||||
}
|
||||
b.ResetTimer()
|
||||
|
||||
benchmarkTruncate(b, s, w-1)
|
||||
}
|
||||
|
Reference in New Issue
Block a user