Skip to content

Commit 655ade7

Browse files
committed
internal/manifest: add test for zero allocs when ranging
Add a unit test that asserts ranging over a LevelMetadata or LevelSlice through their All methods result in zero allocations.
1 parent 2cf8709 commit 655ade7

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

internal/manifest/version_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,3 +784,52 @@ func TestCalculateInuseKeyRangesRandomized(t *testing.T) {
784784
}
785785
}
786786
}
787+
788+
func TestIterAllocs(t *testing.T) {
789+
t.Run("LevelSlice", func(t *testing.T) {
790+
var testLevelSlice = func() LevelSlice {
791+
const n = 10
792+
var tables []*TableMetadata
793+
for i := 0; i < n; i++ {
794+
tables = append(tables, &TableMetadata{
795+
FileNum: base.FileNum(i),
796+
FileBacking: &FileBacking{},
797+
})
798+
}
799+
return NewLevelSliceSeqSorted(tables)
800+
}()
801+
allocs := testing.AllocsPerRun(100, func() {
802+
for v := range testLevelSlice.All() {
803+
if v == nil {
804+
panic("nil")
805+
}
806+
}
807+
})
808+
if allocs > 0 {
809+
t.Fatalf("allocs=%f", allocs)
810+
}
811+
})
812+
t.Run("LevelMetadata", func(t *testing.T) {
813+
var testLevelMetadata = func() LevelMetadata {
814+
const n = 1000
815+
var tables []*TableMetadata
816+
for i := 0; i < n; i++ {
817+
tables = append(tables, &TableMetadata{
818+
FileNum: base.FileNum(i),
819+
FileBacking: &FileBacking{},
820+
})
821+
}
822+
return MakeLevelMetadata(base.DefaultComparer.Compare, 0, tables)
823+
}()
824+
allocs := testing.AllocsPerRun(100, func() {
825+
for v := range testLevelMetadata.All() {
826+
if v == nil {
827+
panic("nil")
828+
}
829+
}
830+
})
831+
if allocs > 0 {
832+
t.Fatalf("allocs=%f", allocs)
833+
}
834+
})
835+
}

0 commit comments

Comments
 (0)