From d4f09abb9ffb43c5ea91b9bfed12307aa61fbfe9 Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Wed, 1 May 2024 21:16:28 +0300 Subject: [PATCH 1/2] Luau global import library compile optimisation --- .../Server/Dependencies/Loadstring/LuaX.lua | 69 ++++++++++++------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/MainModule/Server/Dependencies/Loadstring/LuaX.lua b/MainModule/Server/Dependencies/Loadstring/LuaX.lua index f7fc63cbf6..24532cb137 100644 --- a/MainModule/Server/Dependencies/Loadstring/LuaX.lua +++ b/MainModule/Server/Dependencies/Loadstring/LuaX.lua @@ -199,6 +199,21 @@ PathWaypoint PhysicalProperties Random Ray RaycastParams Rect Region3 Region3int16 RotationCurveKey SharedTable TweenInfo UDim UDim2 Vector2 Vector2int16 Vector3 Vector3int16 ]] +luaX.expdepth = { + "(" = ")", + "{" = "}", + "[" = "]", + "TK_FUNCTION" = "TK_END", + "TK_DO" = "TK_END", + "TK_THEN" = "TK_END", + "TK_REPEAT" = "TK_UNTIL", +} +luaX.expcutoffstat = { + "TK_REPEAT" = true, "TK_IF" = true, ";" = true, "TK_WHILE" = true, + "TK_LOCAL" = true, "TK_BREAK" = true, "TK_CONTINUE" = true, "TK_ELSE" = true, + "TK_ELSEIF" = true, "TK_END" = true, "TK_DO" = true, "TK_IF" = true, + "TK_FOR" = true, "TK_REPEAT" = true, +} ------------------------------------------------------------------------ -- initialize lexer @@ -352,6 +367,7 @@ function luaX:setinput(L, ls, z, source) ls.source = source ls.safeenv = true ls.usedglobals = {} + ls.setglobals = {} self:nextc(ls) -- read first char end @@ -457,7 +473,7 @@ function luaX:str2d(s) -- conversion failed if string.lower(string.sub(s, 1, 2)) == "0x" then -- maybe an hexadecimal constant? - result = tonumber(s, 16) + result = tonumber(string.sub(s, 3), 16) if result then return result end -- most common case -- Was: invalid trailing characters? -- In C, this function then skips over trailing spaces. @@ -465,18 +481,7 @@ function luaX:str2d(s) -- If there is still something else, then it returns a false. -- All this is not necessary using Lua's tonumber. elseif string.lower(string.sub(s, 1, 2)) == "0b" then -- binary intiger constants - if string.match(string.sub(s, 3), "[^01]") then - return nil - end - - local bin = string.reverse(string.sub(s, 3)) - local sum = 0 - - for i = 1, string.len(bin) do - sum = sum + bit32.lshift(string.sub(bin, i, i) == "1" and 1 or 0, i - 1) - end - - return sum + return tonumber(string.sub(s, 3), 2) end return nil end @@ -716,24 +721,46 @@ function luaX:poptk(ls) local tkdata = ls.lexercache local data = tkdata[tkdata.n] tkdata.n, tkdata[tkdata.n] = tkdata.n - 1, nil - ls.t.token, ls.t.seminfo, ls.linenumber = data.type or "TK_EOS", data.seminfo or "", data.line + ls.t.token, ls.t.seminfo, ls.linenumber = data.type or "TK_EOS", data.seminfo or "", data.line or 0 return data.type end -- Generate lex tree stack - local tkdata = {} + local tkdata, depthstack, globalstack = {n = 0}, {n = 1, close = "", "EOS"}, {n = 1, {}} ls.lexercache = tkdata while true do local type = luaX:llex(ls, ls.t) + local seminfo = ls.t.seminfo + local lasttoken = tkdata[tkdata.n] table.insert(tkdata, { type = type, - seminfo = ls.t.seminfo, + seminfo = seminfo, line = ls.linenumber, }) - if type == "TK_EOS" then + if luaX.expcutoffstat[type] then + table.clear(globalstack[globalstack.n]) + end + + if luaX.expdepth[type] then + depthstack[depthstack.n + 1], depthstack.n, depthstack.close = type, depthstack.n + 1, luaX.expdepth[type] + else type == depthstack.close then + local oldindex, newindex = depthstack.n, depthstack.n - 1 + depthstack[oldindex], depthstack.n, depthstack.close = nil, newindex, luaX.expdepth[depthstack[newindex]] or "" + globalstack[oldindex], globalstack.n = nil, newindex + end + + if type == "TK_NAME" then + -- Global optimisation helper + if self.DEOP[seminfo] then + ls.safeenv = false + elseif self.globalvars[seminfo] then + ls.usedglobals[seminfo] = true + globalstack[globalstack.n][seminfo] = true + end + elseif type == "TK_EOS" then break end end @@ -914,14 +941,6 @@ function luaX:llex(ls, Token) local ts = ls.buff local tok = self.enums[ts] if tok then return tok end -- reserved word? - - -- Global optimisation helper - if self.DEOP[ts] then - ls.safeenv = false - elseif self.globalvars[ts] then - table.insert(ls.usedglobals, ts) - end - Token.seminfo = ts return "TK_NAME" else From ab4ffee6a2eb366c154cce2b1f90b9a7a06628bf Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Thu, 2 May 2024 19:21:05 +0300 Subject: [PATCH 2/2] Update LuaX.lua --- .../Server/Dependencies/Loadstring/LuaX.lua | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/MainModule/Server/Dependencies/Loadstring/LuaX.lua b/MainModule/Server/Dependencies/Loadstring/LuaX.lua index 1bbd2ed821..129afc85d7 100644 --- a/MainModule/Server/Dependencies/Loadstring/LuaX.lua +++ b/MainModule/Server/Dependencies/Loadstring/LuaX.lua @@ -726,14 +726,14 @@ function luaX:poptk(ls) end -- Generate lex tree stack - local tkdata, depthstack, globalstack = {n = 0}, {n = 1, close = "", "EOS"}, {n = 1, {}} - ls.lexercache = tkdata + local tkdata, depthstack, globalstack, usedglobals = {n = 0}, {n = 1, close = "", "EOS"}, {n = 1, {}}, ls.usedglobals + ls.lexercache, ls.usedglobals.string = tkdata, true while true do local type = luaX:llex(ls, ls.t) local seminfo = ls.t.seminfo local lasttoken = tkdata[tkdata.n] - table.insert(tkdata, { + tkdata.n, tkdata[tkdata.n + 1] = tkdata.n + 1, { type = type, seminfo = seminfo, line = ls.linenumber, @@ -745,20 +745,30 @@ function luaX:poptk(ls) if luaX.expdepth[type] then depthstack[depthstack.n + 1], depthstack.n, depthstack.close = type, depthstack.n + 1, luaX.expdepth[type] - else type == depthstack.close then + elseif type == depthstack.close then local oldindex, newindex = depthstack.n, depthstack.n - 1 depthstack[oldindex], depthstack.n, depthstack.close = nil, newindex, luaX.expdepth[depthstack[newindex]] or "" globalstack[oldindex], globalstack.n = nil, newindex end if type == "TK_NAME" then + if lasttoken.type == "TK_NAME" then + table.clear(globalstack[globalstack.n]) + end + -- Global optimisation helper if self.DEOP[seminfo] then ls.safeenv = false elseif self.globalvars[seminfo] then - ls.usedglobals[seminfo] = true + usedglobals[seminfo] = true globalstack[globalstack.n][seminfo] = true end + elseif type == "=" then + local foundglobals = globalstack[globalstack.n] + for k, _ in pairs(foundglobals) do + usedglobals[k] = foundglobals[k] + end + table.clear(foundglobals) elseif type == "TK_EOS" then break end @@ -768,7 +778,7 @@ function luaX:poptk(ls) local len = #tkdata tkdata.n = len ls.linenumber, ls.lastline, ls.lookahead.token = 1, 1, "TK_EOS" - for i = 1, math.floor(len/2) do + for i = 1, math.floor(len / 2) do local j = len - i + 1 tkdata[i], tkdata[j] = tkdata[j], tkdata[i] end @@ -777,11 +787,11 @@ function luaX:poptk(ls) -- TODO: Switch to parser logic LOCALEXP(name) = GLOBALEXP(name), ... local usedglobals = ls.usedglobals if ls.safeenv and #usedglobals > 0 then - for i = #usedglobals, 1, -1 do + for _, v in pairs(usedglobals) do tkdata.n = tkdata.n + 2 tkdata[tkdata.n - 1], tkdata[tkdata.n] = { type = "TK_NAME", - seminfo = usedglobals[i], + seminfo = v, line = 1 }, { type = ",", @@ -794,11 +804,11 @@ function luaX:poptk(ls) seminfo = "=", line = 1 } - for i = #usedglobals, 1, -1 do + for _, v in pairs(usedglobals) do tkdata.n = tkdata.n + 2 tkdata[tkdata.n - 1], tkdata[tkdata.n] = { type = "TK_NAME", - seminfo = usedglobals[i], + seminfo = v, line = 1 }, { type = ",",