Skip to content

Commit

Permalink
Reduce E2 __index calls (#3006)
Browse files Browse the repository at this point in the history
* Reduce E2 __index calls

* Update name usage
  • Loading branch information
thecraftianman authored Mar 1, 2024
1 parent 2eed1c1 commit 3d61ee6
Showing 1 changed file with 69 additions and 58 deletions.
127 changes: 69 additions & 58 deletions lua/entities/gmod_wire_expression2/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AddCSLuaFile('cl_init.lua')
AddCSLuaFile('shared.lua')
include('shared.lua')
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

DEFINE_BASECLASS("base_wire_entity")

Expand Down Expand Up @@ -49,12 +49,14 @@ function ENT:UpdateOverlay(clear)
timebench = 0
})
else
local context = self.context

self:SetOverlayData( {
txt = self.name, -- name/error
error = self.error, -- error bool
prfbench = self.context.prfbench,
prfcount = self.context.prfcount,
timebench = self.context.timebench
prfbench = context.prfbench,
prfcount = context.prfcount,
timebench = context.timebench
})
end
end
Expand Down Expand Up @@ -95,86 +97,91 @@ function ENT:Destruct()
end

function ENT:UpdatePerf()
if not self.context then return end
local context = self.context
if not context then return end
if self.error then return end

self.context.prfbench = self.context.prfbench * 0.95 + self.context.prf * 0.05
self.context.prfcount = self.context.prfcount + self.context.prf - e2_softquota
self.context.timebench = self.context.timebench * 0.95 + self.context.time * 0.05 -- Average it over the last 20 ticks
context.prfbench = context.prfbench * 0.95 + context.prf * 0.05
context.prfcount = context.prfcount + context.prf - e2_softquota
context.timebench = context.timebench * 0.95 + context.time * 0.05 -- Average it over the last 20 ticks

if self.context.prfcount < 0 then self.context.prfcount = 0 end
if context.prfcount < 0 then context.prfcount = 0 end

self:UpdateOverlay()

self.context.prf = 0
self.context.time = 0

context.prf = 0
context.time = 0
end

function ENT:Execute()
if self.error or not self.context or self.context.resetting then return end
local selfTbl = self:GetTable()
local context = selfTbl.context
if not context or selfTbl.error or context.resetting then return end

self:PCallHook('preexecute')
self:PCallHook("preexecute")

self.context.stackdepth = self.context.stackdepth + 1
context.stackdepth = context.stackdepth + 1

if self.context.stackdepth >= 150 then
self:Error("Expression 2 (" .. self.name .. "): stack quota exceeded", "stack quota exceeded")
if context.stackdepth >= 150 then
self:Error("Expression 2 (" .. selfTbl.name .. "): stack quota exceeded", "stack quota exceeded")
end

local bench = SysTime()

local ok, msg = pcall(self.script, self.context)
local ok, msg = pcall(selfTbl.script, context)

if not ok then
local _catchable, msg, trace = E2Lib.unpackException(msg)

if msg == "exit" then
self:UpdatePerf()
elseif msg == "perf" then
local trace = self.context.trace
local trace = context.trace
self:UpdatePerf()
self:Error("Expression 2 (" .. self.name .. "): tick quota exceeded (at line " .. trace.start_line .. ", char " .. trace.start_col .. ")", "tick quota exceeded")
self:Error("Expression 2 (" .. selfTbl.name .. "): tick quota exceeded (at line " .. trace.start_line .. ", char " .. trace.start_col .. ")", "tick quota exceeded")
elseif trace then
self:Error("Expression 2 (" .. self.name .. "): Runtime error '" .. msg .. "' at line " .. trace.start_line .. ", char " .. trace.start_col, "script error")
self:Error("Expression 2 (" .. selfTbl.name .. "): Runtime error '" .. msg .. "' at line " .. trace.start_line .. ", char " .. trace.start_col, "script error")
else
local trace = self.context.trace
self:Error("Expression 2 (" .. self.name .. "): Internal error '" .. msg .. "' at line " .. trace.start_line .. ", char " .. trace.start_col, "script error")
local trace = context.trace
self:Error("Expression 2 (" .. selfTbl.name .. "): Internal error '" .. msg .. "' at line " .. trace.start_line .. ", char " .. trace.start_col, "script error")
end
end

self.context.time = self.context.time + (SysTime() - bench)
self.context.stackdepth = self.context.stackdepth - 1
context.time = context.time + (SysTime() - bench)
context.stackdepth = context.stackdepth - 1

local forceTriggerOutputs = self.first or self.duped
self.first = false -- if hooks call execute
self.duped = false -- if hooks call execute
self.context.triggerinput = nil -- if hooks call execute
local forceTriggerOutputs = selfTbl.first or selfTbl.duped
selfTbl.first = false -- if hooks call execute
selfTbl.duped = false -- if hooks call execute
context.triggerinput = nil -- if hooks call execute

