Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

modified freelist_hmap/hashmapGetFreePageIDs function with better performance #219

Closed
wants to merge 2 commits into from
Closed
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
17 changes: 13 additions & 4 deletions freelist_hmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,21 @@ func (f *freelist) hashmapGetFreePageIDs() []pgid {
}

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

keys := make([]pgid, 0, len(f.forwardMap))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
keys := make([]pgid, 0, len(f.forwardMap))
startPageIds := make([]pgid, 0, len(f.forwardMap))

for k := range f.forwardMap {
keys = append(keys, k)
}
sort.Sort(pgids(keys))

for _, start := range keys {
size, ok := f.forwardMap[start]
if ok {
Comment on lines +87 to +88
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
size, ok := f.forwardMap[start]
if ok {
if size, ok := f.forwardMap[start]; ok {

for i := 0; i < int(size); i++ {
m = append(m, start+pgid(i))
}
}
}
sort.Sort(pgids(m))

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

return newFreelist(freelistType)
}

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

N := int32(100000)
fm := make(map[pgid]uint64)
i := int32(0)
val := int32(0)
for i = 0; i < N; {
val = rand.Int31n(1000)
fm[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")
}
}