Skip to content

Commit

Permalink
feat: new unsafex pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaost committed Nov 26, 2024
1 parent b8f4557 commit 0e587e6
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 154 deletions.
25 changes: 0 additions & 25 deletions .github/workflows/release-check.yml

This file was deleted.

41 changes: 7 additions & 34 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
# Options for analysis running.
run:
# include `vendor` `third_party` `testdata` `examples` `Godeps` `builtin`
skip-dirs-use-default: true
skip-dirs:
- kitex_gen
skip-files:
- ".*\\.mock\\.go$"
# output configuration options
output:
# Format: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions
format: colored-line-number
# All available settings of specific linters.
# Refer to https://golangci-lint.run/usage/linters
linters-settings:
gofumpt:
# Choose whether to use the extra rules.
# Default: false
extra-rules: true
govet:
# Disable analyzers by name.
# Run `go tool vet help` to see all analyzers.
disable:
- stdmethods
linters:
linters: # https://golangci-lint.run/usage/linters/
disable-all: true
enable:
- gofumpt
- goimports
- gofmt
disable:
- errcheck
- typecheck
- deadcode
- varcheck
- gosimple
- govet
- ineffassign
- staticcheck
issues:
exclude-use-default: true
- unused
- unconvert
4 changes: 2 additions & 2 deletions container/strmap/strmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"sort"
"strings"

"github.com/cloudwego/gopkg/internal/hack"
"github.com/cloudwego/gopkg/internal/hash/maphash"
"github.com/cloudwego/gopkg/internal/strstore"
"github.com/cloudwego/gopkg/unsafex"
)

