From 29ee555b42407697ad7261e14d1c268ec118a22e Mon Sep 17 00:00:00 2001 From: icy4471 <153393921+icy4471@users.noreply.github.com> Date: Sun, 15 Dec 2024 12:43:57 +0000 Subject: [PATCH] tweak(scheduler.lua): speed up lua event serialization If there are no event handlers for the event, there is no need to unpack or set the source as they will never be used. --- .../citizen/scripting/lua/scheduler.lua | 85 ++++++++++--------- 1 file changed, 44 insertions(+), 41 deletions(-) diff --git a/data/shared/citizen/scripting/lua/scheduler.lua b/data/shared/citizen/scripting/lua/scheduler.lua index 00e5c36bee..f236ac557e 100644 --- a/data/shared/citizen/scripting/lua/scheduler.lua +++ b/data/shared/citizen/scripting/lua/scheduler.lua @@ -146,59 +146,62 @@ local eventHandlers = {} local deserializingNetEvent = false Citizen.SetEventRoutine(function(eventName, eventPayload, eventSource) - -- set the event source - local lastSource = _G.source - _G.source = eventSource - -- try finding an event handler for the event local eventHandlerEntry = eventHandlers[eventName] + -- as the event has no handlers, return early. + if not (eventHandlerEntry and eventHandlerEntry.handlers) then + return + end + + -- set the event source + local lastSource = _G.source + _G.source = eventSource + -- deserialize the event structure (so that we end up adding references to delete later on) local data = msgpack_unpack(eventPayload) + + -- if this is a net event and we don't allow this event to be triggered from the network, return + if eventSource:sub(1, 3) == 'net' then + if not eventHandlerEntry.safeForNet then + Citizen.Trace('event ' .. eventName .. " was not safe for net\n") - if eventHandlerEntry and eventHandlerEntry.handlers then - -- if this is a net event and we don't allow this event to be triggered from the network, return - if eventSource:sub(1, 3) == 'net' then - if not eventHandlerEntry.safeForNet then - Citizen.Trace('event ' .. eventName .. " was not safe for net\n") - - _G.source = lastSource - return - end - - deserializingNetEvent = { source = eventSource } - _G.source = tonumber(eventSource:sub(5)) - elseif isDuplicityVersion and eventSource:sub(1, 12) == 'internal-net' then - deserializingNetEvent = { source = eventSource:sub(10) } - _G.source = tonumber(eventSource:sub(14)) + _G.source = lastSource + return end - -- return an empty table if the data is nil - if not data then - data = {} - end + deserializingNetEvent = { source = eventSource } + _G.source = tonumber(eventSource:sub(5)) + elseif isDuplicityVersion and eventSource:sub(1, 12) == 'internal-net' then + deserializingNetEvent = { source = eventSource:sub(10) } + _G.source = tonumber(eventSource:sub(14)) + end + + -- return an empty table if the data is nil + if not data then + data = {} + end - -- reset serialization - deserializingNetEvent = nil + -- reset serialization + deserializingNetEvent = nil - -- if this is a table... - if type(data) == 'table' then - -- loop through all the event handlers - for k, handler in pairs(eventHandlerEntry.handlers) do - local handlerFn = handler - local handlerMT = getmetatable(handlerFn) + -- if this is a table... + if type(data) == 'table' then + -- loop through all the event handlers + for k, handler in pairs(eventHandlerEntry.handlers) do + local handlerFn = handler + local handlerMT = getmetatable(handlerFn) - if handlerMT and handlerMT.__call then - handlerFn = handlerMT.__call - end + if handlerMT and handlerMT.__call then + handlerFn = handlerMT.__call + end - if type(handlerFn) == 'function' then - local di = debug_getinfo(handlerFn) - - Citizen.CreateThreadNow(function() - handler(table_unpack(data)) - end, ('event %s [%s[%d..%d]]'):format(eventName, di.short_src, di.linedefined, di.lastlinedefined)) - end + if type(handlerFn) == 'function' then + local di = debug_getinfo(handlerFn) + + Citizen.CreateThreadNow(function() + handler(table_unpack(data)) + end, ('event %s [%s[%d..%d]]'):format(eventName, di.short_src, di.linedefined, di.lastlinedefined)) end end end