From 1858583b3b0b9db3794a35fa9689c8c351363dbf Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Thu, 22 Dec 2016 17:05:52 -0800 Subject: Clean up after #636 freelist.lenall duplicated freelist.count. freelist.copyall and mergepgids docs had typos. --- freelist.go | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) (limited to 'freelist.go') diff --git a/freelist.go b/freelist.go index 53efa8f..e07bf0a 100644 --- a/freelist.go +++ b/freelist.go @@ -46,17 +46,8 @@ func (f *freelist) pending_count() int { return count } -// lenall returns the combined number of all free ids and all pending ids. -func (f *freelist) lenall() int { - n := len(f.ids) - for _, list := range f.pending { - n += len(list) - } - return n -} - -// all copies into dst a list of all free ids and all pending ids in one sorted list. -// f.lenall returns the minimum length required for dst. +// copyall copies into dst a list of all free ids and all pending ids in one sorted list. +// f.count returns the minimum length required for dst. func (f *freelist) copyall(dst []pgid) { m := make(pgids, 0, len(f.pending)) // len(f.pending) undercounts, but it is a start for _, list := range f.pending { @@ -200,7 +191,7 @@ func (f *freelist) write(p *page) error { // The page.count can only hold up to 64k elements so if we overflow that // number then we handle it by putting the size in the first element. - lenids := f.lenall() + lenids := f.count() if lenids == 0 { p.count = uint16(lenids) } else if lenids < 0xFFFF { -- cgit v1.2.3 From 0e120dc4700273c23d10ef64f25663fa21b84e9b Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Thu, 22 Dec 2016 17:20:50 -0800 Subject: Precalculate size of pending pgids in freelist.copyall This recovers the slight alloc regression in #636. --- freelist.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'freelist.go') diff --git a/freelist.go b/freelist.go index e07bf0a..de59aaa 100644 --- a/freelist.go +++ b/freelist.go @@ -49,7 +49,7 @@ func (f *freelist) pending_count() int { // copyall copies into dst a list of all free ids and all pending ids in one sorted list. // f.count returns the minimum length required for dst. func (f *freelist) copyall(dst []pgid) { - m := make(pgids, 0, len(f.pending)) // len(f.pending) undercounts, but it is a start + m := make(pgids, 0, f.pending_count()) for _, list := range f.pending { m = append(m, list...) } -- cgit v1.2.3 From 7adfa44e02041a6d9b571025a8237a13fa6fbde8 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Thu, 22 Dec 2016 17:22:44 -0800 Subject: Fix freelist.size calculation for large freelists freelist.size did not account for the extra fake freelist item used to hold the number of elements when the freelist is large. --- freelist.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'freelist.go') diff --git a/freelist.go b/freelist.go index de59aaa..aba48f5 100644 --- a/freelist.go +++ b/freelist.go @@ -24,7 +24,12 @@ func newFreelist() *freelist { // size returns the size of the page after serialization. func (f *freelist) size() int { - return pageHeaderSize + (int(unsafe.Sizeof(pgid(0))) * f.count()) + n := f.count() + if n >= 0xFFFF { + // The first element will be used to store the count. See freelist.write. + n++ + } + return pageHeaderSize + (int(unsafe.Sizeof(pgid(0))) * n) } // count returns count of pages on the freelist -- cgit v1.2.3