Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

modified freelist_hmap/hashmapGetFreePageIDs with better performance #419

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please implement a microbenchmark an use benchstat to show performance improvement. Article shows how properly use it.

Sorry for being a stickler, but from my experience it's easy to make mistakes when optimizing and we need to standardize the process for writing performance improvement PRs.

Copy link
Member Author

@cenkalti cenkalti Mar 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

benchmark                                       old ns/op     new ns/op     delta
Benchmark_freelist_hashmapGetFreePageIDs-12     21804519      181847        -99.17%

benchmark                                       old allocs     new allocs     delta
Benchmark_freelist_hashmapGetFreePageIDs-12     2              3              +50.00%

benchmark                                       old bytes     new bytes     delta
Benchmark_freelist_hashmapGetFreePageIDs-12     802886        812828        +1.24%

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()
}
}