mirror of
https://github.com/gvx/Ser.git
synced 2024-11-14 18:34:22 +00:00
Apply ivan's optimisation
Thanks to Ivan (2DEngine.com), who wrote this and convinced me it was a good idea. This change in writing tables results in up to 20% faster serialization in common cases, and doesn't significantly affect performance in contrived cases.
This commit is contained in:
parent
48dbd6fdf3
commit
0c42f20f63
19
ser.lua
19
ser.lua
@ -57,18 +57,17 @@ local function write_table_ex(t, memo, rev_memo, srefs, name)
|
||||
if type(t) == 'function' then
|
||||
return '_[' .. name .. ']=loadstring' .. make_safe(dump(t))
|
||||
end
|
||||
local m = {'_[', name, ']={'}
|
||||
local mi = 3
|
||||
local m = {}
|
||||
local mi = 1
|
||||
for i = 1, #t do -- don't use ipairs here, we need the gaps
|
||||
local v = t[i]
|
||||
if v == t or is_cyclic(memo, v, t) then
|
||||
srefs[#srefs + 1] = {name, i, v}
|
||||
m[mi + 1] = 'nil,'
|
||||
m[mi] = 'nil'
|
||||
mi = mi + 1
|
||||
else
|
||||
m[mi + 1] = write(v, memo, rev_memo)
|
||||
m[mi + 2] = ','
|
||||
mi = mi + 2
|
||||
m[mi] = write(v, memo, rev_memo)
|
||||
mi = mi + 1
|
||||
end
|
||||
end
|
||||
for k,v in pairs(t) do
|
||||
@ -76,14 +75,12 @@ local function write_table_ex(t, memo, rev_memo, srefs, name)
|
||||
if v == t or k == t or is_cyclic(memo, v, t) or is_cyclic(memo, k, t) then
|
||||
srefs[#srefs + 1] = {name, k, v}
|
||||
else
|
||||
m[mi + 1] = write_key_value_pair(k, v, memo, rev_memo)
|
||||
m[mi + 2] = ','
|
||||
mi = mi + 2
|
||||
m[mi] = write_key_value_pair(k, v, memo, rev_memo)
|
||||
mi = mi + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
m[mi > 3 and mi or mi + 1] = '}'
|
||||
return concat(m)
|
||||
return '_[' .. name .. ']={' .. concat(m, ',') .. '}'
|
||||
end
|
||||
|
||||
return function(t)
|
||||
|
@ -47,7 +47,7 @@ _[0]={}
|
||||
_[0].self=_[0]
|
||||
return _[0]]=], 'simple cycle')
|
||||
|
||||
case_error({coroutine.create(function()end)}, './ser.lua:27: Trying to serialize unsupported type thread', 'unsupported type')
|
||||
case_error({coroutine.create(function()end)}, './ser.lua:29: Trying to serialize unsupported type thread', 'unsupported type')
|
||||
|
||||
case({"a", foo = "bar", ["3f"] = true, _1 = false, ["00"] = 9}, 'return {"a",["3f"]=true,_1=false,["00"]=9,foo="bar"}', 'various')
|
||||
|
||||
@ -58,7 +58,7 @@ case({['\127\230\255\254\128\12\0128\n\31'] = '\0'}, 'return {["\\127\\230\\255\
|
||||
local x = {}
|
||||
case({x, {x}, x}, [=[
|
||||
local _={}
|
||||
_[2]={}
|
||||
_[2]={nil}
|
||||
_[1]={}
|
||||
_[0]={_[1],_[2],_[1]}
|
||||
_[2][1]=_[1]
|
||||
|
Loading…
Reference in New Issue
Block a user