Skip to content

Commit 7389cb7

Browse files
carli2claude
andauthored
Replace stdlib sort with github.com/carli2/hybridsort (#83)
Large-data hot paths (index.go, shard.go) use HybridSort with value-based less for natural run detection + buffered merge. All other sort.Slice/sort.Sort calls use hybridsort.Slice/SliceStable. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 69b40c9 commit 7389cb7

3 files changed

Lines changed: 8 additions & 17 deletions

File tree

go.sum

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.41.6 h1:5fFjR/ToSOzB2OQ/XqWpZBmNvmP/
3838
github.com/aws/aws-sdk-go-v2/service/sts v1.41.6/go.mod h1:qgFDZQSD/Kys7nJnVqYlWKnh0SSdMjAi0uSwON4wgYQ=
3939
github.com/aws/smithy-go v1.24.0 h1:LpilSUItNPFr1eY85RYgTIg5eIEPtvFbskaFcmmIUnk=
4040
github.com/aws/smithy-go v1.24.0/go.mod h1:LEj2LM3rBRQJxPZTB4KuzZkaZYnZPnvgIhb4pu07mx0=
41-
github.com/carli2/hybridsort v0.0.0-20260319191746-8429712d7a1c h1:h9PAuqFyYi8dCWABYlaiJjzpY9KmzXZsNUPU6RnZrGI=
42-
github.com/carli2/hybridsort v0.0.0-20260319191746-8429712d7a1c/go.mod h1:ZSL9x9C2qLz4fL7VZdCs7QZCqNBkBZkcYtY0Fcw2Ezo=
43-
github.com/carli2/hybridsort v0.0.0-20260319194603-ddd18ae27ffa h1:nS1j57V0eKCC2ydo3ja9QTP2hl0EI/vntgp8cVJ1rVw=
44-
github.com/carli2/hybridsort v0.0.0-20260319194603-ddd18ae27ffa/go.mod h1:ZSL9x9C2qLz4fL7VZdCs7QZCqNBkBZkcYtY0Fcw2Ezo=
45-
github.com/carli2/hybridsort v0.0.0-20260319211747-bc628a3877c1 h1:YhHOYcRJ5z+Vc2ly2qJnAAF15qPYxQR39sibTmDfKUM=
46-
github.com/carli2/hybridsort v0.0.0-20260319211747-bc628a3877c1/go.mod h1:ZSL9x9C2qLz4fL7VZdCs7QZCqNBkBZkcYtY0Fcw2Ezo=
47-
github.com/carli2/hybridsort v0.0.0-20260319212745-ac6f7edeb1b1 h1:L5JNXOg6HLjGecOzzrxR9itw3Dal1NSd4nfXlPG4/tw=
48-
github.com/carli2/hybridsort v0.0.0-20260319212745-ac6f7edeb1b1/go.mod h1:ZSL9x9C2qLz4fL7VZdCs7QZCqNBkBZkcYtY0Fcw2Ezo=
4941
github.com/carli2/hybridsort v0.0.0-20260319220658-610599b2851c h1:KLKFOH2gyHQ03+t9t29pQufv+GGVRBYCOTw3VEqFOX8=
5042
github.com/carli2/hybridsort v0.0.0-20260319220658-610599b2851c/go.mod h1:ZSL9x9C2qLz4fL7VZdCs7QZCqNBkBZkcYtY0Fcw2Ezo=
5143
github.com/carli2/text v0.34.1-0.20260305004517-20c7a406302a h1:qnerlW5tSOXN0OYjUAscEZWzTzTzSP+ASRJwpUdfya4=

storage/index.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ package storage
1919

2020
import "sort"
2121
import "sync"
22-
import "github.com/carli2/hybridsort"
2322
import "sync/atomic"
2423
import "time"
2524
import "strings"
2625

26+
import "github.com/carli2/hybridsort"
2727
import "github.com/google/btree"
2828
import "github.com/launix-de/memcp/scm"
2929

@@ -507,16 +507,16 @@ func (s *StorageIndex) buildIndex(cols []colGetter) {
507507
}
508508
// sort indexes; skip non-sorted matcher columns (they don't affect
509509
// sort order, they are query-level overlays for pruning)
510-
hybridsort.Slice(tmp, func(i, j int) bool {
510+
hybridsort.HybridSort(tmp, func(a, b uint32) bool {
511511
for colIdx, g := range cols {
512512
if len(s.ColMatchers) > colIdx && !s.ColMatchers[colIdx].IsSorted() {
513513
continue
514514
}
515-
a := g.get(tmp[i])
516-
b := g.get(tmp[j])
517-
if scm.Less(a, b) {
515+
va := g.get(a)
516+
vb := g.get(b)
517+
if scm.Less(va, vb) {
518518
return true // less
519-
} else if !scm.Equal(a, b) {
519+
} else if !scm.Equal(va, vb) {
520520
return false // greater
521521
}
522522
// otherwise: next iteration

storage/shard.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ Copyright (C) 2023-2026 Carl-Philip Hänsch
1717
package storage
1818

1919
import "fmt"
20-
import "github.com/carli2/hybridsort"
2120
import "sync"
2221
import "sync/atomic"
2322
import "time"
@@ -26,6 +25,7 @@ import "reflect"
2625
import "runtime"
2726
import "encoding/json"
2827
import "encoding/binary"
28+
import "github.com/carli2/hybridsort"
2929
import "github.com/google/uuid"
3030
import "github.com/jtolds/gls"
3131
import "github.com/launix-de/memcp/scm"
@@ -2078,10 +2078,9 @@ func (t *storageShard) rebuild(all bool) *storageShard {
20782078
}
20792079
}
20802080
idxCols := idx.Cols
2081-
hybridsort.Slice(sortPerm, func(a, b int) bool {
2081+
hybridsort.HybridSort(sortPerm, func(idA, idB uint32) bool {
20822082
for _, colName := range idxCols {
20832083
var va, vb scm.Scmer
2084-
idA, idB := sortPerm[a], sortPerm[b]
20852084
if idA < t.main_count {
20862085
va = t.columns[colName].GetValue(idA)
20872086
} else {

0 commit comments

Comments
 (0)