@@ -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 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+
5784local _rawget: function (table , any ): any
5885if rawget then
5986 _rawget = rawget
@@ -212,8 +239,7 @@ local function processRecursive(process: inspect.ProcessFunction,
212239end
213240
214241local 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)
217243end
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 )
347373end
348374
349375setmetatable (inspect, {
0 commit comments