diff --git a/alloc.go b/alloc.go index 7a8cd63a..010850ed 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,7 @@ func init() { // allocator is a fast bulk memory allocator for the LValue. type allocator struct { - size int - fptrs []float64 - fheader *reflect.SliceHeader + fptrs []float64 scratchValue LValue scratchValueP *iface @@ -36,11 +30,8 @@ type allocator struct { func newAllocator(size int) *allocator { al := &allocator{ - size: size, - fptrs: make([]float64, 0, size), - fheader: nil, + fptrs: make([]float64, 0, size), } - al.fheader = (*reflect.SliceHeader)(unsafe.Pointer(&al.fptrs)) al.scratchValue = LNumber(0) al.scratchValueP = (*iface)(unsafe.Pointer(&al.scratchValue)) @@ -62,8 +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.fheader = (*reflect.SliceHeader)(unsafe.Pointer(&al.fptrs)) + al.fptrs = make([]float64, 0, cap(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)) }