Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small compiler speedups, Add lua tests #2923

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions data/expression2/tests/compiler/bench.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
local t = {}
for i = 1, 600 do
t[i] = i
end

local script = "array(" .. table.concat(t, ",") .. ")"

local o1, o2, o3 = debug.gethook()

debug.sethook(error, "", 1e6)

local _, code_ok, compiled = pcall(E2Lib.compileScript, script)

debug.sethook(o1, o2, o3)

assert(code_ok, "Took too long to compile!")

debug.sethook(error, "", 1e6)

local ok = pcall(compiled)

debug.sethook(o1, o2, o3)

assert(ok, "Took too long to run!")
28 changes: 13 additions & 15 deletions lua/entities/gmod_wire_expression2/base/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
local Node, NodeVariant = E2Lib.Parser.Node, E2Lib.Parser.Variant
local Operator = E2Lib.Operator

local pairs, ipairs = pairs, ipairs

local TickQuota = GetConVar("wire_expression2_quotatick"):GetInt()

cvars.RemoveChangeCallback("wire_expression2_quotatick", "compiler_quota_check")
Expand Down Expand Up @@ -390,7 +392,7 @@

---@param data { [1]: Token<string>, [2]: Node, [3]: Node, [4]: Node?, [5]: Node } var start stop step block
[NodeVariant.For] = function (self, trace, data)
local var, start, stop, step = data[1], self:CompileExpr(data[2]), self:CompileExpr(data[3]), data[4] and self:CompileExpr(data[4]) or data[4]

Check warning on line 395 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Unused variable"

Unused variable: step

local block = self:Scope(function(scope)
scope.data.loop = true
Expand Down Expand Up @@ -569,9 +571,9 @@

if state.__break__ then
state.__break__ = false
goto exit

Check warning on line 574 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Goto"

Don't use labels and gotos unless you're jumping out of multiple loops.
elseif state.__return__ then -- Yes this should only be checked if the switch is inside a function, but I don't care enough about the performance of switch case to add another duplicated 30 lines to the file
goto exit

Check warning on line 576 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Goto"

Don't use labels and gotos unless you're jumping out of multiple loops.
else -- Fallthrough, run every case until break found.
for j = i + 1, ncases do
cases[j][2](state)
Expand Down Expand Up @@ -1025,9 +1027,9 @@
state.GlobalScope[var], state.GlobalScope.vclk[var] = val, true

if state.GlobalScope.lookup[val] then
state.GlobalScope.lookup[val][var] = true

Check warning on line 1030 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Scope depth"

Are you Egyptian? What's with these fucking scope pyramids!?
else
state.GlobalScope.lookup[val] = { [var] = true }

Check warning on line 1032 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Scope depth"

Are you Egyptian? What's with these fucking scope pyramids!?
end
end
else
Expand Down Expand Up @@ -1819,7 +1821,7 @@
if fn.attributes.legacy then
local largs = { [1] = {}, [nargs + 2] = arg_types }
for i = 1, nargs do
largs[i + 1] = { [1] = function() return rargs[i] end }

Check warning on line 1824 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Scope depth"

Are you Egyptian? What's with these fucking scope pyramids!?
end
return fn[3](state, largs, arg_types)
elseif varsig == "array(...)" then -- Need this since can't enforce compile time argument type restrictions on string calls. Woop. Array creation should not be a function..
Expand Down Expand Up @@ -2047,26 +2049,22 @@
end
end

---@param node Node
---@return RuntimeOperator
---@return string expr_type
function Compiler:CompileExpr(node)
assert(node.trace, "Incomplete node: " .. tostring(node))
local op, ty = assert(CompileVisitors[node.variant], "Unimplemented Compile Step: " .. node:instr())(self, node.trace, node.data, false)
function Compiler:CompileExpr(node --[[@param node Node]]) ---@return RuntimeOperator, string
local op, ty = CompileVisitors[node.variant](self, node.trace, node.data, false) ---@cast op -nil # Expressions should never return nil function

if node.variant == NodeVariant.ExprDynCall then
self:Assert(ty, "Cannot use void in expression position ( Did you mean Call()[type] ? )", node.trace)
else
self:Assert(ty, "Cannot use void in expression position", node.trace)
end
if ty == nil then
if node.variant == NodeVariant.ExprDynCall then
self:Error("Cannot use void in expression position ( Did you mean Call()[type] ? )", node.trace)
else
self:Error("Cannot use void in expression position", node.trace)
end
end ---@cast ty -nil # LuaLS can't figure this out yet.

return op, ty
end

---@return RuntimeOperator
function Compiler:CompileStmt(node)
assert(node.trace, "Incomplete node: " .. tostring(node))
return assert(CompileVisitors[node.variant], "Unimplemented Compile Step: " .. node:instr())(self, node.trace, node.data, true)
function Compiler:CompileStmt(node --[[@param node Node]])
return CompileVisitors[node.variant](self, node.trace, node.data, true)
end

---@param ast Node
Expand Down
Loading
Loading