From 178effed84d4bbd796cb4fc7129dcc842e3825fa Mon Sep 17 00:00:00 2001 From: mingtingzhang Date: Tue, 28 Apr 2020 17:39:17 +0800 Subject: [PATCH 1/2] modified freelist_hmap/hashmapGetFreePageIDs with better performance --- freelist_hmap.go | 17 +++++++++++++---- freelist_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/freelist_hmap.go b/freelist_hmap.go index 02ef2be04..33af496fe 100644 --- a/freelist_hmap.go +++ b/freelist_hmap.go @@ -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)) + for k, _ := range f.forwardMap { + keys = append(keys, k) + } + sort.Sort(pgids(keys)) + + for _, start := range keys { + size, ok := f.forwardMap[start] + if ok { + for i := 0; i < int(size); i++ { + m = append(m, start+pgid(i)) + } } } - sort.Sort(pgids(m)) return m } diff --git a/freelist_test.go b/freelist_test.go index 97656f4a2..eb546c746 100644 --- a/freelist_test.go +++ b/freelist_test.go @@ -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") + } +} From 9dac4fdd1294a2a9394adf3b06f0733070c64350 Mon Sep 17 00:00:00 2001 From: mingtingzhang Date: Tue, 28 Apr 2020 17:41:12 +0800 Subject: [PATCH 2/2] fix make fmt --- freelist_hmap.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freelist_hmap.go b/freelist_hmap.go index 33af496fe..64aeb8199 100644 --- a/freelist_hmap.go +++ b/freelist_hmap.go @@ -78,7 +78,7 @@ func (f *freelist) hashmapGetFreePageIDs() []pgid { m := make([]pgid, 0, count) keys := make([]pgid, 0, len(f.forwardMap)) - for k, _ := range f.forwardMap { + for k := range f.forwardMap { keys = append(keys, k) } sort.Sort(pgids(keys))