From 20f8dd034791d2e41572d65e9e27a4515db27b39 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Sun, 16 Jan 2022 19:15:23 +0000 Subject: [PATCH] sstable: reuse index block iterator in Layout --- sstable/reader.go | 6 +++++- sstable/reader_test.go | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/sstable/reader.go b/sstable/reader.go index 2c193ed802..ee9bb8c4bd 100644 --- a/sstable/reader.go +++ b/sstable/reader.go @@ -2478,6 +2478,7 @@ func (r *Reader) Layout() (*Layout, error) { } else { l.TopIndex = r.indexBH topIter, _ := newBlockIter(r.Compare, indexH.Get()) + iter := &blockIter{} for key, value := topIter.First(); key != nil; key, value = topIter.Next() { indexBH, err := decodeBlockHandleWithProperties(value) if err != nil { @@ -2490,7 +2491,9 @@ func (r *Reader) Layout() (*Layout, error) { if err != nil { return nil, err } - iter, _ := newBlockIter(r.Compare, subIndex.Get()) + if err := iter.init(r.Compare, subIndex.Get(), 0 /* globalSeqNum */); err != nil { + return nil, err + } for key, value := iter.First(); key != nil; key, value = iter.Next() { dataBH, err := decodeBlockHandleWithProperties(value) if err != nil { @@ -2499,6 +2502,7 @@ func (r *Reader) Layout() (*Layout, error) { l.Data = append(l.Data, dataBH.BlockHandle) } subIndex.Release() + *iter = iter.resetForReuse() } } diff --git a/sstable/reader_test.go b/sstable/reader_test.go index 25a2c55fa9..1da780ffd4 100644 --- a/sstable/reader_test.go +++ b/sstable/reader_test.go @@ -1063,3 +1063,13 @@ func BenchmarkTableIterPrev(b *testing.B) { }) } } + +func BenchmarkLayout(b *testing.B) { + r, _ := buildBenchmarkTable(b, WriterOptions{}) + b.ResetTimer() + for i := 0; i < b.N; i++ { + r.Layout() + } + b.StopTimer() + r.Close() +}