resources/page: Use binary search in Pages.Prev/Next if possible

This is obviously much faster for lager data sets:

```bash
name                         old time/op    new time/op    delta
SearchPage/ByWeight-100-4       267ns ± 4%     272ns ± 5%     ~     (p=0.457 n=4+4)
SearchPage/ByWeight-5000-4     10.8µs ± 3%     1.2µs ± 2%  -88.99%  (p=0.029 n=4+4)
SearchPage/ByWeight-10000-4    21.1µs ± 1%     1.4µs ±11%  -93.28%  (p=0.029 n=4+4)
```

See #4500
This commit is contained in:
Bjørn Erik Pedersen
2019-10-11 13:55:46 +02:00
parent f4f566edf4
commit 653e6856ea
8 changed files with 335 additions and 83 deletions

View File

@@ -15,26 +15,21 @@ package page
// Next returns the next page reletive to the given
func (p Pages) Next(cur Page) Page {
for x, c := range p {
if c.Eq(cur) {
if x == 0 {
return nil
}
return p[x-1]
}
x := searchPage(cur, p)
if x <= 0 {
return nil
}
return nil
return p[x-1]
}
// Prev returns the previous page reletive to the given
func (p Pages) Prev(cur Page) Page {
for x, c := range p {
if c.Eq(cur) {
if x < len(p)-1 {
return p[x+1]
}
return nil
}
x := searchPage(cur, p)
if x == -1 || len(p)-x < 2 {
return nil
}
return nil
return p[x+1]
}