@@ -54,6 +54,33 @@ local char = string.char
5454local gsub = string.gsub
5555local 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+
5784local _rawget: function (table , any ): any
5885if rawget then
5986 _rawget = rawget
@@ -211,11 +238,6 @@ local function processRecursive(process: inspect.ProcessFunction,
211238 return processed
212239end
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
221243local 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 )
347369end
348370
349371setmetatable (inspect, {
0 commit comments