diff --git a/freelist.go b/freelist.go index df544ceaa..90dd14d18 100644 --- a/freelist.go +++ b/freelist.go @@ -174,7 +174,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 {