From b641e5a71738aacdf401ee07149ec7a4fd2a51c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philip=20Dub=C3=A9?= Date: Wed, 13 Mar 2024 00:45:34 +0000 Subject: [PATCH 1/2] Remove reflect.SliceHeader usage reflect.SliceHeader is deprecated in favor of unsafe functions introduced in go 1.20 alloc's fheader was unused, so remove, leaving only unsafeFastStringToReadOnlyBytes to port --- alloc.go | 16 ++++------------ go.mod | 2 +- utils.go | 10 ++-------- 3 files changed, 7 insertions(+), 21 deletions(-) diff --git a/alloc.go b/alloc.go index 7a8cd63a..51731d41 100644 --- a/alloc.go +++ b/alloc.go @@ -1,7 +1,6 @@ package lua import ( - "reflect" "unsafe" ) @@ -13,9 +12,6 @@ type iface struct { const preloadLimit LNumber = 128 -var _fv float64 -var _uv uintptr - var preloads [int(preloadLimit)]LValue func init() { @@ -26,9 +22,8 @@ func init() { // allocator is a fast bulk memory allocator for the LValue. type allocator struct { - size int - fptrs []float64 - fheader *reflect.SliceHeader + size int + fptrs []float64 scratchValue LValue scratchValueP *iface @@ -36,11 +31,9 @@ type allocator struct { func newAllocator(size int) *allocator { al := &allocator{ - size: size, - fptrs: make([]float64, 0, size), - fheader: nil, + size: size, + fptrs: make([]float64, 0, size), } - al.fheader = (*reflect.SliceHeader)(unsafe.Pointer(&al.fptrs)) al.scratchValue = LNumber(0) al.scratchValueP = (*iface)(unsafe.Pointer(&al.scratchValue)) @@ -63,7 +56,6 @@ func (al *allocator) LNumber2I(v LNumber) LValue { // check if we need a new alloc page if cap(al.fptrs) == len(al.fptrs) { al.fptrs = make([]float64, 0, al.size) - al.fheader = (*reflect.SliceHeader)(unsafe.Pointer(&al.fptrs)) } // alloc a new float, and store our value into it diff --git a/go.mod b/go.mod index f501c77b..3018f9f2 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/yuin/gopher-lua -go 1.17 +go 1.20 require github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e diff --git a/utils.go b/utils.go index 2df68dc7..4b4d9ae7 100644 --- a/utils.go +++ b/utils.go @@ -4,7 +4,6 @@ import ( "bufio" "fmt" "io" - "reflect" "strconv" "strings" "time" @@ -255,11 +254,6 @@ func strCmp(s1, s2 string) int { } } -func unsafeFastStringToReadOnlyBytes(s string) (bs []byte) { - sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) - bh := (*reflect.SliceHeader)(unsafe.Pointer(&bs)) - bh.Data = sh.Data - bh.Cap = sh.Len - bh.Len = sh.Len - return +func unsafeFastStringToReadOnlyBytes(s string) []byte { + return unsafe.Slice(unsafe.StringData(s), len(s)) } From 48d3d8022a1a39c72041269ea83ae9f641ffaea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philip=20Dub=C3=A9?= Date: Wed, 13 Mar 2024 11:16:49 +0000 Subject: [PATCH 2/2] alloc: remove size, rely on cap --- alloc.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/alloc.go b/alloc.go index 51731d41..010850ed 100644 --- a/alloc.go +++ b/alloc.go @@ -22,7 +22,6 @@ func init() { // allocator is a fast bulk memory allocator for the LValue. type allocator struct { - size int fptrs []float64 scratchValue LValue @@ -31,7 +30,6 @@ type allocator struct { func newAllocator(size int) *allocator { al := &allocator{ - size: size, fptrs: make([]float64, 0, size), } al.scratchValue = LNumber(0) @@ -55,7 +53,7 @@ func (al *allocator) LNumber2I(v LNumber) LValue { // check if we need a new alloc page if cap(al.fptrs) == len(al.fptrs) { - al.fptrs = make([]float64, 0, al.size) + al.fptrs = make([]float64, 0, cap(al.fptrs)) } // alloc a new float, and store our value into it