Skip to content

Commit f67d4b6

Browse files
authored
BlobList: use recommended sorting primitives (#116)
* BlobList: use recommended sorting primitives * implement benchmarks for sorting * add doc for (BlobList).Sort func
1 parent faa3aa8 commit f67d4b6

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

backends/fs/fs.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8-
"sort"
98
"strings"
109

1110
"github.com/PowerDNS/simpleblob"
@@ -56,9 +55,7 @@ func (b *Backend) List(ctx context.Context, prefix string) (simpleblob.BlobList,
5655
})
5756
}
5857

59-
sort.Slice(blobs, func(i, j int) bool {
60-
return blobs[i].Name < blobs[j].Name
61-
})
58+
blobs.Sort()
6259
return blobs, nil
6360
}
6461

backends/memory/memory.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package memory
33
import (
44
"context"
55
"os"
6-
"sort"
76
"strings"
87
"sync"
98

@@ -30,7 +29,7 @@ func (b *Backend) List(ctx context.Context, prefix string) (simpleblob.BlobList,
3029
}
3130
b.mu.Unlock()
3231

33-
sort.Sort(blobs)
32+
blobs.Sort()
3433
return blobs, nil
3534
}
3635

backends/s3/s3.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"net/url"
1212
"os"
1313
"runtime/debug"
14-
"sort"
1514
"strings"
1615
"sync"
1716
"time"
@@ -284,7 +283,7 @@ func (b *Backend) doList(ctx context.Context, prefix string) (simpleblob.BlobLis
284283

285284
// Minio appears to return them sorted, but maybe not all implementations
286285
// will, so we sort explicitly.
287-
sort.Sort(blobs)
286+
blobs.Sort()
288287

289288
return blobs, nil
290289
}

blobs.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package simpleblob
22

33
import (
4+
"slices"
45
"strings"
56
)
67

@@ -17,12 +18,11 @@ func (bl BlobList) Len() int {
1718
return len(bl)
1819
}
1920

20-
func (bl BlobList) Less(i, j int) bool {
21-
return bl[i].Name < bl[j].Name
22-
}
23-
24-
func (bl BlobList) Swap(i, j int) {
25-
bl[i], bl[j] = bl[j], bl[i]
21+
// Sort sorts the [Blob] items in bl by name.
22+
func (bl BlobList) Sort() {
23+
slices.SortFunc(bl, func(a, b Blob) int {
24+
return strings.Compare(a.Name, b.Name)
25+
})
2626
}
2727

2828
// Names returns a slice of name strings for the BlobList

blobs_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package simpleblob
22

33
import (
4+
"slices"
5+
"sort"
6+
"strconv"
7+
"strings"
48
"testing"
59

610
"github.com/stretchr/testify/assert"
@@ -23,3 +27,41 @@ func TestBlobListStats(t *testing.T) {
2327
assert.Equal(t, blobs.Len(), 2)
2428
assert.Equal(t, blobs.Size(), int64(300))
2529
}
30+
31+
func BenchmarkBlobListSort(b *testing.B) {
32+
var blobList BlobList
33+
for i := range 100 {
34+
blobList = append(blobList, Blob{Name: strconv.Itoa(i), Size: int64(i)})
35+
}
36+
checkSorted := func() {
37+
if !slices.IsSortedFunc(blobList, func(a, b Blob) int {
38+
return strings.Compare(a.Name, b.Name)
39+
}) {
40+
b.Fatal("did not sort")
41+
}
42+
}
43+
44+
b.Run("sort.Sort", func(b *testing.B) {
45+
s := sortable(blobList)
46+
for b.Loop() {
47+
slices.Reverse(blobList)
48+
sort.Sort(s)
49+
checkSorted()
50+
}
51+
})
52+
b.Run("slices.SortFunc", func(b *testing.B) {
53+
for b.Loop() {
54+
slices.Reverse(blobList)
55+
blobList.Sort()
56+
checkSorted()
57+
}
58+
})
59+
}
60+
61+
type sortable BlobList
62+
63+
func (s sortable) Len() int { return len(s) }
64+
func (s sortable) Less(i int, j int) bool { return s[i].Name < s[j].Name }
65+
func (s sortable) Swap(i int, j int) { s[i], s[j] = s[j], s[i] }
66+
67+
var _ sort.Interface = (sortable)(nil)

0 commit comments

Comments
 (0)