forked from TrueCloudLab/frostfs-node
[#1471] Replace sort.Slice in some places
`slices.SortFunc` doesn't use reflection and is a bit faster. I have done some micro-benchmarks for `[]NodeInfo`: ``` $ benchstat -col "/func" out goos: linux goarch: amd64 pkg: git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/pilorama cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz │ sort.Slice │ slices.SortFunc │ │ sec/op │ sec/op vs base │ Sort-8 2.130µ ± 2% 1.253µ ± 2% -41.20% (p=0.000 n=10) ``` Haven't included them, though, as they I don't see them being used a lot. Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
17ec84151b
commit
15102e6dfd
7 changed files with 29 additions and 31 deletions
|
@ -670,9 +670,8 @@ func SearchObjects(ctx context.Context, prm SearchObjectsPrm) (*SearchObjectsRes
|
||||||
return nil, fmt.Errorf("read object list: %w", err)
|
return nil, fmt.Errorf("read object list: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Slice(list, func(i, j int) bool {
|
slices.SortFunc(list, func(a, b oid.ID) int {
|
||||||
lhs, rhs := list[i].EncodeToString(), list[j].EncodeToString()
|
return strings.Compare(a.EncodeToString(), b.EncodeToString())
|
||||||
return strings.Compare(lhs, rhs) < 0
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return &SearchObjectsRes{
|
return &SearchObjectsRes{
|
||||||
|
|
|
@ -2,7 +2,7 @@ package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"sort"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
||||||
|
@ -45,15 +45,11 @@ func StartClientCommandSpan(cmd *cobra.Command) {
|
||||||
})
|
})
|
||||||
commonCmd.ExitOnErr(cmd, "init tracing: %w", err)
|
commonCmd.ExitOnErr(cmd, "init tracing: %w", err)
|
||||||
|
|
||||||
var components sort.StringSlice
|
var components []string
|
||||||
for c := cmd; c != nil; c = c.Parent() {
|
for c := cmd; c != nil; c = c.Parent() {
|
||||||
components = append(components, c.Name())
|
components = append(components, c.Name())
|
||||||
}
|
}
|
||||||
for i, j := 0, len(components)-1; i < j; {
|
slices.Reverse(components)
|
||||||
components.Swap(i, j)
|
|
||||||
i++
|
|
||||||
j--
|
|
||||||
}
|
|
||||||
|
|
||||||
operation := strings.Join(components, ".")
|
operation := strings.Join(components, ".")
|
||||||
ctx, span := tracing.StartSpanFromContext(cmd.Context(), operation)
|
ctx, span := tracing.StartSpanFromContext(cmd.Context(), operation)
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package pilorama
|
package pilorama
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cmp"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"slices"
|
"slices"
|
||||||
"sort"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -48,8 +48,8 @@ func (b *batch) run() {
|
||||||
|
|
||||||
// Sorting without a mutex is ok, because we append to this slice only if timer is non-nil.
|
// Sorting without a mutex is ok, because we append to this slice only if timer is non-nil.
|
||||||
// See (*boltForest).addBatch for details.
|
// See (*boltForest).addBatch for details.
|
||||||
sort.Slice(b.operations, func(i, j int) bool {
|
slices.SortFunc(b.operations, func(mi, mj *Move) int {
|
||||||
return b.operations[i].Time < b.operations[j].Time
|
return cmp.Compare(mi.Time, mj.Time)
|
||||||
})
|
})
|
||||||
b.operations = slices.CompactFunc(b.operations, func(x, y *Move) bool { return x.Time == y.Time })
|
b.operations = slices.CompactFunc(b.operations, func(x, y *Move) bool { return x.Time == y.Time })
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -1093,14 +1092,19 @@ func (t *boltForest) TreeSortedByFilename(ctx context.Context, cid cidSDK.ID, tr
|
||||||
return res, last, metaerr.Wrap(err)
|
return res, last, metaerr.Wrap(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sortByFilename(nodes []NodeInfo) {
|
||||||
|
slices.SortFunc(nodes, func(a, b NodeInfo) int {
|
||||||
|
return bytes.Compare(a.Meta.GetAttr(AttributeFilename), b.Meta.GetAttr(AttributeFilename))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func sortAndCut(result []NodeInfo, last *string) []NodeInfo {
|
func sortAndCut(result []NodeInfo, last *string) []NodeInfo {
|
||||||
var lastBytes []byte
|
var lastBytes []byte
|
||||||
if last != nil {
|
if last != nil {
|
||||||
lastBytes = []byte(*last)
|
lastBytes = []byte(*last)
|
||||||
}
|
}
|
||||||
sort.Slice(result, func(i, j int) bool {
|
sortByFilename(result)
|
||||||
return bytes.Compare(result[i].Meta.GetAttr(AttributeFilename), result[j].Meta.GetAttr(AttributeFilename)) == -1
|
|
||||||
})
|
|
||||||
for i := range result {
|
for i := range result {
|
||||||
if lastBytes == nil || bytes.Compare(lastBytes, result[i].Meta.GetAttr(AttributeFilename)) == -1 {
|
if lastBytes == nil || bytes.Compare(lastBytes, result[i].Meta.GetAttr(AttributeFilename)) == -1 {
|
||||||
return result[i:]
|
return result[i:]
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package pilorama
|
package pilorama
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -192,9 +191,7 @@ func (f *memoryForest) TreeSortedByFilename(_ context.Context, cid cid.ID, treeI
|
||||||
return nil, start, nil
|
return nil, start, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Slice(res, func(i, j int) bool {
|
sortByFilename(res)
|
||||||
return bytes.Compare(res[i].Meta.GetAttr(AttributeFilename), res[j].Meta.GetAttr(AttributeFilename)) == -1
|
|
||||||
})
|
|
||||||
|
|
||||||
r := mergeNodeInfos(res)
|
r := mergeNodeInfos(res)
|
||||||
for i := range r {
|
for i := range r {
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package pilorama
|
package pilorama
|
||||||
|
|
||||||
import "sort"
|
import (
|
||||||
|
"cmp"
|
||||||
|
"slices"
|
||||||
|
)
|
||||||
|
|
||||||
// nodeInfo couples parent and metadata.
|
// nodeInfo couples parent and metadata.
|
||||||
type nodeInfo struct {
|
type nodeInfo struct {
|
||||||
|
@ -131,10 +134,10 @@ func (t tree) getChildren(parent Node) []Node {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Slice(children, func(i, j int) bool {
|
slices.SortFunc(children, func(ci, cj uint64) int {
|
||||||
a := t.infoMap[children[i]]
|
a := t.infoMap[ci]
|
||||||
b := t.infoMap[children[j]]
|
b := t.infoMap[cj]
|
||||||
return a.Meta.Time < b.Meta.Time
|
return cmp.Compare(a.Meta.Time, b.Meta.Time)
|
||||||
})
|
})
|
||||||
return children
|
return children
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"slices"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
|
@ -575,10 +575,9 @@ func sortByFilename(nodes []pilorama.NodeInfo, d GetSubTreeRequest_Body_Order_Di
|
||||||
if len(nodes) == 0 {
|
if len(nodes) == 0 {
|
||||||
return nodes, nil
|
return nodes, nil
|
||||||
}
|
}
|
||||||
less := func(i, j int) bool {
|
slices.SortFunc(nodes, func(a, b pilorama.NodeInfo) int {
|
||||||
return bytes.Compare(nodes[i].Meta.GetAttr(pilorama.AttributeFilename), nodes[j].Meta.GetAttr(pilorama.AttributeFilename)) < 0
|
return bytes.Compare(a.Meta.GetAttr(pilorama.AttributeFilename), b.Meta.GetAttr(pilorama.AttributeFilename))
|
||||||
}
|
})
|
||||||
sort.Slice(nodes, less)
|
|
||||||
return nodes, nil
|
return nodes, nil
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupported order direction: %s", d.String())
|
return nil, fmt.Errorf("unsupported order direction: %s", d.String())
|
||||||
|
|
Loading…
Reference in a new issue