Files
flatbuffers-bigfoot/lua/flatbuffers/view.lua
罗泽轩 d3cd78a87c [Lua] Add LuaJIT support (#6584)
* [Lua] Add LuaJIT support

Here is the output of LuaTest.sh:

Run with LuaJIT:
built 100 512-byte flatbuffers in 0.16sec: 0.63/msec, 0.31MB/sec
built 1000 512-byte flatbuffers in 0.08sec: 12.06/msec, 5.89MB/sec
built 10000 512-byte flatbuffers in 0.80sec: 12.44/msec, 6.07MB/sec
built 10000 512-byte flatbuffers in 0.33sec: 30.58/msec, 14.93MB/sec
traversed 100 592-byte flatbuffers in 0.04sec: 2.51/msec, 1.42MB/sec
traversed 1000 592-byte flatbuffers in 0.03sec: 31.52/msec, 17.79MB/sec
traversed 10000 592-byte flatbuffers in 0.21sec: 48.77/msec, 27.53MB/sec
Run with Lua 5.3:
built 100 512-byte flatbuffers in 0.02sec: 5.44/msec, 2.66MB/sec
built 1000 512-byte flatbuffers in 0.17sec: 5.74/msec, 2.80MB/sec
built 10000 512-byte flatbuffers in 1.75sec: 5.72/msec, 2.79MB/sec
built 10000 512-byte flatbuffers in 1.38sec: 7.26/msec, 3.55MB/sec
traversed 100 592-byte flatbuffers in 0.00sec: 27.64/msec, 15.60MB/sec
traversed 1000 592-byte flatbuffers in 0.03sec: 30.46/msec, 17.20MB/sec
traversed 10000 592-byte flatbuffers in 0.34sec: 29.62/msec, 16.72MB/sec

* [Lua] Better usage description

* update according to the review
2021-04-30 10:38:16 -07:00

113 lines
2.6 KiB
Lua

local compat = require("flatbuffers.compat")
local string_unpack = compat.string_unpack
local m = {}
local mt = {}
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 function unPackUoffset(bytes, off)
return string_unpack("<I4", bytes.str, off + 1)
end
local function unPackVoffset(bytes, off)
return string_unpack("<I2", bytes.str, off + 1)
end
function m.New(buf, 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,
}
setmetatable(o, {__index = mt, __metatable = mt_name})
return o
end
function mt:Offset(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)
enforceOffset(off)
return off + unPackUoffset(self.bytes, off)
end
function mt:String(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)
enforceOffset(off)
off = off + self.pos
off = off + unPackUoffset(self.bytes, off)
return unPackUoffset(self.bytes, off)
end
function mt:Vector(off)
enforceOffset(off)
off = off + self.pos
return off + self:Get(N.UOffsetT, off) + 4
end
function mt:Union(t2, off)
assert(getmetatable(t2) == mt_name)
enforceOffset(off)
off = off + self.pos
t2.pos = off + self:Get(N.UOffsetT, off)
t2.bytes = self.bytes
end
function mt:Get(flags, off)
enforceOffset(off)
return flags:Unpack(self.bytes, off)
end
function mt:GetSlot(slot, d, validatorFlags)
N.VOffsetT:EnforceNumber(slot)
if validatorFlags then
validatorFlags:EnforceNumber(d)
end
local off = self:Offset(slot)
if off == 0 then
return d
end
return self:Get(validatorFlags, self.pos + off)
end
function mt:GetVOffsetTSlot(slot, d)
N.VOffsetT:EnforceNumbers(slot, d)
local off = self:Offset(slot)
if off == 0 then
return d
end
return off
end
return m