Skip to content

Commit d63083f

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

2 files changed

Lines changed: 61 additions & 9 deletions

File tree

inspect.lua

Lines changed: 31 additions & 5 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 sbexist, stringbuffer = pcall(require, "string.buffer")
59+
local sbnew
60+
local sbput
61+
local sbto_s
62+
63+
if sbexist then
64+
sbnew = stringbuffer.new
65+
sbput = function(buf, str)
66+
buf:put(str)
67+
end
68+
sbto_s = function(buf)
69+
return buf:get()
70+
end
71+
else
72+
sbnew = function()
73+
return { n = 0 }
74+
end
75+
sbput = function(buf, str)
76+
buf.n = buf.n + 1
77+
buf[buf.n] = str
78+
end
79+
sbto_s = function(buf)
80+
return table.concat(buf)
81+
end
82+
end
83+
5784
local _rawget
5885
if rawget then
5986
_rawget = rawget
@@ -212,8 +239,7 @@ local function processRecursive(process,
212239
end
213240

214241
local function puts(buf, str)
215-
buf.n = buf.n + 1
216-
buf[buf.n] = str
242+
sbput(buf, str)
217243
end
218244

219245

@@ -332,7 +358,7 @@ function inspect.inspect(root, options)
332358
countCycles(root, cycles, depth)
333359

334360
local inspector = setmetatable({
335-
buf = { n = 0 },
361+
buf = sbnew(),
336362
ids = {},
337363
cycles = cycles,
338364
depth = depth,
@@ -343,7 +369,7 @@ function inspect.inspect(root, options)
343369

344370
inspector:putValue(root)
345371

346-
return table.concat(inspector.buf)
372+
return sbto_s(inspector.buf)
347373
end
348374

349375
setmetatable(inspect, {

inspect.tl

Lines changed: 30 additions & 4 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 sbexist, stringbuffer = pcall(require, "string.buffer")
59+
local sbnew: function(): table
60+
local sbput: function(table, string)
61+
local sbto_s: function(table): string
62+
63+
if sbexist then
64+
sbnew = stringbuffer.new
65+
sbput = function(buf: table, str: string)
66+
buf:put(str)
67+
end
68+
sbto_s = function(buf: table): string
69+
return buf:get()
70+
end
71+
else
72+
sbnew = function(): table
73+
return { n = 0 }
74+
end
75+
sbput = function(buf: table, str: string)
76+
buf.n = buf.n as integer + 1
77+
buf[buf.n as integer] = str
78+
end
79+
sbto_s = 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
@@ -212,8 +239,7 @@ local function processRecursive(process: inspect.ProcessFunction,
212239
end
213240

214241
local function puts(buf: table, str:string): nil
215-
buf.n = buf.n as integer + 1
216-
buf[buf.n as integer] = str
242+
sbput(buf, str)
217243
end
218244

219245
-------------------------------------------------------------------
@@ -332,7 +358,7 @@ function inspect.inspect(root: any, options: inspect.Options): string
332358
countCycles(root, cycles, depth)
333359

334360
local inspector = setmetatable({
335-
buf = { n = 0 },
361+
buf = sbnew(),
336362
ids = {},
337363
cycles = cycles,
338364
depth = depth,
@@ -343,7 +369,7 @@ function inspect.inspect(root: any, options: inspect.Options): string
343369

344370
inspector:putValue(root)
345371

346-
return table.concat(inspector.buf as {string})
372+
return sbto_s(inspector.buf)
347373
end
348374

349375
setmetatable(inspect, {

0 commit comments

Comments
 (0)