Skip to content

Commit

Permalink
modified freelist_hmap/hashmapGetFreePageIDs with better performance
Browse files Browse the repository at this point in the history
Signed-off-by: Cenk Alti <cenkalti@gmail.com>
  • Loading branch information
mingtingzhang authored and cenkalti committed Mar 15, 2023
1 parent 838586a commit c9c264c
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 c9c264c

Please sign in to comment.