Revamp of 'lua_pushfstring' / 'luaO_pushvfstring'

The function 'luaO_pushvfstring' now uses an internal buffer to
concatenate small strings, instead of pushing all pieces on the
stack. This avoids the creation of several small Lua strings for each
piece of the result. (For instance, a format like "n: '%d'" used to
create three intermediate strings: "n: '", the numeral, and "'".
Now it creates none.)
This commit is contained in:
Roberto Ierusalimschy
2019-04-24 14:01:20 -03:00
parent 20b161e285
commit 3da34a5fa7
4 changed files with 177 additions and 44 deletions

View File

@@ -400,5 +400,66 @@ do
assert(co() == "2")
end
if T==nil then
(Message or print)
("\n >>> testC not active: skipping 'pushfstring' tests <<<\n")
else
print"testing 'pushfstring'"
-- formats %U, %f, %I already tested elsewhere
local blen = 400 -- internal buffer length in 'luaO_pushfstring'
local function callpfs (op, fmt, n)
local x = {T.testC("pushfstring" .. op .. "; return *", fmt, n)}
-- stack has code, 'fmt', 'n', and result from operation
assert(#x == 4) -- make sure nothing else was left in the stack
return x[4]
end
local function testpfs (op, fmt, n)
assert(callpfs(op, fmt, n) == string.format(fmt, n))
end
testpfs("I", "", 0)
testpfs("I", string.rep("a", blen - 1), 0)
testpfs("I", string.rep("a", blen), 0)
testpfs("I", string.rep("a", blen + 1), 0)
local str = string.rep("ab", blen) .. "%d" .. string.rep("d", blen / 2)
testpfs("I", str, 2^14)
testpfs("I", str, -2^15)
str = "%d" .. string.rep("cd", blen)
testpfs("I", str, 2^14)
testpfs("I", str, -2^15)
str = string.rep("c", blen - 2) .. "%d"
testpfs("I", str, 2^14)
testpfs("I", str, -2^15)
for l = 12, 14 do
local str1 = string.rep("a", l)
for i = 0, 500, 13 do
for j = 0, 500, 13 do
str = string.rep("a", i) .. "%s" .. string.rep("d", j)
testpfs("S", str, str1)
testpfs("S", str, str)
end
end
end
str = "abc %c def"
testpfs("I", str, string.byte("A"))
-- non-printable character
assert(callpfs("I", str, 255) == "abc <\\255> def")
str = string.rep("a", blen - 1) .. "%p" .. string.rep("cd", blen)
testpfs("P", str, {})
end
print('OK')