diff --git a/Makefile b/Makefile index e185885..b1d61af 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ LUA := $(shell luarocks config variables.LUA) -all: gen check test +all: gen check test perf dev: luarocks install tl diff --git a/inspect.lua b/inspect.lua index e79df77..386614c 100644 --- a/inspect.lua +++ b/inspect.lua @@ -1,4 +1,4 @@ -local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local math = _tl_compat and _tl_compat.math or math; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local type = type +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local math = _tl_compat and _tl_compat.math or math; local pcall = _tl_compat and _tl_compat.pcall or pcall; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local type = type local inspect = { Options = {} } @@ -54,6 +54,33 @@ local char = string.char local gsub = string.gsub local fmt = string.format + +local sbavailable, stringbuffer = pcall(require, "string.buffer") +local buffnew +local puts +local render + +if sbavailable then + buffnew = stringbuffer.new + puts = function(buf, str) + buf:put(str) + end + render = function(buf) + return buf:get() + end +else + buffnew = function() + return { n = 0 } + end + puts = function(buf, str) + buf.n = buf.n + 1 + buf[buf.n] = str + end + render = function(buf) + return table.concat(buf) + end +end + local _rawget if rawget then _rawget = rawget @@ -211,11 +238,6 @@ local function processRecursive(process, return processed end -local function puts(buf, str) - buf.n = buf.n + 1 - buf[buf.n] = str -end - local Inspector = {} @@ -332,7 +354,7 @@ function inspect.inspect(root, options) countCycles(root, cycles, depth) local inspector = setmetatable({ - buf = { n = 0 }, + buf = buffnew(), ids = {}, cycles = cycles, depth = depth, @@ -343,7 +365,7 @@ function inspect.inspect(root, options) inspector:putValue(root) - return table.concat(inspector.buf) + return render(inspector.buf) end setmetatable(inspect, { diff --git a/inspect.tl b/inspect.tl index 340efd7..4ee3d10 100644 --- a/inspect.tl +++ b/inspect.tl @@ -54,6 +54,33 @@ local char = string.char local gsub = string.gsub local fmt = string.format +-- String buffer support (LuaJIT optimization) +local sbavailable, stringbuffer = pcall(require, "string.buffer") +local buffnew: function(): table +local puts: function(table, string) +local render: function(table): string + +if sbavailable then + buffnew = stringbuffer.new + puts = function(buf: table, str: string) + buf:put(str) + end + render = function(buf: table): string + return buf:get() + end +else + buffnew = function(): table + return { n = 0 } + end + puts = function(buf: table, str: string) + buf.n = buf.n as integer + 1 + buf[buf.n as integer] = str + end + render = function(buf: table): string + return table.concat(buf as {string}) + end +end + local _rawget: function(table, any): any if rawget then _rawget = rawget @@ -211,11 +238,6 @@ local function processRecursive(process: inspect.ProcessFunction, return processed 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 @@ -332,7 +354,7 @@ function inspect.inspect(root: any, options: inspect.Options): string countCycles(root, cycles, depth) local inspector = setmetatable({ - buf = { n = 0 }, + buf = buffnew(), ids = {}, cycles = cycles, depth = depth, @@ -343,7 +365,7 @@ function inspect.inspect(root: any, options: inspect.Options): string inspector:putValue(root) - return table.concat(inspector.buf as {string}) + return render(inspector.buf) end setmetatable(inspect, {