mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
(perf) introduce perf test harness and refactor (#54)
Made a lot of backwards-compatible changes and simplifications to the code that I had wanted to do for a lot of time, while also keeping an eye in performance. The new changes improved overall performance on my limited, dev-machine-only, luajit-only tests. Before: ``` $ luajit perf.lua nil,string,empty,sequence,record,hybrid,recursive,meta,process,complex,big 0.054246,0.140177,0.149759,0.323062,0.441312,0.607064,0.296031,0.458631,0.105668,1.717528,2.047272 ``` After: ``` $ luajit perf.lua nil,string,empty,sequence,record,hybrid,recursive,meta,process,complex,big 0.036804,0.188984,0.099349,0.247425,0.342079,0.452339,0.203801,0.288931,0.169462,1.010763,1.616462 ```
This commit is contained in:
parent
2d842fb78b
commit
f18082ac47
7
Makefile
7
Makefile
@ -1,3 +1,7 @@
|
|||||||
|
.PHONY: all dev gen check test perf
|
||||||
|
|
||||||
|
LUA := $(shell luarocks config lua_interpreter)
|
||||||
|
|
||||||
all: gen check test
|
all: gen check test
|
||||||
|
|
||||||
dev:
|
dev:
|
||||||
@ -14,5 +18,8 @@ check:
|
|||||||
test:
|
test:
|
||||||
busted
|
busted
|
||||||
|
|
||||||
|
perf:
|
||||||
|
$(shell luarocks config lua_interpreter) perf.lua
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
257
inspect.lua
257
inspect.lua
@ -48,6 +48,11 @@ inspect.KEY = setmetatable({}, { __tostring = function() return 'inspect.KEY' en
|
|||||||
inspect.METATABLE = setmetatable({}, { __tostring = function() return 'inspect.METATABLE' end })
|
inspect.METATABLE = setmetatable({}, { __tostring = function() return 'inspect.METATABLE' end })
|
||||||
|
|
||||||
local tostring = tostring
|
local tostring = tostring
|
||||||
|
local rep = string.rep
|
||||||
|
local match = string.match
|
||||||
|
local char = string.char
|
||||||
|
local gsub = string.gsub
|
||||||
|
local fmt = string.format
|
||||||
|
|
||||||
local function rawpairs(t)
|
local function rawpairs(t)
|
||||||
return next, t, nil
|
return next, t, nil
|
||||||
@ -56,10 +61,10 @@ end
|
|||||||
|
|
||||||
|
|
||||||
local function smartQuote(str)
|
local function smartQuote(str)
|
||||||
if str:match('"') and not str:match("'") then
|
if match(str, '"') and not match(str, "'") then
|
||||||
return "'" .. str .. "'"
|
return "'" .. str .. "'"
|
||||||
end
|
end
|
||||||
return '"' .. str:gsub('"', '\\"') .. '"'
|
return '"' .. gsub(str, '"', '\\"') .. '"'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -69,17 +74,17 @@ local shortControlCharEscapes = {
|
|||||||
}
|
}
|
||||||
local longControlCharEscapes = { ["\127"] = "\127" }
|
local longControlCharEscapes = { ["\127"] = "\127" }
|
||||||
for i = 0, 31 do
|
for i = 0, 31 do
|
||||||
local ch = string.char(i)
|
local ch = char(i)
|
||||||
if not shortControlCharEscapes[ch] then
|
if not shortControlCharEscapes[ch] then
|
||||||
shortControlCharEscapes[ch] = "\\" .. i
|
shortControlCharEscapes[ch] = "\\" .. i
|
||||||
longControlCharEscapes[ch] = string.format("\\%03d", i)
|
longControlCharEscapes[ch] = fmt("\\%03d", i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function escape(str)
|
local function escape(str)
|
||||||
return (str:gsub("\\", "\\\\"):
|
return (gsub(gsub(gsub(str, "\\", "\\\\"),
|
||||||
gsub("(%c)%f[0-9]", longControlCharEscapes):
|
"(%c)%f[0-9]", longControlCharEscapes),
|
||||||
gsub("%c", shortControlCharEscapes))
|
"%c", shortControlCharEscapes))
|
||||||
end
|
end
|
||||||
|
|
||||||
local function isIdentifier(str)
|
local function isIdentifier(str)
|
||||||
@ -107,59 +112,45 @@ local function sortKeys(a, b)
|
|||||||
return (a) < (b)
|
return (a) < (b)
|
||||||
end
|
end
|
||||||
|
|
||||||
local dta, dtb = defaultTypeOrders[ta], defaultTypeOrders[tb]
|
local dta = defaultTypeOrders[ta] or 100
|
||||||
|
local dtb = defaultTypeOrders[tb] or 100
|
||||||
if dta and dtb then return defaultTypeOrders[ta] < defaultTypeOrders[tb]
|
|
||||||
elseif dta then return true
|
|
||||||
elseif dtb then return false
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
return ta < tb
|
return dta == dtb and ta < tb or dta < dtb
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function getKeys(t)
|
||||||
|
|
||||||
|
local seqLen = 1
|
||||||
local function getSequenceLength(t)
|
while rawget(t, seqLen) ~= nil do
|
||||||
local len = 1
|
seqLen = seqLen + 1
|
||||||
local v = rawget(t, len)
|
|
||||||
while v ~= nil do
|
|
||||||
len = len + 1
|
|
||||||
v = rawget(t, len)
|
|
||||||
end
|
end
|
||||||
return len - 1
|
seqLen = seqLen - 1
|
||||||
end
|
|
||||||
|
|
||||||
local function getNonSequentialKeys(t)
|
local keys, keysLen = {}, 0
|
||||||
local keys, keysLength = {}, 0
|
for k in rawpairs(t) do
|
||||||
local sequenceLength = getSequenceLength(t)
|
if not isSequenceKey(k, seqLen) then
|
||||||
for k, _ in rawpairs(t) do
|
keysLen = keysLen + 1
|
||||||
if not isSequenceKey(k, sequenceLength) then
|
keys[keysLen] = k
|
||||||
keysLength = keysLength + 1
|
|
||||||
keys[keysLength] = k
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
table.sort(keys, sortKeys)
|
table.sort(keys, sortKeys)
|
||||||
return keys, keysLength, sequenceLength
|
return keys, keysLen, seqLen
|
||||||
end
|
end
|
||||||
|
|
||||||
local function countTableAppearances(t, tableAppearances)
|
local function countCycles(x, cycles)
|
||||||
tableAppearances = tableAppearances or {}
|
if type(x) == "table" then
|
||||||
|
if cycles[x] then
|
||||||
if type(t) == "table" then
|
cycles[x] = cycles[x] + 1
|
||||||
if not tableAppearances[t] then
|
|
||||||
tableAppearances[t] = 1
|
|
||||||
for k, v in rawpairs(t) do
|
|
||||||
countTableAppearances(k, tableAppearances)
|
|
||||||
countTableAppearances(v, tableAppearances)
|
|
||||||
end
|
|
||||||
countTableAppearances(getmetatable(t), tableAppearances)
|
|
||||||
else
|
else
|
||||||
tableAppearances[t] = tableAppearances[t] + 1
|
cycles[x] = 1
|
||||||
|
for k, v in rawpairs(x) do
|
||||||
|
countCycles(k, cycles)
|
||||||
|
countCycles(v, cycles)
|
||||||
|
end
|
||||||
|
countCycles(getmetatable(x), cycles)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return tableAppearances
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function makePath(path, a, b)
|
local function makePath(path, a, b)
|
||||||
@ -202,7 +193,10 @@ local function processRecursive(process,
|
|||||||
return processed
|
return processed
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function puts(buf, str)
|
||||||
|
buf.n = buf.n + 1
|
||||||
|
buf[buf.n] = str
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -219,118 +213,85 @@ local Inspector = {}
|
|||||||
|
|
||||||
local Inspector_mt = { __index = Inspector }
|
local Inspector_mt = { __index = Inspector }
|
||||||
|
|
||||||
function Inspector:puts(a, b, c, d, e)
|
local function tabify(inspector)
|
||||||
local buffer = self.buffer
|
puts(inspector.buf, inspector.newline .. rep(inspector.indent, inspector.level))
|
||||||
local len = #buffer
|
|
||||||
buffer[len + 1] = a
|
|
||||||
buffer[len + 2] = b
|
|
||||||
buffer[len + 3] = c
|
|
||||||
buffer[len + 4] = d
|
|
||||||
buffer[len + 5] = e
|
|
||||||
end
|
|
||||||
|
|
||||||
function Inspector:down(f)
|
|
||||||
self.level = self.level + 1
|
|
||||||
f()
|
|
||||||
self.level = self.level - 1
|
|
||||||
end
|
|
||||||
|
|
||||||
function Inspector:tabify()
|
|
||||||
self:puts(self.newline,
|
|
||||||
string.rep(self.indent, self.level))
|
|
||||||
end
|
|
||||||
|
|
||||||
function Inspector:alreadyVisited(v)
|
|
||||||
return self.ids[v] ~= nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Inspector:getId(v)
|
function Inspector:getId(v)
|
||||||
local id = self.ids[v]
|
local id = self.ids[v]
|
||||||
|
local ids = self.ids
|
||||||
if not id then
|
if not id then
|
||||||
local tv = type(v)
|
local tv = type(v)
|
||||||
id = (self.maxIds[tv] or 0) + 1
|
id = (ids[tv] or 0) + 1
|
||||||
self.maxIds[tv] = id
|
ids[v], ids[tv] = id, id
|
||||||
self.ids[v] = id
|
|
||||||
end
|
end
|
||||||
return tostring(id)
|
return tostring(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function Inspector:putValue(_)
|
|
||||||
end
|
|
||||||
|
|
||||||
function Inspector:putKey(k)
|
|
||||||
if isIdentifier(k) then
|
|
||||||
self:puts(k)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
self:puts("[")
|
|
||||||
self:putValue(k)
|
|
||||||
self:puts("]")
|
|
||||||
end
|
|
||||||
|
|
||||||
function Inspector:putTable(t)
|
|
||||||
if t == inspect.KEY or t == inspect.METATABLE then
|
|
||||||
self:puts(tostring(t))
|
|
||||||
elseif self:alreadyVisited(t) then
|
|
||||||
self:puts('<table ', self:getId(t), '>')
|
|
||||||
elseif self.level >= self.depth then
|
|
||||||
self:puts('{...}')
|
|
||||||
else
|
|
||||||
if self.tableAppearances[t] > 1 then self:puts('<', self:getId(t), '>') end
|
|
||||||
|
|
||||||
local nonSequentialKeys, nonSequentialKeysLength, sequenceLength = getNonSequentialKeys(t)
|
|
||||||
local mt = getmetatable(t)
|
|
||||||
|
|
||||||
self:puts('{')
|
|
||||||
self:down(function()
|
|
||||||
local count = 0
|
|
||||||
for i = 1, sequenceLength do
|
|
||||||
if count > 0 then self:puts(',') end
|
|
||||||
self:puts(' ')
|
|
||||||
self:putValue(t[i])
|
|
||||||
count = count + 1
|
|
||||||
end
|
|
||||||
|
|
||||||
for i = 1, nonSequentialKeysLength do
|
|
||||||
local k = nonSequentialKeys[i]
|
|
||||||
if count > 0 then self:puts(',') end
|
|
||||||
self:tabify()
|
|
||||||
self:putKey(k)
|
|
||||||
self:puts(' = ')
|
|
||||||
self:putValue(t[k])
|
|
||||||
count = count + 1
|
|
||||||
end
|
|
||||||
|
|
||||||
if type(mt) == 'table' then
|
|
||||||
if count > 0 then self:puts(',') end
|
|
||||||
self:tabify()
|
|
||||||
self:puts('<metatable> = ')
|
|
||||||
self:putValue(mt)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
if nonSequentialKeysLength > 0 or type(mt) == 'table' then
|
|
||||||
self:tabify()
|
|
||||||
elseif sequenceLength > 0 then
|
|
||||||
self:puts(' ')
|
|
||||||
end
|
|
||||||
|
|
||||||
self:puts('}')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function Inspector:putValue(v)
|
function Inspector:putValue(v)
|
||||||
|
local buf = self.buf
|
||||||
local tv = type(v)
|
local tv = type(v)
|
||||||
if tv == 'string' then
|
if tv == 'string' then
|
||||||
self:puts(smartQuote(escape(v)))
|
puts(buf, smartQuote(escape(v)))
|
||||||
elseif tv == 'number' or tv == 'boolean' or tv == 'nil' or
|
elseif tv == 'number' or tv == 'boolean' or tv == 'nil' or
|
||||||
tv == 'cdata' or tv == 'ctype' then
|
tv == 'cdata' or tv == 'ctype' then
|
||||||
self:puts(tostring(v))
|
puts(buf, tostring(v))
|
||||||
elseif tv == 'table' then
|
elseif tv == 'table' and not self.ids[v] then
|
||||||
self:putTable(v)
|
local t = v
|
||||||
|
|
||||||
|
if t == inspect.KEY or t == inspect.METATABLE then
|
||||||
|
puts(buf, tostring(t))
|
||||||
|
elseif self.level >= self.depth then
|
||||||
|
puts(buf, '{...}')
|
||||||
else
|
else
|
||||||
self:puts('<', tv, ' ', self:getId(v), '>')
|
if self.cycles[t] > 1 then puts(buf, fmt('<%d>', self:getId(t))) end
|
||||||
|
|
||||||
|
local keys, keysLen, seqLen = getKeys(t)
|
||||||
|
|
||||||
|
puts(buf, '{')
|
||||||
|
self.level = self.level + 1
|
||||||
|
|
||||||
|
for i = 1, seqLen + keysLen do
|
||||||
|
if i > 1 then puts(buf, ',') end
|
||||||
|
if i <= seqLen then
|
||||||
|
puts(buf, ' ')
|
||||||
|
self:putValue(t[i])
|
||||||
|
else
|
||||||
|
local k = keys[i - seqLen]
|
||||||
|
tabify(self)
|
||||||
|
if isIdentifier(k) then
|
||||||
|
puts(buf, k)
|
||||||
|
else
|
||||||
|
puts(buf, "[")
|
||||||
|
self:putValue(k)
|
||||||
|
puts(buf, "]")
|
||||||
|
end
|
||||||
|
puts(buf, ' = ')
|
||||||
|
self:putValue(t[k])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local mt = getmetatable(t)
|
||||||
|
if type(mt) == 'table' then
|
||||||
|
if seqLen + keysLen > 0 then puts(buf, ',') end
|
||||||
|
tabify(self)
|
||||||
|
puts(buf, '<metatable> = ')
|
||||||
|
self:putValue(mt)
|
||||||
|
end
|
||||||
|
|
||||||
|
self.level = self.level - 1
|
||||||
|
|
||||||
|
if keysLen > 0 or type(mt) == 'table' then
|
||||||
|
tabify(self)
|
||||||
|
elseif seqLen > 0 then
|
||||||
|
puts(buf, ' ')
|
||||||
|
end
|
||||||
|
|
||||||
|
puts(buf, '}')
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
puts(buf, fmt('<%s %d>', tv, self:getId(v)))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -349,20 +310,22 @@ function inspect.inspect(root, options)
|
|||||||
root = processRecursive(process, root, {}, {})
|
root = processRecursive(process, root, {}, {})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local cycles = {}
|
||||||
|
countCycles(root, cycles)
|
||||||
|
|
||||||
local inspector = setmetatable({
|
local inspector = setmetatable({
|
||||||
|
buf = { n = 0 },
|
||||||
|
ids = {},
|
||||||
|
cycles = cycles,
|
||||||
depth = depth,
|
depth = depth,
|
||||||
level = 0,
|
level = 0,
|
||||||
buffer = {},
|
|
||||||
ids = {},
|
|
||||||
maxIds = {},
|
|
||||||
newline = newline,
|
newline = newline,
|
||||||
indent = indent,
|
indent = indent,
|
||||||
tableAppearances = countTableAppearances(root),
|
|
||||||
}, Inspector_mt)
|
}, Inspector_mt)
|
||||||
|
|
||||||
inspector:putValue(root)
|
inspector:putValue(root)
|
||||||
|
|
||||||
return table.concat(inspector.buffer)
|
return table.concat(inspector.buf)
|
||||||
end
|
end
|
||||||
|
|
||||||
setmetatable(inspect, {
|
setmetatable(inspect, {
|
||||||
|
269
inspect.tl
269
inspect.tl
@ -48,6 +48,11 @@ inspect.KEY = setmetatable({}, {__tostring = function(): string return 'in
|
|||||||
inspect.METATABLE = setmetatable({}, {__tostring = function(): string return 'inspect.METATABLE' end})
|
inspect.METATABLE = setmetatable({}, {__tostring = function(): string return 'inspect.METATABLE' end})
|
||||||
|
|
||||||
local tostring = tostring
|
local tostring = tostring
|
||||||
|
local rep = string.rep
|
||||||
|
local match = string.match
|
||||||
|
local char = string.char
|
||||||
|
local gsub = string.gsub
|
||||||
|
local fmt = string.format
|
||||||
|
|
||||||
local function rawpairs(t: table): function, table, nil
|
local function rawpairs(t: table): function, table, nil
|
||||||
return next, t, nil
|
return next, t, nil
|
||||||
@ -56,10 +61,10 @@ end
|
|||||||
-- Apostrophizes the string if it has quotes, but not aphostrophes
|
-- Apostrophizes the string if it has quotes, but not aphostrophes
|
||||||
-- Otherwise, it returns a regular quoted string
|
-- Otherwise, it returns a regular quoted string
|
||||||
local function smartQuote(str: string): string
|
local function smartQuote(str: string): string
|
||||||
if str:match('"') and not str:match("'") then
|
if match(str, '"') and not match(str, "'") then
|
||||||
return "'" .. str .. "'"
|
return "'" .. str .. "'"
|
||||||
end
|
end
|
||||||
return '"' .. str:gsub('"', '\\"') .. '"'
|
return '"' .. gsub(str, '"', '\\"') .. '"'
|
||||||
end
|
end
|
||||||
|
|
||||||
-- \a => '\\a', \0 => '\\0', 31 => '\31'
|
-- \a => '\\a', \0 => '\\0', 31 => '\31'
|
||||||
@ -69,17 +74,17 @@ local shortControlCharEscapes: {string:string} = {
|
|||||||
}
|
}
|
||||||
local longControlCharEscapes: {string:string} = {["\127"]="\127"} -- \a => nil, \0 => \000, 31 => \031
|
local longControlCharEscapes: {string:string} = {["\127"]="\127"} -- \a => nil, \0 => \000, 31 => \031
|
||||||
for i=0, 31 do
|
for i=0, 31 do
|
||||||
local ch: string = string.char(i)
|
local ch: string = char(i)
|
||||||
if not shortControlCharEscapes[ch] then
|
if not shortControlCharEscapes[ch] then
|
||||||
shortControlCharEscapes[ch] = "\\"..i
|
shortControlCharEscapes[ch] = "\\"..i
|
||||||
longControlCharEscapes[ch] = string.format("\\%03d", i)
|
longControlCharEscapes[ch] = fmt("\\%03d", i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function escape(str: string): string
|
local function escape(str: string): string
|
||||||
return (str:gsub("\\", "\\\\")
|
return (gsub(gsub(gsub(str,"\\", "\\\\"),
|
||||||
:gsub("(%c)%f[0-9]", longControlCharEscapes)
|
"(%c)%f[0-9]", longControlCharEscapes),
|
||||||
:gsub("%c", shortControlCharEscapes))
|
"%c", shortControlCharEscapes))
|
||||||
end
|
end
|
||||||
|
|
||||||
local function isIdentifier(str: any): boolean
|
local function isIdentifier(str: any): boolean
|
||||||
@ -107,59 +112,45 @@ local function sortKeys(a:any, b:any): boolean
|
|||||||
return (a as string) < (b as string)
|
return (a as string) < (b as string)
|
||||||
end
|
end
|
||||||
|
|
||||||
local dta, dtb: integer, integer = defaultTypeOrders[ta], defaultTypeOrders[tb]
|
local dta: integer = defaultTypeOrders[ta] or 100
|
||||||
-- Two default types are compared according to the defaultTypeOrders table
|
local dtb: integer = defaultTypeOrders[tb] or 100
|
||||||
if dta and dtb then return defaultTypeOrders[ta] < defaultTypeOrders[tb]
|
-- Default types are compared according to defaultTypeOrders
|
||||||
elseif dta then return true -- default types before custom ones
|
-- Custom types are compared alphabetically
|
||||||
elseif dtb then return false -- custom types after default ones
|
return dta == dtb and ta < tb or dta < dtb
|
||||||
end
|
|
||||||
|
|
||||||
-- custom types are sorted out alphabetically
|
|
||||||
return ta < tb
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- For implementation reasons, the behavior of rawlen & # is "undefined" when
|
local function getKeys(t: table): {any}, integer, integer
|
||||||
-- tables aren't pure sequences. So we implement our own # operator.
|
-- seqLen counts the "array-like" keys
|
||||||
local function getSequenceLength(t: table): integer
|
local seqLen: integer = 1
|
||||||
local len: integer = 1
|
while rawget(t, seqLen) ~= nil do
|
||||||
local v: any = rawget(t, len)
|
seqLen = seqLen + 1
|
||||||
while v ~= nil do
|
|
||||||
len = len + 1
|
|
||||||
v = rawget(t,len)
|
|
||||||
end
|
end
|
||||||
return len - 1
|
seqLen = seqLen - 1
|
||||||
end
|
|
||||||
|
|
||||||
local function getNonSequentialKeys(t: table): {any}, integer, integer
|
local keys, keysLen: {any}, integer = {}, 0
|
||||||
local keys, keysLength: {any}, integer = {}, 0
|
for k in rawpairs(t) do
|
||||||
local sequenceLength: integer = getSequenceLength(t)
|
if not isSequenceKey(k, seqLen) then
|
||||||
for k,_ in rawpairs(t) do
|
keysLen = keysLen + 1
|
||||||
if not isSequenceKey(k, sequenceLength) then
|
keys[keysLen] = k
|
||||||
keysLength = keysLength + 1
|
|
||||||
keys[keysLength] = k
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
table.sort(keys, sortKeys)
|
table.sort(keys, sortKeys)
|
||||||
return keys, keysLength, sequenceLength
|
return keys, keysLen, seqLen
|
||||||
end
|
end
|
||||||
|
|
||||||
local function countTableAppearances(t: any, tableAppearances: {any:integer}): {any:integer}
|
local function countCycles(x: any, cycles: {any:integer}): nil
|
||||||
tableAppearances = tableAppearances or {}
|
if x is table then
|
||||||
|
if cycles[x] then
|
||||||
if t is table then
|
cycles[x] = cycles[x] + 1
|
||||||
if not tableAppearances[t] then
|
|
||||||
tableAppearances[t] = 1
|
|
||||||
for k,v in rawpairs(t) do
|
|
||||||
countTableAppearances(k, tableAppearances)
|
|
||||||
countTableAppearances(v, tableAppearances)
|
|
||||||
end
|
|
||||||
countTableAppearances(getmetatable(t), tableAppearances)
|
|
||||||
else
|
else
|
||||||
tableAppearances[t] = tableAppearances[t] + 1
|
cycles[x] = 1
|
||||||
|
for k,v in rawpairs(x) do
|
||||||
|
countCycles(k, cycles)
|
||||||
|
countCycles(v, cycles)
|
||||||
|
end
|
||||||
|
countCycles(getmetatable(x), cycles)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return tableAppearances
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function makePath(path: {any}, a: any, b: any): {any}
|
local function makePath(path: {any}, a: any, b: any): {any}
|
||||||
@ -202,135 +193,105 @@ local function processRecursive(process: inspect.ProcessFunction,
|
|||||||
return processed
|
return processed
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function puts(buf: table, str:string): nil
|
||||||
|
buf.n = buf.n as integer + 1
|
||||||
|
buf[buf.n as integer] = str
|
||||||
|
end
|
||||||
|
|
||||||
-------------------------------------------------------------------
|
-------------------------------------------------------------------
|
||||||
|
|
||||||
local type Inspector = record
|
local type Inspector = record
|
||||||
|
buf: table
|
||||||
depth: integer
|
depth: integer
|
||||||
level: integer
|
level: integer
|
||||||
buffer: {string}
|
|
||||||
ids: {any:integer}
|
ids: {any:integer}
|
||||||
maxIds: {any:integer}
|
|
||||||
newline: string
|
newline: string
|
||||||
indent: string
|
indent: string
|
||||||
tableAppearances: {table: integer}
|
cycles: {table: integer}
|
||||||
|
puts: function(string)
|
||||||
end
|
end
|
||||||
|
|
||||||
local Inspector_mt = {__index = Inspector}
|
local Inspector_mt = {__index = Inspector}
|
||||||
|
|
||||||
function Inspector:puts(a:string, b:string, c:string, d:string, e:string): nil
|
local function tabify(inspector: Inspector)
|
||||||
local buffer: {string} = self.buffer
|
puts(inspector.buf, inspector.newline .. rep(inspector.indent, inspector.level))
|
||||||
local len: integer = #buffer
|
|
||||||
buffer[len+1] = a
|
|
||||||
buffer[len+2] = b
|
|
||||||
buffer[len+3] = c
|
|
||||||
buffer[len+4] = d
|
|
||||||
buffer[len+5] = e
|
|
||||||
end
|
|
||||||
|
|
||||||
function Inspector:down(f: function()): nil
|
|
||||||
self.level = self.level + 1
|
|
||||||
f()
|
|
||||||
self.level = self.level - 1
|
|
||||||
end
|
|
||||||
|
|
||||||
function Inspector:tabify(): nil
|
|
||||||
self:puts(self.newline,
|
|
||||||
string.rep(self.indent, self.level))
|
|
||||||
end
|
|
||||||
|
|
||||||
function Inspector:alreadyVisited(v: any): boolean
|
|
||||||
return self.ids[v] ~= nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Inspector:getId(v: any): string
|
function Inspector:getId(v: any): string
|
||||||
local id: integer = self.ids[v]
|
local id: integer = self.ids[v]
|
||||||
|
local ids = self.ids
|
||||||
if not id then
|
if not id then
|
||||||
local tv: string = type(v)
|
local tv: string = type(v)
|
||||||
id = (self.maxIds[tv] or 0) + 1
|
id = (ids[tv] or 0) + 1
|
||||||
self.maxIds[tv] = id
|
ids[v], ids[tv] = id, id
|
||||||
self.ids[v] = id
|
|
||||||
end
|
end
|
||||||
return tostring(id)
|
return tostring(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- dummy function; defined later
|
|
||||||
function Inspector:putValue(_: any):nil
|
|
||||||
end
|
|
||||||
|
|
||||||
function Inspector:putKey(k: any): nil
|
|
||||||
if isIdentifier(k) then
|
|
||||||
self:puts(k as string)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
self:puts("[")
|
|
||||||
self:putValue(k)
|
|
||||||
self:puts("]")
|
|
||||||
end
|
|
||||||
|
|
||||||
function Inspector:putTable(t: table): nil
|
|
||||||
if t == inspect.KEY or t == inspect.METATABLE then
|
|
||||||
self:puts(tostring(t))
|
|
||||||
elseif self:alreadyVisited(t) then
|
|
||||||
self:puts('<table ', self:getId(t), '>')
|
|
||||||
elseif self.level >= self.depth then
|
|
||||||
self:puts('{...}')
|
|
||||||
else
|
|
||||||
if self.tableAppearances[t] > 1 then self:puts('<', self:getId(t), '>') end
|
|
||||||
|
|
||||||
local nonSequentialKeys, nonSequentialKeysLength, sequenceLength = getNonSequentialKeys(t)
|
|
||||||
local mt = getmetatable(t)
|
|
||||||
|
|
||||||
self:puts('{')
|
|
||||||
self:down(function()
|
|
||||||
local count = 0
|
|
||||||
for i=1, sequenceLength do
|
|
||||||
if count > 0 then self:puts(',') end
|
|
||||||
self:puts(' ')
|
|
||||||
self:putValue(t[i])
|
|
||||||
count = count + 1
|
|
||||||
end
|
|
||||||
|
|
||||||
for i=1, nonSequentialKeysLength do
|
|
||||||
local k = nonSequentialKeys[i]
|
|
||||||
if count > 0 then self:puts(',') end
|
|
||||||
self:tabify()
|
|
||||||
self:putKey(k)
|
|
||||||
self:puts(' = ')
|
|
||||||
self:putValue(t[k])
|
|
||||||
count = count + 1
|
|
||||||
end
|
|
||||||
|
|
||||||
if type(mt) == 'table' then
|
|
||||||
if count > 0 then self:puts(',') end
|
|
||||||
self:tabify()
|
|
||||||
self:puts('<metatable> = ')
|
|
||||||
self:putValue(mt)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
if nonSequentialKeysLength > 0 or type(mt) == 'table' then -- result is multi-lined. Justify closing }
|
|
||||||
self:tabify()
|
|
||||||
elseif sequenceLength > 0 then -- array tables have one extra space before closing }
|
|
||||||
self:puts(' ')
|
|
||||||
end
|
|
||||||
|
|
||||||
self:puts('}')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function Inspector:putValue(v: any)
|
function Inspector:putValue(v: any)
|
||||||
|
local buf = self.buf
|
||||||
local tv: string = type(v)
|
local tv: string = type(v)
|
||||||
if tv == 'string' then
|
if tv == 'string' then
|
||||||
self:puts(smartQuote(escape(v as string)))
|
puts(buf, smartQuote(escape(v as string)))
|
||||||
elseif tv == 'number' or tv == 'boolean' or tv == 'nil' or
|
elseif tv == 'number' or tv == 'boolean' or tv == 'nil' or
|
||||||
tv == 'cdata' or tv == 'ctype' then
|
tv == 'cdata' or tv == 'ctype' then
|
||||||
self:puts(tostring(v as number))
|
puts(buf, tostring(v as number))
|
||||||
elseif tv == 'table' then
|
elseif tv == 'table' and not self.ids[v] then
|
||||||
self:putTable(v as table)
|
local t = v as table
|
||||||
|
|
||||||
|
if t == inspect.KEY or t == inspect.METATABLE then
|
||||||
|
puts(buf, tostring(t))
|
||||||
|
elseif self.level >= self.depth then
|
||||||
|
puts(buf, '{...}')
|
||||||
else
|
else
|
||||||
self:puts('<', tv, ' ', self:getId(v), '>')
|
if self.cycles[t] > 1 then puts(buf, fmt('<%d>', self:getId(t))) end
|
||||||
|
|
||||||
|
local keys, keysLen, seqLen = getKeys(t)
|
||||||
|
|
||||||
|
puts(buf, '{')
|
||||||
|
self.level = self.level + 1
|
||||||
|
|
||||||
|
for i = 1, seqLen + keysLen do
|
||||||
|
if i > 1 then puts(buf, ',') end
|
||||||
|
if i <= seqLen then
|
||||||
|
puts(buf, ' ')
|
||||||
|
self:putValue(t[i])
|
||||||
|
else
|
||||||
|
local k = keys[i - seqLen]
|
||||||
|
tabify(self)
|
||||||
|
if isIdentifier(k) then
|
||||||
|
puts(buf, k as string)
|
||||||
|
else
|
||||||
|
puts(buf, "[")
|
||||||
|
self:putValue(k)
|
||||||
|
puts(buf, "]")
|
||||||
|
end
|
||||||
|
puts(buf, ' = ')
|
||||||
|
self:putValue(t[k])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local mt = getmetatable(t)
|
||||||
|
if type(mt) == 'table' then
|
||||||
|
if seqLen + keysLen > 0 then puts(buf, ',') end
|
||||||
|
tabify(self)
|
||||||
|
puts(buf, '<metatable> = ')
|
||||||
|
self:putValue(mt)
|
||||||
|
end
|
||||||
|
|
||||||
|
self.level = self.level - 1
|
||||||
|
|
||||||
|
if keysLen > 0 or type(mt) == 'table' then -- result is multi-lined. Justify closing }
|
||||||
|
tabify(self)
|
||||||
|
elseif seqLen > 0 then -- array tables have one extra space before closing }
|
||||||
|
puts(buf, ' ')
|
||||||
|
end
|
||||||
|
|
||||||
|
puts(buf, '}')
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
puts(buf, fmt('<%s %d>', tv, self:getId(v)))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -349,20 +310,22 @@ function inspect.inspect(root: any, options: inspect.Options): string
|
|||||||
root = processRecursive(process, root, {}, {})
|
root = processRecursive(process, root, {}, {})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local cycles = {}
|
||||||
|
countCycles(root, cycles)
|
||||||
|
|
||||||
local inspector = setmetatable({
|
local inspector = setmetatable({
|
||||||
|
buf = { n = 0 },
|
||||||
|
ids = {},
|
||||||
|
cycles = cycles,
|
||||||
depth = depth,
|
depth = depth,
|
||||||
level = 0,
|
level = 0,
|
||||||
buffer = {},
|
|
||||||
ids = {},
|
|
||||||
maxIds = {},
|
|
||||||
newline = newline,
|
newline = newline,
|
||||||
indent = indent,
|
indent = indent,
|
||||||
tableAppearances = countTableAppearances(root)
|
|
||||||
} as Inspector, Inspector_mt)
|
} as Inspector, Inspector_mt)
|
||||||
|
|
||||||
inspector:putValue(root)
|
inspector:putValue(root)
|
||||||
|
|
||||||
return table.concat(inspector.buffer)
|
return table.concat(inspector.buf as {string})
|
||||||
end
|
end
|
||||||
|
|
||||||
setmetatable(inspect, {
|
setmetatable(inspect, {
|
||||||
|
112
perf.lua
Normal file
112
perf.lua
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
local inspect = require 'inspect'
|
||||||
|
|
||||||
|
local skip_headers = ...
|
||||||
|
|
||||||
|
local N=100000
|
||||||
|
|
||||||
|
local results = {}
|
||||||
|
|
||||||
|
local time = function(name, n, f)
|
||||||
|
local clock = os.clock
|
||||||
|
|
||||||
|
collectgarbage()
|
||||||
|
collectgarbage()
|
||||||
|
collectgarbage()
|
||||||
|
|
||||||
|
local startTime = clock()
|
||||||
|
|
||||||
|
for i=0,n do f() end
|
||||||
|
|
||||||
|
local duration = clock() - startTime
|
||||||
|
|
||||||
|
results[#results + 1] = { name, duration }
|
||||||
|
end
|
||||||
|
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
time('nil', N, function()
|
||||||
|
inspect(nil)
|
||||||
|
end)
|
||||||
|
|
||||||
|
time('string', N, function()
|
||||||
|
inspect("hello")
|
||||||
|
end)
|
||||||
|
|
||||||
|
local e={}
|
||||||
|
time('empty', N, function()
|
||||||
|
inspect(e)
|
||||||
|
end)
|
||||||
|
|
||||||
|
local seq={1,2,3,4}
|
||||||
|
time('sequence', N, function()
|
||||||
|
inspect(seq)
|
||||||
|
end)
|
||||||
|
|
||||||
|
local record={a=1, b=2, c=3}
|
||||||
|
time('record', N, function()
|
||||||
|
inspect(record)
|
||||||
|
end)
|
||||||
|
|
||||||
|
local hybrid={1, 2, 3, a=1, b=2, c=3}
|
||||||
|
time('hybrid', N, function()
|
||||||
|
inspect(hybrid)
|
||||||
|
end)
|
||||||
|
|
||||||
|
local recursive = {}
|
||||||
|
recursive.x = recursive
|
||||||
|
time('recursive', N, function()
|
||||||
|
inspect(recursive)
|
||||||
|
end)
|
||||||
|
|
||||||
|
local with_meta=setmetatable({},
|
||||||
|
{ __tostring = function() return "s" end })
|
||||||
|
time('meta', N, function()
|
||||||
|
inspect(with_meta)
|
||||||
|
end)
|
||||||
|
|
||||||
|
local process_options = {
|
||||||
|
process = function(i,p) return "p" end
|
||||||
|
}
|
||||||
|
time('process', N, function()
|
||||||
|
inspect(seq, process_options)
|
||||||
|
end)
|
||||||
|
|
||||||
|
local complex = {
|
||||||
|
a = 1,
|
||||||
|
true,
|
||||||
|
print,
|
||||||
|
[print] = print,
|
||||||
|
[{}] = { {}, 3, b = {x = 42} }
|
||||||
|
}
|
||||||
|
complex.x = complex
|
||||||
|
setmetatable(complex, complex)
|
||||||
|
time('complex', N, function()
|
||||||
|
inspect(complex)
|
||||||
|
end)
|
||||||
|
|
||||||
|
local big = {}
|
||||||
|
for i = 1,1000 do
|
||||||
|
big[i] = i
|
||||||
|
end
|
||||||
|
for i = 1,1000 do
|
||||||
|
big["a" .. i] = 1
|
||||||
|
end
|
||||||
|
time('big', N/100, function()
|
||||||
|
inspect(big)
|
||||||
|
end)
|
||||||
|
|
||||||
|
------
|
||||||
|
|
||||||
|
if not skip_headers then
|
||||||
|
for i,r in ipairs(results) do
|
||||||
|
if i > 1 then io.write(",") end
|
||||||
|
io.write(r[1])
|
||||||
|
end
|
||||||
|
io.write("\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
for i,r in ipairs(results) do
|
||||||
|
if i > 1 then io.write(",") end
|
||||||
|
io.write(r[2])
|
||||||
|
end
|
||||||
|
io.write("\n")
|
Loading…
Reference in New Issue
Block a user