index/suffixarray: 4.5x faster index serialization (to memory)
Benchmark results (best of 3 runs):
old: suffixarray.BenchmarkSaveRestore 1 1931909000 ns/op 28.21 MB/s
new: suffixarray.BenchmarkSaveRestore 5 429721800 ns/op 117.14 MB/s
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5161043
diff --git a/src/pkg/index/suffixarray/suffixarray_test.go b/src/pkg/index/suffixarray/suffixarray_test.go
index ec8796d..f6b2f00 100644
--- a/src/pkg/index/suffixarray/suffixarray_test.go
+++ b/src/pkg/index/suffixarray/suffixarray_test.go
@@ -230,11 +230,13 @@
return true
}
-func testSaveRestore(t *testing.T, tc *testCase, x *Index) {
+// returns the serialized index size
+func testSaveRestore(t *testing.T, tc *testCase, x *Index) int {
var buf bytes.Buffer
if err := x.Write(&buf); err != nil {
t.Errorf("failed writing index %s (%s)", tc.name, err)
}
+ size := buf.Len()
var y Index
if err := y.Read(&buf); err != nil {
t.Errorf("failed reading index %s (%s)", tc.name, err)
@@ -242,6 +244,7 @@
if !equal(x, &y) {
t.Errorf("restored index doesn't match saved index %s", tc.name)
}
+ return size
}
func TestIndex(t *testing.T) {
@@ -284,13 +287,14 @@
func BenchmarkSaveRestore(b *testing.B) {
b.StopTimer()
r := rand.New(rand.NewSource(0x5a77a1)) // guarantee always same sequence
- data := make([]byte, 10<<20) // 10MB index data
+ data := make([]byte, 10<<20) // 10MB of data to index
for i := range data {
data[i] = byte(r.Intn(256))
}
x := New(data)
- testSaveRestore(nil, nil, x) // verify correctness
- buf := bytes.NewBuffer(make([]byte, len(data))) // avoid frequent growing
+ size := testSaveRestore(nil, nil, x) // verify correctness
+ buf := bytes.NewBuffer(make([]byte, size)) // avoid growing
+ b.SetBytes(int64(size))
b.StartTimer()
for i := 0; i < b.N; i++ {
x.Write(buf)