Skip to content

Commit c8e18f3

Browse files
committed
slices: use old non-generic sort code
Saves 220-360 KB from tailscaled. DO NOT SUBMIT; draft
1 parent aa85d15 commit c8e18f3

File tree

3 files changed

+510
-6
lines changed

3 files changed

+510
-6
lines changed

src/go/build/deps_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ var depsRules = `
111111
< errors
112112
< internal/oserror;
113113
114-
cmp, runtime, math/bits
114+
cmp, runtime, math/bits, internal/reflectlite
115115
< iter
116116
< maps, slices;
117117

src/slices/sort.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ package slices
88

99
import (
1010
"cmp"
11+
"internal/reflectlite"
1112
"math/bits"
1213
)
1314

1415
// Sort sorts a slice of any ordered type in ascending order.
1516
// When sorting floating-point numbers, NaNs are ordered before other values.
1617
func Sort[S ~[]E, E cmp.Ordered](x S) {
17-
n := len(x)
18-
pdqsortOrdered(x, 0, n, bits.Len(uint(n)))
18+
sortSlice(x, func(i, j int) bool {
19+
return cmp.Less(x[i], x[j])
20+
})
1921
}
2022

2123
// SortFunc sorts the slice x in ascending order as determined by the cmp
@@ -28,14 +30,36 @@ func Sort[S ~[]E, E cmp.Ordered](x S) {
2830
// See https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings.
2931
// The function should return 0 for incomparable items.
3032
func SortFunc[S ~[]E, E any](x S, cmp func(a, b E) int) {
31-
n := len(x)
32-
pdqsortCmpFunc(x, 0, n, bits.Len(uint(n)), cmp)
33+
sortSlice(x, func(i, j int) bool {
34+
return cmp(x[i], x[j]) < 0
35+
})
3336
}
3437

3538
// SortStableFunc sorts the slice x while keeping the original order of equal
3639
// elements, using cmp to compare elements in the same way as [SortFunc].
3740
func SortStableFunc[S ~[]E, E any](x S, cmp func(a, b E) int) {
38-
stableCmpFunc(x, len(x), cmp)
41+
sortSliceStable(x, func(i, j int) bool {
42+
return cmp(x[i], x[j]) < 0
43+
})
44+
}
45+
46+
type lessSwap struct {
47+
Less func(i, j int) bool
48+
Swap func(i, j int)
49+
}
50+
51+
func sortSlice(x any, less func(i, j int) bool) {
52+
rv := reflectlite.ValueOf(x)
53+
swap := reflectlite.Swapper(x)
54+
length := rv.Len()
55+
limit := bits.Len(uint(length))
56+
pdqsort_func(lessSwap{less, swap}, 0, length, limit)
57+
}
58+
59+
func sortSliceStable(x any, less func(i, j int) bool) {
60+
rv := reflectlite.ValueOf(x)
61+
swap := reflectlite.Swapper(x)
62+
stable_func(lessSwap{less, swap}, rv.Len())
3963
}
4064

4165
// IsSorted reports whether x is sorted in ascending order.

0 commit comments

Comments
 (0)