self:PCallHook('postexecute')
self:PCallHook("postexecute")

self:TriggerOutputs(forceTriggerOutputs)

for k, v in pairs(self.inports[3]) do
if self.GlobalScope[k] then
if wire_expression_types[self.Inputs[k].Type][3] then
self.GlobalScope[k] = wire_expression_types[self.Inputs[k].Type][3](self.context, self.Inputs[k].Value)
local globalScope = selfTbl.GlobalScope
local inputs = selfTbl.Inputs

for k, v in pairs(selfTbl.inports[3]) do
if globalScope[k] then
if wire_expression_types[inputs[k].Type][3] then
globalScope[k] = wire_expression_types[inputs[k].Type][3](context, inputs[k].Value)
else
self.GlobalScope[k] = self.Inputs[k].Value
globalScope[k] = inputs[k].Value
end
end
end

self.GlobalScope.vclk = {}
if not self.directives.strict then
for k, var in pairs(self.globvars_mut) do
self.GlobalScope[k] = fixDefault(wire_expression_types2[var.type][2])
globalScope.vclk = {}
if not selfTbl.directives.strict then
for k, var in pairs(selfTbl.globvars_mut) do
globalScope[k] = fixDefault(wire_expression_types2[var.type][2])
end
end

if self.context.prfcount + self.context.prf - e2_softquota > e2_hardquota then
local trace = self.context.trace
self:Error("Expression 2 (" .. self.name .. "): tick quota exceeded (at line " .. trace.start_line .. ", char " .. trace.start_col .. ")", "hard quota exceeded")
if context.prfcount + context.prf - e2_softquota > e2_hardquota then
local trace = context.trace
self:Error("Expression 2 (" .. selfTbl.name .. "): tick quota exceeded (at line " .. trace.start_line .. ", char " .. trace.start_col .. ")", "hard quota exceeded")
end

if self.error then
Expand Down Expand Up @@ -251,21 +258,23 @@ function ENT:Think()
BaseClass.Think(self)
self:NextThink(CurTime() + 0.030303)

if not self.context then return true end
if self.error then return true end
local selfTbl = self:GetTable()
local context = selfTbl.context
if not context then return true end
if selfTbl.error then return true end

self:UpdatePerf()

if self.context.prfcount < 0 then self.context.prfcount = 0 end
if context.prfcount < 0 then context.prfcount = 0 end

self:UpdateOverlay()

self.context.prf = 0
self.context.time = 0
context.prf = 0
context.time = 0

if e2_timequota > 0 and self.context.timebench > e2_timequota then
self:Error("Expression 2 (" .. self.name .. "): time quota exceeded", "time quota exceeded")
self:PCallHook('destruct')
if e2_timequota > 0 and context.timebench > e2_timequota then
self:Error("Expression 2 (" .. selfTbl.name .. "): time quota exceeded", "time quota exceeded")
self:PCallHook("destruct")
end

return true
Expand Down Expand Up @@ -522,10 +531,10 @@ function ENT:Setup(buffer, includes, restore, forcecompile, filepath)

self:SetOverlayText(self.name)

local ok, msg = pcall(self.CallHook, self, 'construct')
local ok, msg = pcall(self.CallHook, self, "construct")
if not ok then
Msg("Construct hook(s) failed, executing destruct hooks...\n")
local ok2, msg2 = pcall(self.CallHook, self, 'destruct')
local ok2, msg2 = pcall(self.CallHook, self, "destruct")
if ok2 then
self:Error(msg .. "\nDestruct hooks succeeded.")
else
Expand Down Expand Up @@ -590,12 +599,15 @@ function ENT:TriggerInput(key, value)
end

function ENT:TriggerOutputs(force)
local globalScope = self.GlobalScope
local context = self.context

for key, t in pairs(self.outports[3]) do
if self.GlobalScope.vclk[key] or force then
if globalScope.vclk[key] or force then
if wire_expression_types2[t][4] then
WireLib.TriggerOutput(self, key, wire_expression_types2[t][4](self.context, self.GlobalScope[key]))
WireLib.TriggerOutput(self, key, wire_expression_types2[t][4](context, globalScope[key]))
else
WireLib.TriggerOutput(self, key, self.GlobalScope[key])
WireLib.TriggerOutput(self, key, globalScope[key])
end
end
end
Expand Down Expand Up @@ -660,7 +672,6 @@ hook.Add("EntityRemoved", "Wire_Expression2_Player_Disconnected", function(ent)
end)

hook.Add("PlayerAuthed", "Wire_Expression2_Player_Authed", function(ply, sid, uid)
local c
for _, ent in ipairs(ents.FindByClass("gmod_wire_expression2")) do
if (ent.uid == uid) then
ent.context.player = ply
Expand Down

0 comments on commit 3d61ee6

Please sign in to comment.