Skip to content

Commit

Permalink
Changing some unsafe use.
Browse files Browse the repository at this point in the history
  • Loading branch information
can1357 committed Mar 4, 2024
1 parent e871e3a commit c8e5092
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
11 changes: 6 additions & 5 deletions lb/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,25 @@ func (err SuppressedHttpError) Error() string {
return "suppressed http error"
}

type bufferType [32 * 1024]byte
const bufferLen = 32 * 1024

type bufferPool struct {
sync.Pool
}

func (b *bufferPool) Get() []byte {
ptr := b.Pool.Get()
return (*bufferType)(ptr.(unsafe.Pointer))[:]
return unsafe.Slice(ptr.(*byte), bufferLen)
}
func (b *bufferPool) Put(buf []byte) {
b.Pool.Put(unsafe.Pointer(&buf[0]))
b.Pool.Put(&buf[0])
}

var globalBufferPool = &bufferPool{
sync.Pool{
New: func() any {
buffer := new(bufferType)
return unsafe.Pointer(&buffer[0])
buf := make([]byte, bufferLen)
return &buf[0]
},
},
}
Expand Down
4 changes: 1 addition & 3 deletions ray/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package ray

import (
"fmt"

"get.pme.sh/pmesh/util"
)

type Host [3]byte
Expand All @@ -18,7 +16,7 @@ func ToHostString(s string) string {

func (p Host) String() string {
buf := p.Normal()
return util.UnsafeString(buf[:])
return string(buf[:])
}
func (p Host) Normal() Host {
for i, c := range p {
Expand Down
14 changes: 9 additions & 5 deletions ray/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ func (r ID) Timestamp() time.Time {
return r.ID.Timestamp()
}
func (r ID) String() string {
buf, _ := r.MarshalText()
return util.UnsafeString(buf)
arr := r.ToArray()
return string(arr[:])
}
func (r ID) MarshalText() (text []byte, err error) {
func (r ID) ToArray() (text [RayLength]byte) {
var buf [RayLength]byte

var sno [8]byte
Expand All @@ -42,7 +42,11 @@ func (r ID) MarshalText() (text []byte, err error) {
buf[17] = r.Host[0]
buf[18] = r.Host[1]
buf[19] = r.Host[2]
return buf[:], nil
return buf
}
func (r ID) MarshalText() (text []byte, err error) {
arr := r.ToArray()
return arr[:], nil
}
func (r *ID) UnmarshalText(text []byte) error {
if len(text) != RayLength || text[16] != '-' {
Expand Down Expand Up @@ -75,5 +79,5 @@ func (r *Generator) Next() string {
var sno [8]byte
binary.BigEndian.PutUint64(sno[:], uint64(snowflake.New()))
hex.Encode(buf[:16], sno[:])
return util.UnsafeString(buf[:])
return string(buf[:])
}
6 changes: 3 additions & 3 deletions xlog/parser_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package xlog

import (
"time"
"unsafe"

"get.pme.sh/pmesh/util"
"github.com/rs/zerolog"
"github.com/valyala/fastjson"
)
Expand All @@ -23,7 +23,7 @@ func ParseLine(line []byte) (ll Line, err error) {

func (ll Line) Domain() string {
sb := ll.GetStringBytes(zerolog.CallerFieldName)
return unsafe.String(&sb[0], len(sb))
return util.UnsafeString(sb)
}
func (ll Line) Time() time.Time {
f := ll.GetInt64(zerolog.TimestampFieldName)
Expand All @@ -37,7 +37,7 @@ func (ll Line) Level() Level {
if level != nil {
if level.Type() == fastjson.TypeString {
lstr := level.GetStringBytes()
if v, ok := unformatLevel[unsafe.String(&lstr[0], len(lstr))]; ok {
if v, ok := unformatLevel[util.UnsafeString(lstr)]; ok {
return v
}
} else if level.Type() == fastjson.TypeNumber {
Expand Down

0 comments on commit c8e5092

Please sign in to comment.