diff options
author | Derek Bailey <dbaileychess@gmail.com> | 2019-11-14 15:46:47 -0800 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2019-11-14 15:46:46 -0800 |
commit | 0f2ff7eaa96b58adbfcf58cb08e123c943f1ff01 (patch) | |
tree | 055f7aa0afd19bb7c8ffa9057759dcac59cee3d2 | |
parent | dda095023d9f2811c22519654f20e4027d301e9c (diff) | |
download | flatbuffers-0f2ff7eaa96b58adbfcf58cb08e123c943f1ff01.tar.gz flatbuffers-0f2ff7eaa96b58adbfcf58cb08e123c943f1ff01.tar.bz2 flatbuffers-0f2ff7eaa96b58adbfcf58cb08e123c943f1ff01.zip |
Lua cleanup (#5624)
-rw-r--r-- | lua/flatbuffers/builder.lua | 42 | ||||
-rw-r--r-- | lua/flatbuffers/numTypes.lua | 15 | ||||
-rw-r--r-- | lua/flatbuffers/view.lua | 65 | ||||
-rw-r--r-- | tests/luatest.lua | 24 |
4 files changed, 82 insertions, 64 deletions
diff --git a/lua/flatbuffers/builder.lua b/lua/flatbuffers/builder.lua index 19f214de..4a0c4f6a 100644 --- a/lua/flatbuffers/builder.lua +++ b/lua/flatbuffers/builder.lua @@ -29,12 +29,12 @@ local getAlignSize = compat.GetAlignSize local function vtableEqual(a, objectStart, b) UOffsetT:EnforceNumber(objectStart) - if (#a * VOffsetT.bytewidth) ~= #b then + if (#a * 2) ~= #b then return false end for i, elem in ipairs(a) do - local x = string.unpack(VOffsetT.packFmt, b, 1 + (i - 1) * VOffsetT.bytewidth) + local x = string.unpack(VOffsetT.packFmt, b, 1 + (i - 1) * 2) if x ~= 0 or elem ~= 0 then local y = objectStart - elem if x ~= y then @@ -121,7 +121,7 @@ function mt:WriteVtable() local vt2lenstr = self.bytes:Slice(vt2Start, vt2Start+1) local vt2Len = string.unpack(VOffsetT.packFmt, vt2lenstr, 1) - local metadata = VtableMetadataFields * VOffsetT.bytewidth + local metadata = VtableMetadataFields * 2 local vt2End = vt2Start + vt2Len local vt2 = self.bytes:Slice(vt2Start+metadata,vt2End) @@ -150,7 +150,7 @@ function mt:WriteVtable() self:PrependVOffsetT(objectSize) local vBytes = #self.currentVTable + VtableMetadataFields - vBytes = vBytes * VOffsetT.bytewidth + vBytes = vBytes * 2 self:PrependVOffsetT(vBytes) local objectStart = #self.bytes - objectOffset @@ -225,17 +225,17 @@ function mt:Prep(size, additionalBytes) end function mt:PrependSOffsetTRelative(off) - self:Prep(SOffsetT.bytewidth, 0) + self:Prep(4, 0) assert(off <= self:Offset(), "Offset arithmetic error") - local off2 = self:Offset() - off + SOffsetT.bytewidth + local off2 = self:Offset() - off + 4 self:Place(off2, SOffsetT) end function mt:PrependUOffsetTRelative(off) - self:Prep(UOffsetT.bytewidth, 0) + self:Prep(4, 0) local soffset = self:Offset() if off <= soffset then - local off2 = soffset - off + UOffsetT.bytewidth + local off2 = soffset - off + 4 self:Place(off2, UOffsetT) else error("Offset arithmetic error") @@ -245,8 +245,9 @@ end function mt:StartVector(elemSize, numElements, alignment) assert(not self.nested) self.nested = true - self:Prep(Uint32.bytewidth, elemSize * numElements) - self:Prep(alignment, elemSize * numElements) + local elementSize = elemSize * numElements + self:Prep(4, elementSize) -- Uint32 length + self:Prep(alignment, elementSize) return self:Offset() end @@ -263,7 +264,7 @@ function mt:CreateString(s) assert(type(s) == "string") - self:Prep(UOffsetT.bytewidth, (#s + 1)*Uint8.bytewidth) + self:Prep(4, #s + 1) self:Place(0, Uint8) local l = #s @@ -271,20 +272,21 @@ function mt:CreateString(s) self.bytes:Set(s, self.head, self.head + l) - return self:EndVector(#s) + return self:EndVector(l) end function mt:CreateByteVector(x) assert(not self.nested) self.nested = true - self:Prep(UOffsetT.bytewidth, #x*Uint8.bytewidth) local l = #x + self:Prep(4, l) + self.head = self.head - l self.bytes:Set(x, self.head, self.head + l) - return self:EndVector(#x) + return self:EndVector(l) end function mt:Slot(slotnum) @@ -295,12 +297,7 @@ end local function finish(self, rootTable, sizePrefix) UOffsetT:EnforceNumber(rootTable) - local prepSize = UOffsetT.bytewidth - if sizePrefix then - prepSize = prepSize + Int32.bytewidth - end - - self:Prep(self.minalign, prepSize) + self:Prep(self.minalign, sizePrefix and 8 or 4) self:PrependUOffsetTRelative(rootTable) if sizePrefix then local size = #self.bytes - self.head @@ -325,8 +322,9 @@ function mt:Prepend(flags, off) end function mt:PrependSlot(flags, o, x, d) - flags:EnforceNumber(x) - flags:EnforceNumber(d) + flags:EnforceNumbers(x,d) +-- flags:EnforceNumber(x) +-- flags:EnforceNumber(d) if x ~= d then self:Prepend(flags, x) self:Slot(o) diff --git a/lua/flatbuffers/numTypes.lua b/lua/flatbuffers/numTypes.lua index 8fec21c4..01f41993 100644 --- a/lua/flatbuffers/numTypes.lua +++ b/lua/flatbuffers/numTypes.lua @@ -34,6 +34,20 @@ function type_mt:EnforceNumber(n) error("Number is not in the valid range") end +function type_mt:EnforceNumbers(a,b) + -- duplicate code since the overhead of function calls + -- for such a popular method is time consuming + if not self.min_value and not self.max_value then + return + end + + if self.min_value <= a and a <= self.max_value and self.min_value <= b and b <= self.max_value then + return + end + + error("Number is not in the valid range") +end + function type_mt:EnforceNumberAndPack(n) return bpack(self.packFmt, n) end @@ -58,6 +72,7 @@ local bool_mt = Unpack = function(self, buf, pos) return buf[pos] == "1" end, ValidNumber = function(self, n) return true end, -- anything is a valid boolean in Lua EnforceNumber = function(self, n) end, -- anything is a valid boolean in Lua + EnforceNumbers = function(self, a, b) end, -- anything is a valid boolean in Lua EnforceNumberAndPack = function(self, n) return self:Pack(value) end, } diff --git a/lua/flatbuffers/view.lua b/lua/flatbuffers/view.lua index da0f8bf7..fde15c3d 100644 --- a/lua/flatbuffers/view.lua +++ b/lua/flatbuffers/view.lua @@ -6,69 +6,83 @@ local mt_name = "flatbuffers.view.mt" local N = require("flatbuffers.numTypes") local binaryarray = require("flatbuffers.binaryarray") +local function enforceOffset(off) + if off < 0 or off > 42949672951 then + error("Offset is not valid") + end +end + +local unpack = string.unpack +local function unPackUoffset(bytes, off) + return unpack("<I4", bytes.str, off + 1) +end + +local function unPackVoffset(bytes, off) + return unpack("<I2", bytes.str, off + 1) +end + function m.New(buf, pos) - N.UOffsetT:EnforceNumber(pos) - + enforceOffset(pos) -- need to convert from a string buffer into -- a binary array local o = { bytes = type(buf) == "string" and binaryarray.New(buf) or buf, - pos = pos + pos = pos, } setmetatable(o, {__index = mt, __metatable = mt_name}) return o end function mt:Offset(vtableOffset) - local vtable = self.pos - self:Get(N.SOffsetT, self.pos) - local vtableEnd = self:Get(N.VOffsetT, vtable) - if vtableOffset < vtableEnd then - return self:Get(N.VOffsetT, vtable + vtableOffset) + local vtable = self.vtable + if not vtable then + vtable = self.pos - self:Get(N.SOffsetT, self.pos) + self.vtable = vtable + self.vtableEnd = self:Get(N.VOffsetT, vtable) + end + if vtableOffset < self.vtableEnd then + return unPackVoffset(self.bytes, vtable + vtableOffset) end return 0 end function mt:Indirect(off) - N.UOffsetT:EnforceNumber(off) - return off + N.UOffsetT:Unpack(self.bytes, off) + enforceOffset(off) + return off + unPackUoffset(self.bytes, off) end function mt:String(off) - N.UOffsetT:EnforceNumber(off) - off = off + N.UOffsetT:Unpack(self.bytes, off) - local start = off + N.UOffsetT.bytewidth - local length = N.UOffsetT:Unpack(self.bytes, off) + enforceOffset(off) + off = off + unPackUoffset(self.bytes, off) + local start = off + 4 + local length = unPackUoffset(self.bytes, off) return self.bytes:Slice(start, start+length) end function mt:VectorLen(off) - N.UOffsetT:EnforceNumber(off) + enforceOffset(off) off = off + self.pos - off = off + N.UOffsetT:Unpack(self.bytes, off) - return N.UOffsetT:Unpack(self.bytes, off) + off = off + unPackUoffset(self.bytes, off) + return unPackUoffset(self.bytes, off) end function mt:Vector(off) - N.UOffsetT:EnforceNumber(off) - + enforceOffset(off) off = off + self.pos - local x = off + self:Get(N.UOffsetT, off) - x = x + N.UOffsetT.bytewidth - return x + return off + self:Get(N.UOffsetT, off) + 4 end function mt:Union(t2, off) assert(getmetatable(t2) == mt_name) - N.UOffsetT:EnforceNumber(off) - + enforceOffset(off) off = off + self.pos t2.pos = off + self:Get(N.UOffsetT, off) t2.bytes = self.bytes end function mt:Get(flags, off) - N.UOffsetT:EnforceNumber(off) + enforceOffset(off) return flags:Unpack(self.bytes, off) end @@ -85,8 +99,7 @@ function mt:GetSlot(slot, d, validatorFlags) end function mt:GetVOffsetTSlot(slot, d) - N.VOffsetT:EnforceNumber(slot) - N.VOffsetT:EnforceNumber(d) + N.VOffsetT:EnforceNumbers(slot, d) local off = self:Offset(slot) if off == 0 then return d diff --git a/tests/luatest.lua b/tests/luatest.lua index 04f185ec..e60f837d 100644 --- a/tests/luatest.lua +++ b/tests/luatest.lua @@ -82,6 +82,7 @@ local function checkReadBuffer(buf, offset, sizePrefix) end local function generateMonster(sizePrefix, b) + if b then b:Clear() end b = b or flatbuffers.Builder(0) local str = b:CreateString("MyMonster") local test1 = b:CreateString("test1") @@ -208,26 +209,16 @@ local function testCanonicalData() checkReadBuffer(wireData) end -local function benchmarkMakeMonster(count) - local length = #(generateMonster()) - - --require("flatbuffers.profiler") - --profiler = newProfiler("call") - --profiler:start() - +local function benchmarkMakeMonster(count, reuseBuilder) + local fbb = reuseBuilder and flatbuffers.Builder(0) + local length = #(generateMonster(false, fbb)) + local s = os.clock() for i=1,count do - generateMonster() + generateMonster(false, fbb) end local e = os.clock() - - --profiler:stop() - --local outfile = io.open( "profile.txt", "w+" ) - --profiler:report( outfile, true) - --outfile:close() - - local dur = (e - s) local rate = count / (dur * 1000) local data = (length * count) / (1024 * 1024) @@ -279,6 +270,7 @@ local tests = {100}, {1000}, {10000}, + {10000, true} } }, { @@ -290,7 +282,7 @@ local tests = {10000}, -- uncomment following to run 1 million to compare. -- Took ~141 seconds on my machine - --{1000000}, + --{1000000}, } }, } |