Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/build/incrementality.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"hash"
"os"
"path/filepath"
"slices"
"sort"

"github.com/thought-machine/please/src/core"
Expand Down Expand Up @@ -156,7 +157,7 @@ func ruleHash(state *core.BuildState, target *core.BuildTarget, runtime bool) []
for dep := range target.DeclaredDependencies() {
deps = append(deps, dep)
}
sort.Sort(deps)
slices.SortFunc(deps, core.BuildLabel.Compare)
for _, dep := range deps {
h.Write([]byte(dep.String()))
}
Expand Down
11 changes: 6 additions & 5 deletions src/cache/dir_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ package cache
import (
"archive/tar"
"bufio"
"cmp"
"compress/gzip"
"encoding/base64"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"slices"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -445,12 +446,12 @@ func (cache *dirCache) clean(highWaterMark, lowWaterMark uint64) uint64 {
return totalSize // Nothing to do, cache is small enough.
}
// OK, we need to slim it down a bit. We implement a simple LRU algorithm.
sort.Slice(entries, func(i, j int) bool {
diff := entries[i].Atime - entries[j].Atime
slices.SortFunc(entries, func(a, b cacheEntry) int {
diff := a.Atime - b.Atime
if diff > -accessTimeGracePeriod && diff < accessTimeGracePeriod {
return entries[i].Size > entries[j].Size
return cmp.Compare(b.Size, a.Size)
}
return entries[i].Atime < entries[j].Atime
return cmp.Compare(a.Atime, b.Atime)
})
for _, entry := range entries {
if _, marked := cache.isMarked(entry.Path); marked {
Expand Down
5 changes: 3 additions & 2 deletions src/cli/suggest.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package cli

import (
"sort"
"cmp"
"slices"
"strings"

"github.com/texttheater/golang-levenshtein/levenshtein"
Expand All @@ -17,7 +18,7 @@ func Suggest(needle string, haystack []string, maxSuggestionDistance int) []stri
options = append(options, suggestion{s: straw, dist: distance})
}
}
sort.Slice(options, func(i, j int) bool { return options[i].dist < options[j].dist })
slices.SortFunc(options, func(a, b suggestion) int { return cmp.Compare(a.dist, b.dist) })
ret := make([]string, len(options))
for i, o := range options {
ret[i] = o.s
Expand Down
32 changes: 6 additions & 26 deletions src/core/build_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,23 +310,12 @@ func (label BuildLabel) Includes(that BuildLabel) bool {

// Compare compares this build label to another one (suitable for using with slices.SortFunc)
func (label BuildLabel) Compare(other BuildLabel) int {
if label.Subrepo != other.Subrepo {
if label.Subrepo < other.Subrepo {
return -1
}
return 1
} else if label.PackageName != other.PackageName {
if label.PackageName < other.PackageName {
return -1
}
return 1
} else if label.Name != other.Name {
if label.Name < other.Name {
return -1
}
return 1
if c := strings.Compare(label.Subrepo, other.Subrepo); c != 0 {
return c
} else if c := strings.Compare(label.PackageName, other.PackageName); c != 0 {
return c
}
return 0
return strings.Compare(label.Name, other.Name)
}

// Less returns true if this build label would sort less than another one.
Expand Down Expand Up @@ -613,18 +602,9 @@ func LooksLikeABuildLabel(str string) bool {
return strings.HasPrefix(str, "//") || strings.HasPrefix(str, ":") || (strings.HasPrefix(str, "@") && (strings.ContainsRune(str, ':') || strings.Contains(str, "//")))
}

// BuildLabels makes slices of build labels sortable.
// BuildLabels is a slice of build labels.
type BuildLabels []BuildLabel

func (slice BuildLabels) Len() int {
return len(slice)
}
func (slice BuildLabels) Less(i, j int) bool {
return slice[i].Less(slice[j])
}
func (slice BuildLabels) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
func (slice BuildLabels) String() string {
s := make([]string, len(slice))
for i, l := range slice {
Expand Down
22 changes: 7 additions & 15 deletions src/core/build_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ func (target *BuildTarget) Dependencies(graph *BuildGraph) ([]*BuildTarget, []Bu
labels[i] = dep.Label
}
target.mutex.RUnlock()
ret := make(BuildTargets, 0, len(labels))
ret := make([]*BuildTarget, 0, len(labels))
var unresolved []BuildLabel
for _, l := range labels {
depTarget := graph.Target(l)
Expand All @@ -595,7 +595,7 @@ func (target *BuildTarget) Dependencies(graph *BuildGraph) ([]*BuildTarget, []Bu
}
}
}
sort.Sort(ret)
slices.SortFunc(ret, compareTargets)
return ret, unresolved
}

Expand All @@ -612,7 +612,7 @@ func (target *BuildTarget) ExternalDependencies(graph *BuildGraph) ([]*BuildTarg
labels[i] = dep.Label
}
target.mutex.RUnlock()
ret := make(BuildTargets, 0, len(labels))
ret := make([]*BuildTarget, 0, len(labels))
var unresolved []BuildLabel
for _, l := range labels {
depTarget := graph.Target(l)
Expand All @@ -633,7 +633,7 @@ func (target *BuildTarget) ExternalDependencies(graph *BuildGraph) ([]*BuildTarg
}
}
}
sort.Sort(ret)
slices.SortFunc(ret, compareTargets)
return ret, unresolved
}

Expand Down Expand Up @@ -2064,17 +2064,9 @@ func (target *BuildTarget) CheckLicences(config *Configuration) (string, error)
return "", nil
}

// BuildTargets makes a slice of build targets sortable by their labels.
type BuildTargets []*BuildTarget

func (slice BuildTargets) Len() int {
return len(slice)
}
func (slice BuildTargets) Less(i, j int) bool {
return slice[i].Label.Less(slice[j].Label)
}
func (slice BuildTargets) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
// compareTargets compares two build targets by their labels (suitable for using with slices.SortFunc)
func compareTargets(a, b *BuildTarget) int {
return a.Label.Compare(b.Label)
}

const sourceListFileDir = "_plz"
Expand Down
7 changes: 2 additions & 5 deletions src/core/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package core
import (
"iter"
"slices"
"sort"

"github.com/thought-machine/please/src/cmap"
)
Expand Down Expand Up @@ -141,11 +140,9 @@ func (graph *BuildGraph) SubrepoOrWait(subrepo string) (*Subrepo, <-chan struct{
}

// AllTargets returns a consistently ordered slice of all the targets in the graph.
func (graph *BuildGraph) AllTargets() BuildTargets {
func (graph *BuildGraph) AllTargets() []*BuildTarget {
targets := graph.targets.Values()
sort.Slice(targets, func(i, j int) bool {
return targets[i].Label.Less(targets[j].Label)
})
slices.SortFunc(targets, compareTargets)
return targets
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package core
import (
"fmt"
"path/filepath"
"sort"
"slices"
"strings"
"sync"

Expand Down Expand Up @@ -165,14 +165,14 @@ func (pkg *Package) MustRegisterOutput(state *BuildState, fileName string, targe
// AllChildren returns all child targets of the given one.
// The given target is included as well.
func (pkg *Package) AllChildren(target *BuildTarget) []*BuildTarget {
ret := BuildTargets{}
ret := []*BuildTarget{}
parent := target.Label.Parent()
for _, t := range pkg.targets {
if t.Label.Parent() == parent {
ret = append(ret, t)
}
}
sort.Sort(ret)
slices.SortFunc(ret, compareTargets)
return ret
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
iofs "io/fs"
"iter"
"path/filepath"
"sort"
"slices"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -594,7 +594,7 @@ func (state *BuildState) expandOriginalPseudoTarget(label BuildLabel, justTests
}
}
}
sort.Sort(ret)
slices.SortFunc(ret, BuildLabel.Compare)
return ret
}

Expand Down
15 changes: 8 additions & 7 deletions src/fs/sort.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package fs

import (
"sort"
"cmp"
"slices"
"strings"
)

// SortPaths sorts a list of filepaths in lexicographic order by component
// (i.e. so all files in a directory sort after all subdirectories).
func SortPaths(files []string) []string {
sort.Slice(files, func(i, j int) bool {
si, sj := commonPrefix(strings.Split(files[i], "/"), strings.Split(files[j], "/"))
slices.SortFunc(files, func(a, b string) int {
si, sj := commonPrefix(strings.Split(a, "/"), strings.Split(b, "/"))
if len(si) == 1 && len(sj) > 1 {
return false // si is a leaf and sj is not
return 1 // si is a leaf and sj is not
} else if len(sj) == 1 && len(si) > 1 {
return true // sj is a leaf and si is not
return -1 // sj is a leaf and si is not
} else if len(si) == 0 || len(sj) == 0 {
return len(si) < len(sj) // one or the other is empty.
return cmp.Compare(len(si), len(sj)) // one or the other is empty.
}
return si[0] < sj[0]
return strings.Compare(si[0], sj[0])
})
return files
}
Expand Down
3 changes: 2 additions & 1 deletion src/gc/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"fmt"
"os"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -140,7 +141,7 @@ func targetsToRemove(graph *core.BuildGraph, filter, targets, targetsToKeep []co
}
}
}
sort.Sort(ret)
slices.SortFunc(ret, core.BuildLabel.Compare)
sort.Strings(retSrcs)
log.Notice("%d targets to remove", len(ret))
log.Notice("%d sources to remove", len(retSrcs))
Expand Down
4 changes: 2 additions & 2 deletions src/output/shell_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"math/rand"
"os"
"path/filepath"
"sort"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -153,7 +153,7 @@ func printTestResults(state *core.BuildState, failedTargets map[core.BuildLabel]
for t := range failedTargets {
targets = append(targets, t)
}
sort.Sort(targets)
slices.SortFunc(targets, core.BuildLabel.Compare)
for _, failed := range targets {
target := state.Graph.TargetOrDie(failed)
if target.Test.Results.Failures() == 0 && target.Test.Results.Errors() == 0 {
Expand Down
6 changes: 4 additions & 2 deletions src/parse/asp/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
package main

import (
"cmp"
"context"
"fmt"
iofs "io/fs"
"os"
"path/filepath"
"reflect"
"regexp"
"slices"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -158,8 +160,8 @@ func checkAST(stmts []*asp.Statement, parentScopes ...map[string]assignment) (er
errs = append(errs, assign)
}
}
sort.Slice(errs, func(i, j int) bool {
return errs[i].Pos < errs[j].Pos
slices.SortFunc(errs, func(a, b assignment) int {
return cmp.Compare(a.Pos, b.Pos)
})
return errs
}
Expand Down
4 changes: 2 additions & 2 deletions src/query/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"crypto/sha1"
"path/filepath"
"sort"
"slices"

"github.com/thought-machine/please/src/build"
"github.com/thought-machine/please/src/core"
Expand Down Expand Up @@ -83,7 +83,7 @@ func changedTargets(state *core.BuildState, files []string, changed map[*core.Bu
ls = append(ls, l)
}
}
sort.Sort(ls)
slices.SortFunc(ls, core.BuildLabel.Compare)
return ls
}

Expand Down
Loading
Loading