Avoid the creation of too many strings in 'package'

Both when setting a path and searching for a file ('searchpath'),
this commit reduces the number of intermediate strings created
in Lua.
(For setting a path the change is not relevant, because this is
done only twice when loading the module. Anyway, it is a nice example
of how to use auxlib buffers to manipulate strings in the C API.)
This commit is contained in:
Roberto Ierusalimschy
2019-05-03 10:14:25 -03:00
parent b36e26f51b
commit b14609032c
4 changed files with 79 additions and 41 deletions

View File

@@ -142,12 +142,18 @@ do
prepfile("print(package.path, package.cpath)")
RUN('env LUA_INIT="error(10)" LUA_PATH=xxx LUA_CPATH=xxx lua -E %s > %s',
prog, out)
local output = getoutput()
defaultpath = string.match(output, "^(.-)\t")
defaultCpath = string.match(output, "\t(.-)$")
-- running with an empty environment
RUN('env -i lua %s > %s', prog, out)
local out = getoutput()
defaultpath = string.match(out, "^(.-)\t")
defaultCpath = string.match(out, "\t(.-)$")
assert(defaultpath == string.match(output, "^(.-)\t"))
assert(defaultCpath == string.match(output, "\t(.-)$"))
end
-- paths did not changed
-- paths did not change
assert(not string.find(defaultpath, "xxx") and
string.find(defaultpath, "lua") and
not string.find(defaultCpath, "xxx") and
@@ -160,15 +166,20 @@ local function convert (p)
RUN('env LUA_PATH="%s" lua %s > %s', p, prog, out)
local expected = getoutput()
expected = string.sub(expected, 1, -2) -- cut final end of line
assert(string.gsub(p, ";;", ";"..defaultpath..";") == expected)
if string.find(p, ";;") then
p = string.gsub(p, ";;", ";"..defaultpath..";")
p = string.gsub(p, "^;", "") -- remove ';' at the beginning
p = string.gsub(p, ";$", "") -- remove ';' at the end
end
assert(p == expected)
end
convert(";")
convert(";;")
convert(";;;")
convert(";;;;")
convert(";;;;;")
convert(";;a;;;bc")
convert("a;;b")
convert(";;b")
convert("a;;")
convert("a;b;;c")
-- test -l over multiple libraries