// StrMap represents GC friendly readonly string map implementation.
Expand Down Expand Up @@ -133,7 +133,7 @@ func (m *StrMap[V]) Len() int {
// It panics if i is not in the range [0, Len()).
func (m *StrMap[V]) Item(i int) (string, V) {
e := &m.items[i]
return hack.ByteSliceToString(m.data[e.off : e.off+int(e.sz)]), e.v
return unsafex.BinaryToString(m.data[e.off : e.off+int(e.sz)]), e.v
}

type itemsBySlot[V any] []mapItem[V]
Expand Down
10 changes: 5 additions & 5 deletions container/strmap/strmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ import (
"runtime"
"testing"

"github.com/cloudwego/gopkg/internal/hack"
"github.com/cloudwego/gopkg/unsafex"
"github.com/stretchr/testify/require"
)

func randStrings(m, n int) []string {
b := make([]byte, m*n)
rand.Read(b)
_, _ = rand.Read(b)
ret := make([]string, 0, n)
for i := 0; i < n; i++ {
s := b[m*i:]
s = s[:m]
ret = append(ret, hack.ByteSliceToString(s))
ret = append(ret, unsafex.BinaryToString(s))
}
return ret
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func BenchmarkLoadFromMap(b *testing.B) {
p := New[uint]()
b.ResetTimer()
for i := 0; i < b.N; i++ {
p.LoadFromMap(m)
_ = p.LoadFromMap(m)
}
}

Expand All @@ -188,7 +188,7 @@ func BenchmarkLoadFromSlice(b *testing.B) {
p := New[int]()
b.ResetTimer()
for i := 0; i < b.N; i++ {
p.LoadFromSlice(kk, vv)
_ = p.LoadFromSlice(kk, vv)
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/strstore/strstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func BenchmarkStrStoreLoad(b *testing.B) {
strStore := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
strStore.Load(ss)
_, _ = strStore.Load(ss)
}
}

Expand Down
6 changes: 3 additions & 3 deletions protocol/thrift/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"unsafe"

"github.com/bytedance/gopkg/lang/span"
"github.com/cloudwego/gopkg/internal/hack"
"github.com/cloudwego/gopkg/unsafex"
)

var (
Expand Down Expand Up @@ -136,7 +136,7 @@ func (p BinaryProtocol) WriteStringNocopy(buf []byte, w NocopyWriter, v string)
return p.WriteString(buf, v)
}
binary.BigEndian.PutUint32(buf, uint32(len(v)))
_ = w.WriteDirect(hack.StringToByteSlice(v), len(buf[4:])) // always err == nil ?
_ = w.WriteDirect(unsafex.StringToBinary(v), len(buf[4:])) // always err == nil ?
return 4
}

Expand Down Expand Up @@ -357,7 +357,7 @@ func (p BinaryProtocol) ReadString(buf []byte) (s string, l int, err error) {
}
if spanCacheEnable {
data := spanCache.Copy(buf[4:l])
s = hack.ByteSliceToString(data)
s = unsafex.BinaryToString(data)
} else {
s = string(buf[4:l])
}
Expand Down
52 changes: 26 additions & 26 deletions protocol/thrift/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ func TestBinary(t *testing.T) {

b := Binary.AppendBool([]byte(nil), true)
b = Binary.AppendBool(b, false)
require.Equal(t, int(sz), len(b))
require.Equal(t, sz, len(b))

b1 := make([]byte, sz)
l := Binary.WriteBool(b1, true)
l += Binary.WriteBool(b1[l:], false)
require.Equal(t, int(sz), l)
require.Equal(t, sz, l)
require.Equal(t, b, b1)

v, l, _ := Binary.ReadBool(b)
Expand All @@ -53,11 +53,11 @@ func TestBinary(t *testing.T) {
sz := Binary.ByteLength()

b := Binary.AppendByte([]byte(nil), 1)
require.Equal(t, int(sz), len(b))
require.Equal(t, sz, len(b))

b1 := make([]byte, sz)
l := Binary.WriteByte(b1, 1)
require.Equal(t, int(sz), l)
require.Equal(t, sz, l)
require.Equal(t, b, b1)

v, l, _ := Binary.ReadByte(b)
Expand All @@ -73,11 +73,11 @@ func TestBinary(t *testing.T) {
sz := Binary.I16Length()

b := Binary.AppendI16([]byte(nil), testv)
require.Equal(t, int(sz), len(b))
require.Equal(t, sz, len(b))

b1 := make([]byte, sz)
l := Binary.WriteI16(b1, testv)
require.Equal(t, int(sz), l)
require.Equal(t, sz, l)
require.Equal(t, b, b1)

v, l, _ := Binary.ReadI16(b)
Expand All @@ -93,11 +93,11 @@ func TestBinary(t *testing.T) {
sz := Binary.I32Length()

b := Binary.AppendI32([]byte(nil), testv)
require.Equal(t, int(sz), len(b))
require.Equal(t, sz, len(b))

b1 := make([]byte, sz)
l := Binary.WriteI32(b1, testv)
require.Equal(t, int(sz), l)
require.Equal(t, sz, l)
require.Equal(t, b, b1)

v, l, _ := Binary.ReadI32(b)
Expand All @@ -113,11 +113,11 @@ func TestBinary(t *testing.T) {
sz := Binary.I64Length()

b := Binary.AppendI64([]byte(nil), testv)
require.Equal(t, int(sz), len(b))
require.Equal(t, sz, len(b))

b1 := make([]byte, sz)
l := Binary.WriteI64(b1, testv)
require.Equal(t, int(sz), l)
require.Equal(t, sz, l)
require.Equal(t, b, b1)

v, l, _ := Binary.ReadI64(b)
Expand All @@ -133,11 +133,11 @@ func TestBinary(t *testing.T) {
sz := Binary.DoubleLength()

b := Binary.AppendDouble([]byte(nil), testv)
require.Equal(t, int(sz), len(b))
require.Equal(t, sz, len(b))

b1 := make([]byte, sz)
l := Binary.WriteDouble(b1, testv)
require.Equal(t, int(sz), l)
require.Equal(t, sz, l)
require.Equal(t, b, b1)

v, l, _ := Binary.ReadDouble(b)
Expand All @@ -153,11 +153,11 @@ func TestBinary(t *testing.T) {
sz := Binary.BinaryLength(testv)

b := Binary.AppendBinary([]byte(nil), testv)
require.Equal(t, int(sz), len(b))
require.Equal(t, sz, len(b))

b1 := make([]byte, sz)
l := Binary.WriteBinaryNocopy(b1, nil, testv)
require.Equal(t, int(sz), l)
require.Equal(t, sz, l)
require.Equal(t, b, b1)

v, l, _ := Binary.ReadBinary(b)
Expand All @@ -173,11 +173,11 @@ func TestBinary(t *testing.T) {
sz := Binary.StringLength(testv)

b := Binary.AppendString([]byte(nil), testv)
require.Equal(t, int(sz), len(b))
require.Equal(t, sz, len(b))

b1 := make([]byte, sz)
l := Binary.WriteStringNocopy(b1, nil, testv)
require.Equal(t, int(sz), l)
require.Equal(t, sz, l)
require.Equal(t, b, b1)

v, l, _ := Binary.ReadString(b)
Expand All @@ -193,11 +193,11 @@ func TestBinary(t *testing.T) {
sz := Binary.MessageBeginLength(testname)

b := Binary.AppendMessageBegin([]byte(nil), testname, testtyp, testseq)
require.Equal(t, int(sz), len(b))
require.Equal(t, sz, len(b))

b1 := make([]byte, sz)
l := Binary.WriteMessageBegin(b1, testname, testtyp, testseq)
require.Equal(t, int(sz), l)
require.Equal(t, sz, l)
require.Equal(t, b, b1)

name, typ, seq, l, _ := Binary.ReadMessageBegin(b)
Expand All @@ -216,12 +216,12 @@ func TestBinary(t *testing.T) {

b := Binary.AppendFieldBegin([]byte(nil), testtyp, testfid)
b = Binary.AppendFieldStop(b)
require.Equal(t, int(sz), len(b))
require.Equal(t, sz, len(b))

b1 := make([]byte, sz)
l := Binary.WriteFieldBegin(b1, testtyp, testfid)
l += Binary.WriteFieldStop(b1[l:])
require.Equal(t, int(sz), l)
require.Equal(t, sz, l)
require.Equal(t, b, b1)

typ, fid, l, _ := Binary.ReadFieldBegin(b)
Expand All @@ -243,11 +243,11 @@ func TestBinary(t *testing.T) {
sz := Binary.MapBeginLength()

b := Binary.AppendMapBegin([]byte(nil), testkt, testvt, testsize)
require.Equal(t, int(sz), len(b))
require.Equal(t, sz, len(b))

b1 := make([]byte, sz)
l := Binary.WriteMapBegin(b1, testkt, testvt, testsize)
require.Equal(t, int(sz), l)
require.Equal(t, sz, l)
require.Equal(t, b, b1)

kt, vt, size, l, _ := Binary.ReadMapBegin(b)
Expand All @@ -265,11 +265,11 @@ func TestBinary(t *testing.T) {
sz := Binary.ListBeginLength()

b := Binary.AppendListBegin([]byte(nil), testvt, testsize)
require.Equal(t, int(sz), len(b))
require.Equal(t, sz, len(b))

b1 := make([]byte, sz)
l := Binary.WriteListBegin(b1, testvt, testsize)
require.Equal(t, int(sz), l)
require.Equal(t, sz, l)
require.Equal(t, b, b1)

vt, size, l, _ := Binary.ReadListBegin(b)
Expand All @@ -286,11 +286,11 @@ func TestBinary(t *testing.T) {
sz := Binary.SetBeginLength()

b := Binary.AppendSetBegin([]byte(nil), testvt, testsize)
require.Equal(t, int(sz), len(b))
require.Equal(t, sz, len(b))

b1 := make([]byte, sz)
l := Binary.WriteSetBegin(b1, testvt, testsize)
require.Equal(t, int(sz), l)
require.Equal(t, sz, l)
require.Equal(t, b, b1)

vt, size, l, _ := Binary.ReadSetBegin(b)
Expand Down
6 changes: 3 additions & 3 deletions protocol/thrift/bufferreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"github.com/bytedance/gopkg/lang/dirtmake"
"github.com/cloudwego/gopkg/bufiox"
"github.com/cloudwego/gopkg/internal/hack"
"github.com/cloudwego/gopkg/unsafex"
)

// BufferReader represents a reader for binary protocol
Expand Down Expand Up @@ -162,7 +162,7 @@ func (r *BufferReader) ReadString() (s string, err error) {
if err != nil {
return "", err
}
return hack.ByteSliceToString(b), nil
return unsafex.BinaryToString(b), nil
}

// ReadMessageBegin ...
Expand Down Expand Up @@ -274,7 +274,7 @@ func (r *BufferReader) skipType(t TType, maxdepth int) error {
}
ksz, vsz := int(typeToSize[kt]), int(typeToSize[vt])
if ksz > 0 && vsz > 0 {
return r.skipn(int(sz) * (ksz + vsz))
return r.skipn(sz * (ksz + vsz))
}
for j := 0; j < sz; j++ {
if ksz > 0 {
Expand Down
4 changes: 2 additions & 2 deletions protocol/thrift/bufferwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"sync"

"github.com/cloudwego/gopkg/bufiox"
"github.com/cloudwego/gopkg/internal/hack"
"github.com/cloudwego/gopkg/unsafex"
)

type BufferWriter struct {
Expand Down Expand Up @@ -117,7 +117,7 @@ func (w *BufferWriter) WriteBinary(v []byte) error {
}

func (w *BufferWriter) WriteString(v string) error {
return w.WriteBinary(hack.StringToByteSlice(v))
return w.WriteBinary(unsafex.StringToBinary(v))
}

func (w *BufferWriter) WriteBool(v bool) error {
Expand Down
Loading

0 comments on commit 0e587e6

Please sign in to comment.