Skip to content

Bad code in the event part is wasting a lot of CPU per tick #1439

@TiberiumFusion

Description

@TiberiumFusion

Pac is full of a lot of crap that burns CPU by doing completely unnecessary or redundant work, as is the case here.

The event part's OnThink() method calls expensive spaghetti bandaid function fix_args() every single tick.

function PART:OnThink()
self.nextactivationrefresh = self.nextactivationrefresh or CurTime()
if not self.singleactivatestate and self.nextactivationrefresh < CurTime() then
self.singleactivatestate = true
end
local ent = get_owner(self)
if not ent:IsValid() then return end
local data = PART.Events[self.Event]
if not data then return end
self:fix_args()
self:TriggerEvent(should_trigger(self, ent, data))
if pace and pace.IsActive() and self.Name == "" then
if self.pace_properties and self.pace_properties["Name"] and self.pace_properties["Name"]:IsValid() then
self.pace_properties["Name"]:SetText(self:GetNiceName())
end
end
end

I do not see any reason why fix_args must do its bad inefficient work every single tick. It is already used once in SetEvent(), where it at least makes sense as a sanitizer. But, if the event arguments can somehow be modified outside of a SetEvent(), then 1) fix that horrible footgun, and 2) change fix_args() or its call inside OnThink() to not be so horrible.

For example, with a basic invalidation check like this...

function PART:fix_args()
	if (self.Arguments == self.LastFixedArguments) then return end
	-- expensive code
	self.LastFixedArguments = self.Arguments
end

...I get a ~15% reduction in outfit rendering time on my pacs that have about ~50 event parts each. No breakages. That's rare.

The event and proxy parts are notoriously costly and widespread, so unless I am missing something here, I see no reason why this cannot be fixed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions