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
This commit is contained in:
Robert Griesemer 2011-09-30 11:31:28 -07:00
parent 40c26fff14
commit a7a7cc5a55
2 changed files with 101 additions and 44 deletions

View file

@ -230,11 +230,13 @@ func equal(x, y *Index) bool {
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 @@ func testSaveRestore(t *testing.T, tc *testCase, x *Index) {
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 BenchmarkNewIndexRepeat(b *testing.B) {
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)