Skip to content

Commit

Permalink
Speed up compiler slightly, Lua tests (#2923)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vurv78 authored Dec 7, 2023
1 parent 8fa7912 commit 4edf127
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 239 deletions.
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 Token, TokenVariant = E2Lib.Tokenizer.Token, E2Lib.Tokenizer.Variant
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 @@ -2049,26 +2051,22 @@ function Compiler:GetFunction(name, types, method)
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

0 comments on commit 4edf127

Please sign in to comment.