enhancement: replace sort.Slice with slices.SortFunc to reduce reflection
This commit is contained in:
@@ -4,12 +4,11 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/chrislusf/seaweedfs/weed/pb"
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/types"
|
||||
"io"
|
||||
"sort"
|
||||
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/types"
|
||||
"golang.org/x/exp/slices"
|
||||
"io"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -411,8 +410,8 @@ func doBalanceEcRack(commandEnv *CommandEnv, ecRack *EcRack, applyBalancing bool
|
||||
hasMove := true
|
||||
for hasMove {
|
||||
hasMove = false
|
||||
sort.Slice(rackEcNodes, func(i, j int) bool {
|
||||
return rackEcNodes[i].freeEcSlot > rackEcNodes[j].freeEcSlot
|
||||
slices.SortFunc(rackEcNodes, func(a, b *EcNode) bool {
|
||||
return a.freeEcSlot > b.freeEcSlot
|
||||
})
|
||||
emptyNode, fullNode := rackEcNodes[0], rackEcNodes[len(rackEcNodes)-1]
|
||||
emptyNodeShardCount, fullNodeShardCount := ecNodeIdToShardCount[emptyNode.info.Id], ecNodeIdToShardCount[fullNode.info.Id]
|
||||
@@ -492,8 +491,8 @@ func pickNEcShardsToMoveFrom(ecNodes []*EcNode, vid needle.VolumeId, n int) map[
|
||||
})
|
||||
}
|
||||
}
|
||||
sort.Slice(candidateEcNodes, func(i, j int) bool {
|
||||
return candidateEcNodes[i].shardCount > candidateEcNodes[j].shardCount
|
||||
slices.SortFunc(candidateEcNodes, func(a, b *CandidateEcNode) bool {
|
||||
return a.shardCount > b.shardCount
|
||||
})
|
||||
for i := 0; i < n; i++ {
|
||||
selectedEcNodeIndex := -1
|
||||
|
||||
@@ -3,18 +3,17 @@ package shell
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/chrislusf/seaweedfs/weed/pb"
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/types"
|
||||
"math"
|
||||
"sort"
|
||||
|
||||
"github.com/chrislusf/seaweedfs/weed/glog"
|
||||
"github.com/chrislusf/seaweedfs/weed/operation"
|
||||
"github.com/chrislusf/seaweedfs/weed/pb"
|
||||
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
||||
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/types"
|
||||
"golang.org/x/exp/slices"
|
||||
"google.golang.org/grpc"
|
||||
"math"
|
||||
)
|
||||
|
||||
func moveMountedShardToEcNode(commandEnv *CommandEnv, existingLocation *EcNode, collection string, vid needle.VolumeId, shardId erasure_coding.ShardId, destinationEcNode *EcNode, applyBalancing bool) (err error) {
|
||||
@@ -116,14 +115,14 @@ func eachDataNode(topo *master_pb.TopologyInfo, fn func(dc string, rack RackId,
|
||||
}
|
||||
|
||||
func sortEcNodesByFreeslotsDecending(ecNodes []*EcNode) {
|
||||
sort.Slice(ecNodes, func(i, j int) bool {
|
||||
return ecNodes[i].freeEcSlot > ecNodes[j].freeEcSlot
|
||||
slices.SortFunc(ecNodes, func(a, b *EcNode) bool {
|
||||
return a.freeEcSlot > b.freeEcSlot
|
||||
})
|
||||
}
|
||||
|
||||
func sortEcNodesByFreeslotsAscending(ecNodes []*EcNode) {
|
||||
sort.Slice(ecNodes, func(i, j int) bool {
|
||||
return ecNodes[i].freeEcSlot < ecNodes[j].freeEcSlot
|
||||
slices.SortFunc(ecNodes, func(a, b *EcNode) bool {
|
||||
return a.freeEcSlot < b.freeEcSlot
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -2,11 +2,10 @@ package shell
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"io"
|
||||
"sort"
|
||||
|
||||
"github.com/golang/protobuf/jsonpb"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"golang.org/x/exp/slices"
|
||||
"io"
|
||||
|
||||
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/chrislusf/seaweedfs/weed/util"
|
||||
@@ -55,14 +54,12 @@ func (c *commandFsMetaCat) Do(args []string, commandEnv *CommandEnv, writer io.W
|
||||
EmitDefaults: true,
|
||||
Indent: " ",
|
||||
}
|
||||
|
||||
sort.Slice(respLookupEntry.Entry.Chunks, func(i, j int) bool {
|
||||
if respLookupEntry.Entry.Chunks[i].Offset == respLookupEntry.Entry.Chunks[j].Offset {
|
||||
return respLookupEntry.Entry.Chunks[i].Mtime < respLookupEntry.Entry.Chunks[j].Mtime
|
||||
slices.SortFunc(respLookupEntry.Entry.Chunks, func(a, b *filer_pb.FileChunk) bool {
|
||||
if a.Offset == b.Offset {
|
||||
return a.Mtime < b.Mtime
|
||||
}
|
||||
return respLookupEntry.Entry.Chunks[i].Offset < respLookupEntry.Entry.Chunks[j].Offset
|
||||
return a.Offset < b.Offset
|
||||
})
|
||||
|
||||
text, marshalErr := m.MarshalToString(respLookupEntry.Entry)
|
||||
if marshalErr != nil {
|
||||
return fmt.Errorf("marshal meta: %v", marshalErr)
|
||||
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
"github.com/chrislusf/seaweedfs/weed/pb"
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/super_block"
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/types"
|
||||
"golang.org/x/exp/slices"
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
||||
@@ -224,14 +224,14 @@ func (n *Node) selectVolumes(fn func(v *master_pb.VolumeInformationMessage) bool
|
||||
}
|
||||
|
||||
func sortWritableVolumes(volumes []*master_pb.VolumeInformationMessage) {
|
||||
sort.Slice(volumes, func(i, j int) bool {
|
||||
return volumes[i].Size < volumes[j].Size
|
||||
slices.SortFunc(volumes, func(a, b *master_pb.VolumeInformationMessage) bool {
|
||||
return a.Size < b.Size
|
||||
})
|
||||
}
|
||||
|
||||
func sortReadOnlyVolumes(volumes []*master_pb.VolumeInformationMessage) {
|
||||
sort.Slice(volumes, func(i, j int) bool {
|
||||
return volumes[i].Id < volumes[j].Id
|
||||
slices.SortFunc(volumes, func(a, b *master_pb.VolumeInformationMessage) bool {
|
||||
return a.Id < b.Id
|
||||
})
|
||||
}
|
||||
|
||||
@@ -255,10 +255,9 @@ func balanceSelectedVolume(commandEnv *CommandEnv, diskType types.DiskType, volu
|
||||
|
||||
for hasMoved {
|
||||
hasMoved = false
|
||||
sort.Slice(nodesWithCapacity, func(i, j int) bool {
|
||||
return nodesWithCapacity[i].localVolumeRatio(capacityFunc) < nodesWithCapacity[j].localVolumeRatio(capacityFunc)
|
||||
slices.SortFunc(nodesWithCapacity, func(a, b *Node) bool {
|
||||
return a.localVolumeRatio(capacityFunc) < b.localVolumeRatio(capacityFunc)
|
||||
})
|
||||
|
||||
fullNode := nodesWithCapacity[len(nodesWithCapacity)-1]
|
||||
var candidateVolumes []*master_pb.VolumeInformationMessage
|
||||
for _, v := range fullNode.selectedVolumes {
|
||||
|
||||
@@ -9,9 +9,9 @@ import (
|
||||
"github.com/chrislusf/seaweedfs/weed/pb"
|
||||
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/needle_map"
|
||||
"golang.org/x/exp/slices"
|
||||
"io"
|
||||
"math"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -70,8 +70,8 @@ func (c *commandVolumeCheckDisk) Do(args []string, commandEnv *CommandEnv, write
|
||||
}
|
||||
|
||||
for _, replicas := range volumeReplicas {
|
||||
sort.Slice(replicas, func(i, j int) bool {
|
||||
return fileCount(replicas[i]) > fileCount(replicas[j])
|
||||
slices.SortFunc(replicas, func(a, b *VolumeReplica) bool {
|
||||
return fileCount(a) > fileCount(b)
|
||||
})
|
||||
for len(replicas) >= 2 {
|
||||
a, b := replicas[0], replicas[1]
|
||||
|
||||
@@ -7,9 +7,9 @@ import (
|
||||
"github.com/chrislusf/seaweedfs/weed/pb"
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/types"
|
||||
"golang.org/x/exp/slices"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -308,8 +308,8 @@ func (c *commandVolumeFixReplication) fixOneUnderReplicatedVolume(commandEnv *Co
|
||||
|
||||
func keepDataNodesSorted(dataNodes []location, diskType types.DiskType) {
|
||||
fn := capacityByFreeVolumeCount(diskType)
|
||||
sort.Slice(dataNodes, func(i, j int) bool {
|
||||
return fn(dataNodes[i].dataNode) > fn(dataNodes[j].dataNode)
|
||||
slices.SortFunc(dataNodes, func(a, b location) bool {
|
||||
return fn(a.dataNode) > fn(b.dataNode)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -488,9 +488,7 @@ func countReplicas(replicas []*VolumeReplica) (diffDc, diffRack, diffNode map[st
|
||||
}
|
||||
|
||||
func pickOneReplicaToDelete(replicas []*VolumeReplica, replicaPlacement *super_block.ReplicaPlacement) *VolumeReplica {
|
||||
|
||||
sort.Slice(replicas, func(i, j int) bool {
|
||||
a, b := replicas[i], replicas[j]
|
||||
slices.SortFunc(replicas, func(a, b *VolumeReplica) bool {
|
||||
if a.info.Size != b.info.Size {
|
||||
return a.info.Size < b.info.Size
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
"fmt"
|
||||
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
|
||||
"golang.org/x/exp/slices"
|
||||
|
||||
"io"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -67,8 +67,8 @@ func diskInfoToString(diskInfo *master_pb.DiskInfo) string {
|
||||
|
||||
func writeTopologyInfo(writer io.Writer, t *master_pb.TopologyInfo, volumeSizeLimitMb uint64, verbosityLevel int) statistics {
|
||||
output(verbosityLevel >= 0, writer, "Topology volumeSizeLimit:%d MB%s\n", volumeSizeLimitMb, diskInfosToString(t.DiskInfos))
|
||||
sort.Slice(t.DataCenterInfos, func(i, j int) bool {
|
||||
return t.DataCenterInfos[i].Id < t.DataCenterInfos[j].Id
|
||||
slices.SortFunc(t.DataCenterInfos, func(a, b *master_pb.DataCenterInfo) bool {
|
||||
return a.Id < b.Id
|
||||
})
|
||||
var s statistics
|
||||
for _, dc := range t.DataCenterInfos {
|
||||
@@ -80,8 +80,8 @@ func writeTopologyInfo(writer io.Writer, t *master_pb.TopologyInfo, volumeSizeLi
|
||||
func writeDataCenterInfo(writer io.Writer, t *master_pb.DataCenterInfo, verbosityLevel int) statistics {
|
||||
output(verbosityLevel >= 1, writer, " DataCenter %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
|
||||
var s statistics
|
||||
sort.Slice(t.RackInfos, func(i, j int) bool {
|
||||
return t.RackInfos[i].Id < t.RackInfos[j].Id
|
||||
slices.SortFunc(t.RackInfos, func(a, b *master_pb.RackInfo) bool {
|
||||
return a.Id < b.Id
|
||||
})
|
||||
for _, r := range t.RackInfos {
|
||||
s = s.plus(writeRackInfo(writer, r, verbosityLevel))
|
||||
@@ -92,8 +92,8 @@ func writeDataCenterInfo(writer io.Writer, t *master_pb.DataCenterInfo, verbosit
|
||||
func writeRackInfo(writer io.Writer, t *master_pb.RackInfo, verbosityLevel int) statistics {
|
||||
output(verbosityLevel >= 2, writer, " Rack %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
|
||||
var s statistics
|
||||
sort.Slice(t.DataNodeInfos, func(i, j int) bool {
|
||||
return t.DataNodeInfos[i].Id < t.DataNodeInfos[j].Id
|
||||
slices.SortFunc(t.DataNodeInfos, func(a, b *master_pb.DataNodeInfo) bool {
|
||||
return a.Id < b.Id
|
||||
})
|
||||
for _, dn := range t.DataNodeInfos {
|
||||
s = s.plus(writeDataNodeInfo(writer, dn, verbosityLevel))
|
||||
@@ -118,8 +118,8 @@ func writeDiskInfo(writer io.Writer, t *master_pb.DiskInfo, verbosityLevel int)
|
||||
diskType = "hdd"
|
||||
}
|
||||
output(verbosityLevel >= 4, writer, " Disk %s(%s)\n", diskType, diskInfoToString(t))
|
||||
sort.Slice(t.VolumeInfos, func(i, j int) bool {
|
||||
return t.VolumeInfos[i].Id < t.VolumeInfos[j].Id
|
||||
slices.SortFunc(t.VolumeInfos, func(a, b *master_pb.VolumeInformationMessage) bool {
|
||||
return a.Id < b.Id
|
||||
})
|
||||
for _, vi := range t.VolumeInfos {
|
||||
s = s.plus(writeVolumeInformationMessage(writer, vi, verbosityLevel))
|
||||
|
||||
@@ -8,9 +8,9 @@ import (
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/super_block"
|
||||
"github.com/chrislusf/seaweedfs/weed/storage/types"
|
||||
"golang.org/x/exp/slices"
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -153,11 +153,9 @@ func evacuateEcVolumes(commandEnv *CommandEnv, topologyInfo *master_pb.TopologyI
|
||||
func moveAwayOneEcVolume(commandEnv *CommandEnv, ecShardInfo *master_pb.VolumeEcShardInformationMessage, thisNode *EcNode, otherNodes []*EcNode, applyChange bool) (hasMoved bool, err error) {
|
||||
|
||||
for _, shardId := range erasure_coding.ShardBits(ecShardInfo.EcIndexBits).ShardIds() {
|
||||
|
||||
sort.Slice(otherNodes, func(i, j int) bool {
|
||||
return otherNodes[i].localShardIdCount(ecShardInfo.Id) < otherNodes[j].localShardIdCount(ecShardInfo.Id)
|
||||
slices.SortFunc(otherNodes, func(a, b *EcNode) bool {
|
||||
return a.localShardIdCount(ecShardInfo.Id) < b.localShardIdCount(ecShardInfo.Id)
|
||||
})
|
||||
|
||||
for i := 0; i < len(otherNodes); i++ {
|
||||
emptyNode := otherNodes[i]
|
||||
collectionPrefix := ""
|
||||
@@ -188,10 +186,9 @@ func moveAwayOneNormalVolume(commandEnv *CommandEnv, volumeReplicas map[uint32][
|
||||
return v.DiskType == vol.DiskType
|
||||
})
|
||||
}
|
||||
sort.Slice(otherNodes, func(i, j int) bool {
|
||||
return otherNodes[i].localVolumeRatio(fn) > otherNodes[j].localVolumeRatio(fn)
|
||||
slices.SortFunc(otherNodes, func(a, b *Node) bool {
|
||||
return a.localVolumeRatio(fn) > b.localVolumeRatio(fn)
|
||||
})
|
||||
|
||||
for i := 0; i < len(otherNodes); i++ {
|
||||
emptyNode := otherNodes[i]
|
||||
hasMoved, err = maybeMoveOneVolume(commandEnv, volumeReplicas, thisNode, vol, emptyNode, applyChange)
|
||||
|
||||
@@ -8,12 +8,12 @@ import (
|
||||
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
||||
"github.com/chrislusf/seaweedfs/weed/util/grace"
|
||||
"golang.org/x/exp/slices"
|
||||
"io"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/peterh/liner"
|
||||
@@ -25,11 +25,9 @@ var (
|
||||
)
|
||||
|
||||
func RunShell(options ShellOptions) {
|
||||
|
||||
sort.Slice(Commands, func(i, j int) bool {
|
||||
return strings.Compare(Commands[i].Name(), Commands[j].Name()) < 0
|
||||
slices.SortFunc(Commands, func(a, b command) bool {
|
||||
return strings.Compare(a.Name(), b.Name()) < 0
|
||||
})
|
||||
|
||||
line = liner.NewLiner()
|
||||
defer line.Close()
|
||||
grace.OnInterrupt(func() {
|
||||
|
||||
Reference in New Issue
Block a user