Skip to content

Commit

Permalink
fix: avoid boundary ptr (#88)
Browse files Browse the repository at this point in the history
* fxi: avoid boundary ptr

* update ci

* fix
  • Loading branch information
AsterDY authored Feb 14, 2025
1 parent d9ccd31 commit 405ebed
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/push-check-linux-x64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ jobs:
build:
strategy:
matrix:
go-version: [1.20.x]
runs-on: ubuntu-latest
go-version: [1.23.x]
runs-on: [macos-latest, ubuntu-latest]
steps:
- uses: actions/checkout@v2

Expand Down
36 changes: 21 additions & 15 deletions internal/json/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func decodeFalse(src string, pos int) (ret int) {

func decodeString(src string, pos int) (ret int, v string) {
ret, ep := skipString(src, pos)
if ret < 0 {
return ret, ""
}
if ep == -1 {
(*rt.GoString)(unsafe.Pointer(&v)).Ptr = rt.IndexChar(src, pos+1)
(*rt.GoString)(unsafe.Pointer(&v)).Len = ret - pos - 2
Expand Down Expand Up @@ -97,18 +100,18 @@ func isDigit(c byte) bool {
return c >= '0' && c <= '9'
}

//go:nocheckptr
func decodeInt64(src string, pos int) (ret int, v int64, err error) {
sp := uintptr(rt.IndexChar(src, pos))
ss := uintptr(sp)
se := uintptr(rt.IndexChar(src, len(src)))
sp := rt.IndexCharUint(src, pos)
se := rt.StrBoundary(src)
if uintptr(sp) >= se {
return -int(types.ERR_EOF), 0, nil
}

if c := *(*byte)(unsafe.Pointer(sp)); c == '-' {
sp += 1
}
if sp == se {
if sp >= se {
return -int(types.ERR_EOF), 0, nil
}

Expand All @@ -126,7 +129,7 @@ func decodeInt64(src string, pos int) (ret int, v int64, err error) {

var vv string
ret = int(uintptr(sp) - uintptr((*rt.GoString)(unsafe.Pointer(&src)).Ptr))
(*rt.GoString)(unsafe.Pointer(&vv)).Ptr = unsafe.Pointer(ss)
(*rt.GoString)(unsafe.Pointer(&vv)).Ptr = rt.IndexChar(src, pos)
(*rt.GoString)(unsafe.Pointer(&vv)).Len = ret - pos

v, err = strconv.ParseInt(vv, 10, 64)
Expand All @@ -146,10 +149,10 @@ func isNumberChars(c byte) bool {
return (c >= '0' && c <= '9') || c == '+' || c == '-' || c == 'e' || c == 'E' || c == '.'
}

//go:nocheckptr
func decodeFloat64(src string, pos int) (ret int, v float64, err error) {
sp := uintptr(rt.IndexChar(src, pos))
ss := uintptr(sp)
se := uintptr(rt.IndexChar(src, len(src)))
sp := rt.IndexCharUint(src, pos)
se := rt.StrBoundary(src)
if uintptr(sp) >= se {
return -int(types.ERR_EOF), 0, nil
}
Expand All @@ -169,7 +172,7 @@ func decodeFloat64(src string, pos int) (ret int, v float64, err error) {

var vv string
ret = int(uintptr(sp) - uintptr((*rt.GoString)(unsafe.Pointer(&src)).Ptr))
(*rt.GoString)(unsafe.Pointer(&vv)).Ptr = unsafe.Pointer(ss)
(*rt.GoString)(unsafe.Pointer(&vv)).Ptr = rt.IndexChar(src, pos)
(*rt.GoString)(unsafe.Pointer(&vv)).Len = ret - pos

v, err = strconv.ParseFloat(vv, 64)
Expand Down Expand Up @@ -290,9 +293,10 @@ func DecodeValue(src string, pos int) (ret int, v types.JsonState) {
}
}

//go:nocheckptr
func skipNumber(src string, pos int) (ret int) {
sp := uintptr(rt.IndexChar(src, pos))
se := uintptr(rt.IndexChar(src, len(src)))
sp := rt.IndexCharUint(src, pos)
se := rt.StrBoundary(src)
if uintptr(sp) >= se {
return -int(types.ERR_EOF)
}
Expand Down Expand Up @@ -354,13 +358,14 @@ func skipNumber(src string, pos int) (ret int) {
return int(uintptr(sp) - uintptr((*rt.GoString)(unsafe.Pointer(&src)).Ptr))
}

//go:nocheckptr
func skipString(src string, pos int) (ret int, ep int) {
if pos+1 >= len(src) {
return -int(types.ERR_EOF), -1
}

sp := uintptr(rt.IndexChar(src, pos))
se := uintptr(rt.IndexChar(src, len(src)))
sp := rt.IndexCharUint(src, pos)
se := rt.StrBoundary(src)

if *(*byte)(unsafe.Pointer(sp)) != '"' {
return -int(types.ERR_INVALID_CHAR), -1
Expand Down Expand Up @@ -391,13 +396,14 @@ func skipString(src string, pos int) (ret int, ep int) {
return int(uintptr(sp) - uintptr((*rt.GoString)(unsafe.Pointer(&src)).Ptr)), ep
}

//go:nocheckptr
func skipPair(src string, pos int, lchar byte, rchar byte) (ret int) {
if pos+1 >= len(src) {
return -int(types.ERR_EOF)
}

sp := uintptr(rt.IndexChar(src, pos))
se := uintptr(rt.IndexChar(src, len(src)))
sp := rt.IndexCharUint(src, pos)
se := rt.StrBoundary(src)

if *(*byte)(unsafe.Pointer(sp)) != lchar {
return -int(types.ERR_INVALID_CHAR)
Expand Down
11 changes: 5 additions & 6 deletions internal/json/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,12 @@ func EncodeString(buf []byte, val string) []byte {
return buf
}


func EncodeInt64(buf []byte, val int64) []byte {
i64toa(&buf, val)
return buf
}

func EncodeFloat64(buf []byte, val float64) ([]byte) {
func EncodeFloat64(buf []byte, val float64) []byte {
f64toa(&buf, val)
return buf
}
Expand Down Expand Up @@ -125,8 +124,8 @@ const _blankCharsMask = (1 << ' ') | (1 << '\t') | (1 << '\r') | (1 << '\n')

//go:nocheckptr
func SkipBlank(src string, pos int) int {
se := uintptr(rt.IndexChar(src, len(src)))
sp := uintptr(rt.IndexChar(src, pos))
se := rt.StrBoundary(src)
sp := rt.IndexCharUint(src, pos)

for sp < se {
if !IsSpace(*(*byte)(unsafe.Pointer(sp))) {
Expand All @@ -138,5 +137,5 @@ func SkipBlank(src string, pos int) int {
return -int(types.ERR_EOF)
}
runtime.KeepAlive(src)
return int(sp - uintptr(rt.IndexChar(src, 0)))
}
return int(sp - rt.IndexCharUint(src, 0))
}
11 changes: 11 additions & 0 deletions internal/rt/fastmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ func IndexChar(src string, index int) unsafe.Pointer {
return unsafe.Pointer(uintptr((*GoString)(unsafe.Pointer(&src)).Ptr) + uintptr(index))
}

func IndexCharUint(src string, index int) uintptr {
// if slice.Ptr == nil || slice.Cap == 0 {
// return nil
// }
return uintptr((*GoString)(unsafe.Pointer(&src)).Ptr) + uintptr(index)
}

func StrBoundary(src string) uintptr {
return uintptr((*GoString)(unsafe.Pointer(&src)).Ptr) + uintptr(len(src))
}

func GuardSlice(buf *[]byte, n int) {
c := cap(*buf)
l := len(*buf)
Expand Down

0 comments on commit 405ebed

Please sign in to comment.