mirror of
https://github.com/vbatts/go-mtree.git
synced 2025-10-04 04:31:00 +00:00
unvis: improve performance by reducing allocations
By using a buffer, we can avoid a bunch of small allocations that the previous implementation did. Based on a few small benchmarks, the performance improvement is very stark (~3x faster for strings that don't require any escaping, and ~20% faster for multi-byte utf8 strings): goos: linux goarch: amd64 pkg: github.com/vbatts/go-mtree/pkg/govis cpu: AMD Ryzen 7 7840U w/ Radeon 780M Graphics │ before │ after │ │ sec/op │ sec/op vs base │ Unvis/NoChange-16 1501.0n ± 0% 497.7n ± 1% -66.84% (p=0.000 n=10) Unvis/Binary-16 1317.5n ± 3% 934.9n ± 9% -29.04% (p=0.000 n=10) Unvis/ASCII-16 1325.5n ± 1% 616.8n ± 1% -53.47% (p=0.000 n=10) Unvis/German-16 1884.5n ± 1% 986.9n ± 2% -47.63% (p=0.000 n=10) Unvis/Russian-16 4.636µ ± 1% 3.796µ ± 1% -18.11% (p=0.000 n=10) Unvis/Japanese-16 3.453µ ± 1% 2.867µ ± 1% -16.99% (p=0.000 n=10) geomean 2.072µ 1.206µ -41.77% Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
This commit is contained in:
parent
01d93a93e2
commit
70d3b19776
2 changed files with 107 additions and 55 deletions
|
@ -19,6 +19,7 @@
|
|||
package govis
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
|
@ -172,3 +173,56 @@ func TestUnvisUnicode(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkUnvis(b *testing.B) {
|
||||
doBench := func(b *testing.B, text string) {
|
||||
encoded, err := Vis(text, DefaultVisFlags)
|
||||
require.NoErrorf(b, err, "vis(%q)", text)
|
||||
|
||||
decoded, err := Unvis(encoded, DefaultVisFlags)
|
||||
require.NoErrorf(b, err, "unvis(vis(%q) = %q)", text, encoded)
|
||||
require.Equalf(b, text, decoded, "unvis(vis(%q) = %q)", text, encoded)
|
||||
|
||||
for b.Loop() {
|
||||
_, _ = Unvis(encoded, DefaultVisFlags)
|
||||
}
|
||||
}
|
||||
|
||||
b.Run("NoChange", func(b *testing.B) {
|
||||
text := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
doBench(b, text)
|
||||
})
|
||||
|
||||
b.Run("Binary", func(b *testing.B) {
|
||||
var data [32]byte
|
||||
n, err := rand.Read(data[:])
|
||||
require.NoError(b, err, "rand.Read")
|
||||
require.Equal(b, len(data), n, "rand.Read len return")
|
||||
|
||||
text := string(data[:])
|
||||
doBench(b, text)
|
||||
})
|
||||
|
||||
// The rest of these test strings come from a set of test strings collated
|
||||
// in <https://www.w3.org/2001/06/utf-8-test/quickbrown.html>.
|
||||
|
||||
b.Run("ASCII", func(b *testing.B) {
|
||||
text := "The quick brown fox jumps over the lazy dog."
|
||||
doBench(b, text)
|
||||
})
|
||||
|
||||
b.Run("German", func(b *testing.B) {
|
||||
text := "Falsches Üben von Xylophonmusik quält jeden größeren Zwerg"
|
||||
doBench(b, text)
|
||||
})
|
||||
|
||||
b.Run("Russian", func(b *testing.B) {
|
||||
text := "В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!"
|
||||
doBench(b, text)
|
||||
})
|
||||
|
||||
b.Run("Japanese", func(b *testing.B) {
|
||||
text := "いろはにほへとちりぬるをイロハニホヘトチリヌルヲ"
|
||||
doBench(b, text)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue