use slices.Sort to eliminate bounds check (#1128)

Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
This commit is contained in:
Weizhen Wang 2024-01-23 11:24:15 +08:00 committed by GitHub
parent 5d0ae57f22
commit fd2fc84032
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 14 additions and 22 deletions

View File

@ -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 ./...
@ -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 ./...
@ -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

View File

@ -4,7 +4,7 @@ linters:
disable-all: true
enable:
- bodyclose
- depguard
#- depguard
- exportloopref
- gofmt
- goimports

View File

@ -38,6 +38,7 @@ import (
"bytes"
"context"
"math"
"slices"
"sort"
"sync"
"time"
@ -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 {

View File

@ -38,7 +38,6 @@ import (
"bytes"
"fmt"
"math"
"reflect"
"sync"
"unsafe"
@ -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 (

View File

@ -43,6 +43,7 @@ import (
"math"
"math/rand"
"runtime/trace"
"slices"
"sort"
"sync"
"sync/atomic"
@ -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++ {

View File

@ -38,7 +38,6 @@ import (
"context"
"encoding/hex"
"fmt"
"reflect"
"strconv"
"strings"
"time"
@ -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