Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
Permalink
v0.1
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
103 lines (78 sloc) 2.2 KB
package.cpath = "./?.so;"..package.cpath
local luabins = require("luabins")
local table_concat = table.concat
local loadstring, assert = loadstring, assert
local pairs, type, tostring = pairs, type, tostring
local luabins_save, luabins_load = luabins.save, luabins.load
local lua = ([[return {
true, false, 42, "string",
[{
true, false, 42, "string",
[true] = true, [false] = false, [42] = 42, string = "string"
}] =
{
true, false, 42, "string",
[true] = true, [false] = false, [42] = 42, string = "string"
}
}]]):gsub("[%s\n]+", "") -- Remove spaces for compactness
local data = assert(loadstring(lua))()
local saved = assert(luabins_save(data))
-- Imagine we know exact data structure.
-- We still impose some overhead on table.concat() related
-- stuff, since it is more realistic scenario.
-- Still looks a bit silly.
local concat = function(data)
local buf = {}
local function cat(v) buf[#buf + 1] = tostring(v) return cat end
-- Find table key
local tablekey, tableval
for k, v in pairs(data) do
if type(k) == "table" then
tablekey, tableval = k, v
break
end
end
cat 'return{'
cat (data[1]) ','
cat (data[2]) ','
cat (data[3]) ','
cat '"' (data[4]) '",'
cat '[{'
cat (tablekey[1]) ','
cat (tablekey[2]) ','
cat (tablekey[3]) ','
cat '"' (tablekey[4]) '",'
cat '[' (true) ']=' (tablekey[true]) ','
cat '[' (false) ']=' (tablekey[false]) ','
cat '[' (42) ']=' (tablekey[42]) ','
cat 'string' '=' '"' (tablekey["string"]) '"'
cat '}]='
cat '{'
cat (tablekey[1]) ','
cat (tablekey[2]) ','
cat (tablekey[3]) ','
cat '"' (tablekey[4]) '",'
cat '[' (true) ']=' (tablekey[true]) ','
cat '[' (false) ']=' (tablekey[false]) ','
cat '[' (42) ']=' (tablekey[42]) ','
cat 'string' '=' '"' (tablekey["string"]) '"'
cat '}'
cat '}'
return table_concat(buf, '')
end
-- Sanity check
assert(concat(data) == lua)
local bench = {}
bench.concat = function()
assert(concat(data))
end
bench.loadstring = function()
assert(loadstring(lua))()
end
bench.luabins_save = function()
assert(luabins_save(data))
end
bench.luabins_load = function()
assert(luabins_load(saved))
end
return bench