From 445ae0e47ae5853818f97c3806ce7bd6a4bb8542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philip=20Dub=C3=A9?= Date: Fri, 15 Mar 2024 19:23:58 +0000 Subject: [PATCH] wip --- flow/pua/flatbuffers_binaryarray.go | 10 +++------- flow/pua/flatbuffers_builder.go | 23 +++++++++-------------- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/flow/pua/flatbuffers_binaryarray.go b/flow/pua/flatbuffers_binaryarray.go index 50cd5ad46c..635e8f7f55 100644 --- a/flow/pua/flatbuffers_binaryarray.go +++ b/flow/pua/flatbuffers_binaryarray.go @@ -85,17 +85,13 @@ func BinaryArraySlice(ls *lua.LState) int { return 1 } -func (ba *BinaryArray) Grow(newsize int) { - newdata := make([]byte, newsize) - copy(newdata[newsize-len(ba.data):], ba.data) - ba.data = newdata -} - func BinaryArrayGrow(ls *lua.LState) int { baud, ba := LuaBinaryArray.Check(ls, 1) newsize := int(ls.CheckNumber(2)) if newsize > len(ba.data) { - ba.Grow(newsize) + newdata := make([]byte, newsize) + copy(newdata[newsize-len(ba.data):], ba.data) + ba.data = newdata baud.Value = ba } return 0 diff --git a/flow/pua/flatbuffers_builder.go b/flow/pua/flatbuffers_builder.go index 828fbbdcfd..0c024446c9 100644 --- a/flow/pua/flatbuffers_builder.go +++ b/flow/pua/flatbuffers_builder.go @@ -1,6 +1,8 @@ package pua import ( + "slices" + "github.com/yuin/gopher-lua" ) @@ -58,18 +60,11 @@ func (b *Builder) Prep(width uint8, additional int) { space := alignsize + int(width) + additional if b.head < space { - oldBufSize := len(b.ba.data) - if oldBufSize == 0 { - b.ba.data = make([]byte, space) - b.head = space - } else { - newBufSize := oldBufSize - for newBufSize < oldBufSize+space { - newBufSize *= 2 - } - b.ba.Grow(newBufSize) - b.head += newBufSize - oldBufSize - } + oldlen := len(b.ba.data) + newdata := slices.Grow(b.ba.data, space) + newdata = newdata[:cap(newdata)] + copy(newdata[:oldlen], newdata[len(newdata)-oldlen:]) + b.head += len(newdata) - oldlen } b.Pad(alignsize) @@ -137,8 +132,8 @@ func (b *Builder) Slot(ls *lua.LState, slotnum int) { ls.RaiseError("Slot called outside nested context") return } - for slotnum < len(b.currentVT) { - b.currentVT = append(b.currentVT, 0) + if slotnum >= len(b.currentVT) { + b.currentVT = slices.Grow(b.currentVT, slotnum-len(b.currentVT)+1) } b.currentVT[slotnum] = b.Offset() }