Skip to content

Commit

Permalink
use slices.Sort to eliminate bounds check (#1128)
Browse files Browse the repository at this point in the history
Signed-off-by: Weizhen Wang <[email protected]>
  • Loading branch information
hawkingrei committed Jan 23, 2024
1 parent 5d0ae57 commit fd2fc84
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 22 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.21.0
go-version: 1.21.6

- name: Test
run: go test ./...
Expand All @@ -28,7 +28,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.21.0
go-version: 1.21.6

- name: Test with race
run: go test -race ./...
Expand All @@ -42,10 +42,10 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.21.0
go-version: 1.21.6

- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.51.2
version: v1.55.2

2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ linters:
disable-all: true
enable:
- bodyclose
- depguard
#- depguard
- exportloopref
- gofmt
- goimports
Expand Down
5 changes: 3 additions & 2 deletions internal/mockstore/mocktikv/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"bytes"
"context"
"math"
"slices"
"sort"
"sync"
"time"
Expand Down Expand Up @@ -358,8 +359,8 @@ func (c *Cluster) ScanRegions(startKey, endKey []byte, limit int, opts ...pd.Get
regions = append(regions, region)
}

sort.Slice(regions, func(i, j int) bool {
return bytes.Compare(regions[i].Meta.GetStartKey(), regions[j].Meta.GetStartKey()) < 0
slices.SortFunc(regions, func(i, j *Region) int {
return bytes.Compare(i.Meta.GetStartKey(), j.Meta.GetStartKey())
})

startPos := sort.Search(len(regions), func(i int) bool {
Expand Down
9 changes: 2 additions & 7 deletions internal/unionstore/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
"bytes"
"fmt"
"math"
"reflect"
"sync"
"unsafe"

Expand Down Expand Up @@ -837,12 +836,8 @@ func (n *memdbNode) setBlack() {
}

func (n *memdbNode) getKey() []byte {
var ret []byte
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&ret))
hdr.Data = uintptr(unsafe.Pointer(&n.flags)) + kv.FlagBytes
hdr.Len = int(n.klen)
hdr.Cap = int(n.klen)
return ret
base := unsafe.Add(unsafe.Pointer(&n.flags), kv.FlagBytes)
return unsafe.Slice((*byte)(base), int(n.klen))
}

const (
Expand Down
5 changes: 3 additions & 2 deletions txnkv/transaction/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"math"
"math/rand"
"runtime/trace"
"slices"
"sort"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -1314,8 +1315,8 @@ func deduplicateKeys(keys [][]byte) [][]byte {
return keys
}

sort.Slice(keys, func(i, j int) bool {
return bytes.Compare(keys[i], keys[j]) < 0
slices.SortFunc(keys, func(i, j []byte) int {
return bytes.Compare(i, j)
})
deduped := keys[:1]
for i := 1; i < len(keys); i++ {
Expand Down
7 changes: 1 addition & 6 deletions util/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
"context"
"encoding/hex"
"fmt"
"reflect"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -168,11 +167,7 @@ func String(b []byte) (s string) {
if len(b) == 0 {
return ""
}
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
pstring.Data = pbytes.Data
pstring.Len = pbytes.Len
return
return unsafe.String(unsafe.SliceData(b), len(b))
}

// ToUpperASCIIInplace bytes.ToUpper but zero-cost
Expand Down

0 comments on commit fd2fc84

Please sign in to comment.