From 87b6edd99249123fc96eb9bee54bea3aa1bb98de Mon Sep 17 00:00:00 2001 From: Sokolov Yura aka funny_falcon Date: Mon, 17 Aug 2015 19:20:32 +0300 Subject: [PATCH] sort pgids on file open only if they aren't sorted --- freelist.go | 4 +++- page.go | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/freelist.go b/freelist.go index e0942a0ae..923f77fcb 100644 --- a/freelist.go +++ b/freelist.go @@ -172,7 +172,9 @@ func (f *freelist) read(p *page) { copy(f.ids, ids) // Make sure they're sorted. - sort.Sort(pgids(f.ids)) + if !pgids(f.ids).isSorted() { + sort.Sort(pgids(f.ids)) + } // Rebuild the page cache. f.reindex() diff --git a/page.go b/page.go index 818aa1b15..673a73086 100644 --- a/page.go +++ b/page.go @@ -133,6 +133,17 @@ type pgids []pgid func (s pgids) Len() int { return len(s) } func (s pgids) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s pgids) Less(i, j int) bool { return s[i] < s[j] } +func (s pgids) isSorted() bool { + if len(s) == 0 { + return true + } + for i, v := range s[:len(s)-1] { + if s[i+1] < v { + return false + } + } + return true +} // merge returns the sorted union of a and b. func (a pgids) merge(b pgids) pgids {