Skip to content

Commit 0078d26

Browse files
committed
internal/core/export: swap sort.Slice APIs for slices.Sort
The former uses reflection rather than generics, so a lot of the slices end up leaking to the heap and allocating. Moreover, the latter uses cmp functions returning integers, so fewer comparison calls are needed. Given that JSON marshalling needs to visit all struct fields in sorted order, this causes a minor speed-up there. │ old │ new │ │ sec/op │ sec/op vs base │ LargeValueMarshalJSON-8 6.484m ± 1% 6.370m ± 1% -1.77% (p=0.004 n=6) │ old │ new │ │ B/op │ B/op vs base │ LargeValueMarshalJSON-8 4.224Mi ± 0% 4.178Mi ± 0% -1.09% (p=0.002 n=6) │ old │ new │ │ allocs/op │ allocs/op vs base │ LargeValueMarshalJSON-8 62.27k ± 0% 60.26k ± 0% -3.22% (p=0.002 n=6) Signed-off-by: Daniel Martí <[email protected]> Change-Id: I12dbd3bdcfe7ba5f8ef66500203f08f1b5c676ec Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201825 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent 9c3e156 commit 0078d26

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

internal/core/export/expr.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
package export
1616

1717
import (
18+
"cmp"
1819
"fmt"
19-
"sort"
20+
"slices"
2021

2122
"cuelang.org/go/cue/ast"
2223
"cuelang.org/go/cue/token"
@@ -184,17 +185,20 @@ func (x *exporter) mergeValues(label adt.Feature, src *adt.Vertex, a []conjunct,
184185
// Sort fields in case features lists are missing to ensure
185186
// predictability. Also sort in reverse order, so that bugs
186187
// are more likely exposed.
187-
sort.Slice(fields, func(i, j int) bool {
188-
return fields[i] > fields[j]
188+
slices.SortFunc(fields, func(f1, f2 adt.Feature) int {
189+
return -cmp.Compare(f1, f2)
189190
})
190191

191192
if adt.DebugSort == 0 {
192193
m := sortArcs(extractFeatures(e.structs))
193-
sort.SliceStable(fields, func(i, j int) bool {
194-
if m[fields[j]] == 0 {
195-
return m[fields[i]] != 0
194+
slices.SortStableFunc(fields, func(f1, f2 adt.Feature) int {
195+
if m[f2] == 0 {
196+
if m[f1] == 0 {
197+
return +1
198+
}
199+
return -1
196200
}
197-
return m[fields[i]] > m[fields[j]]
201+
return -cmp.Compare(m[f1], m[f2])
198202
})
199203
} else {
200204
adt.DebugSortFields(e.ctx, fields)

internal/core/export/toposort.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
package export
1616

1717
import (
18-
"sort"
18+
"cmp"
19+
"slices"
1920

2021
"cuelang.org/go/internal/core/adt"
2122
)
@@ -88,7 +89,7 @@ func sortedArcsFromMap(m map[adt.Feature]int) []adt.Feature {
8889
a = append(a, k)
8990
}
9091

91-
sort.Slice(a, func(i, j int) bool { return m[a[i]] > m[a[j]] })
92+
slices.SortFunc(a, func(a1, a2 adt.Feature) int { return -cmp.Compare(m[a1], m[a2]) })
9293

9394
return a
9495
}

0 commit comments

Comments
 (0)