Skip to content

Commit

Permalink
Merge pull request #419 from cenkalti/freelist_hmap
Browse files Browse the repository at this point in the history
modified freelist_hmap/hashmapGetFreePageIDs with better performance
  • Loading branch information
ahrtr committed Mar 15, 2023
2 parents 5c7326c + c9c264c commit eb468cb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
16 changes: 12 additions & 4 deletions freelist_hmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,20 @@ func (f *freelist) hashmapGetFreePageIDs() []common.Pgid {
}

m := make([]common.Pgid, 0, count)
for start, size := range f.forwardMap {
for i := 0; i < int(size); i++ {
m = append(m, start+common.Pgid(i))

startPageIds := make([]common.Pgid, 0, len(f.forwardMap))
for k := range f.forwardMap {
startPageIds = append(startPageIds, k)
}
sort.Sort(common.Pgids(startPageIds))

for _, start := range startPageIds {
if size, ok := f.forwardMap[start]; ok {
for i := 0; i < int(size); i++ {
m = append(m, start+common.Pgid(i))
}
}
}
sort.Sort(common.Pgids(m))

return m
}
Expand Down
49 changes: 49 additions & 0 deletions freelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,52 @@ func newTestFreelist() *freelist {

return newFreelist(freelistType)
}

func Test_freelist_hashmapGetFreePageIDs(t *testing.T) {
f := newTestFreelist()
if f.freelistType == common.FreelistArrayType {
t.Skip()
}

N := int32(100000)
fm := make(map[common.Pgid]uint64)
i := int32(0)
val := int32(0)
for i = 0; i < N; {
val = rand.Int31n(1000)
fm[common.Pgid(i)] = uint64(val)
i += val
}

f.forwardMap = fm
res := f.hashmapGetFreePageIDs()

if !sort.SliceIsSorted(res, func(i, j int) bool { return res[i] < res[j] }) {
t.Fatalf("pgids not sorted")
}
}

func Benchmark_freelist_hashmapGetFreePageIDs(b *testing.B) {
f := newTestFreelist()
if f.freelistType == common.FreelistArrayType {
b.Skip()
}

N := int32(100000)
fm := make(map[common.Pgid]uint64)
i := int32(0)
val := int32(0)
for i = 0; i < N; {
val = rand.Int31n(1000)
fm[common.Pgid(i)] = uint64(val)
i += val
}

f.forwardMap = fm

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
f.hashmapGetFreePageIDs()
}
}

0 comments on commit eb468cb

Please sign in to comment.