Skip to content

Commit 07b3dd8

Browse files
committed
perf(luaJIT): use LuaJIT stringbuffers, if present. Fixes #67
1 parent 9e13863 commit 07b3dd8

2 files changed

Lines changed: 59 additions & 15 deletions

File tree

inspect.lua

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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
1+
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
22
local inspect = { Options = {} }
33

44

@@ -54,6 +54,33 @@ local char = string.char
5454
local gsub = string.gsub
5555
local fmt = string.format
5656

57+
58+
local sbavailable, stringbuffer = pcall(require, "string.buffer")
59+
local buffnew
60+
local puts
61+
local render
62+
63+
if sbavailable then
64+
buffnew = stringbuffer.new
65+
puts = function(buf, str)
66+
buf:put(str)
67+
end
68+
render = function(buf)
69+
return buf:get()
70+
end
71+
else
72+
buffnew = function()
73+
return { n = 0 }
74+
end
75+
puts = function(buf, str)
76+
buf.n = buf.n + 1
77+
buf[buf.n] = str
78+
end
79+
render = function(buf)
80+
return table.concat(buf)
81+
end
82+
end
83+
5784
local _rawget
5885
if rawget then
5986
_rawget = rawget
@@ -211,11 +238,6 @@ local function processRecursive(process,
211238
return processed
212239
end
213240

214-
local function puts(buf, str)
215-
buf.n = buf.n + 1
216-
buf[buf.n] = str
217-
end
218-
219241

220242

221243
local Inspector = {}
@@ -332,7 +354,7 @@ function inspect.inspect(root, options)
332354
countCycles(root, cycles, depth)
333355

334356
local inspector = setmetatable({
335-
buf = { n = 0 },
357+
buf = buffnew(),
336358
ids = {},
337359
cycles = cycles,
338360
depth = depth,
@@ -343,7 +365,7 @@ function inspect.inspect(root, options)
343365

344366
inspector:putValue(root)
345367

346-
return table.concat(inspector.buf)
368+
return render(inspector.buf)
347369
end
348370

349371
setmetatable(inspect, {

inspect.tl

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,33 @@ local char = string.char
5454
local gsub = string.gsub
5555
local fmt = string.format
5656

57+
-- String buffer support (LuaJIT optimization)
58+
local sbavailable, stringbuffer = pcall(require, "string.buffer")
59+
local buffnew: function(): table
60+
local puts: function(table, string)
61+
local render: function(table): string
62+
63+
if sbavailable then
64+
buffnew = stringbuffer.new
65+
puts = function(buf: table, str: string)
66+
buf:put(str)
67+
end
68+
render = function(buf: table): string
69+
return buf:get()
70+
end
71+
else
72+
buffnew = function(): table
73+
return { n = 0 }
74+
end
75+
puts = function(buf: table, str: string)
76+
buf.n = buf.n as integer + 1
77+
buf[buf.n as integer] = str
78+
end
79+
render = function(buf: table): string
80+
return table.concat(buf as {string})
81+
end
82+
end
83+
5784
local _rawget: function(table, any): any
5885
if rawget then
5986
_rawget = rawget
@@ -211,11 +238,6 @@ local function processRecursive(process: inspect.ProcessFunction,
211238
return processed
212239
end
213240

214-
local function puts(buf: table, str:string): nil
215-
buf.n = buf.n as integer + 1
216-
buf[buf.n as integer] = str
217-
end
218-
219241
-------------------------------------------------------------------
220242

221243
local type Inspector = record
@@ -332,7 +354,7 @@ function inspect.inspect(root: any, options: inspect.Options): string
332354
countCycles(root, cycles, depth)
333355

334356
local inspector = setmetatable({
335-
buf = { n = 0 },
357+
buf = buffnew(),
336358
ids = {},
337359
cycles = cycles,
338360
depth = depth,
@@ -343,7 +365,7 @@ function inspect.inspect(root: any, options: inspect.Options): string
343365

344366
inspector:putValue(root)
345367

346-
return table.concat(inspector.buf as {string})
368+
return render(inspector.buf)
347369
end
348370

349371
setmetatable(inspect, {

0 commit comments

Comments
 (0)