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

Core/BossPrototype: Add Throttle function #573

Closed
wants to merge 4 commits into from

Conversation

Oppzippy
Copy link
Member

@Oppzippy Oppzippy commented Jan 10, 2019

The following pattern occurs very frequently for throttling messages from spells that are all cast in rapid succession.

do
	local prev = 0
	function mod:AnAbility(args)
		local t = args.time
		if t-prev > 2 then
			prev = t
			-- do stuff
		end
	end
end

This could be rewritten as:

function mod:AnAbility(args)
	if self:Throttle(args.spellId, 2, args.time) then
	-- or this: if not self:Throttle(args.spellId, 2, args.time) then return end
		-- do stuff
	end
end

@elvador
Copy link
Member

elvador commented Jan 10, 2019

While this might arguably be a bit prettier, it performs worse. It requires an unneeded table lookup and the scope of the variable gets bigger.

So for me it's a nope - performance is the highest priority in a throttle function.

@Oppzippy
Copy link
Member Author

Oppzippy commented Jan 10, 2019

Fair enough. Didn't think performance would really matter for something run so infrequently but you are right. A quick test shows this to take 300% as long to run as the current solution.

Edit:
At the same time though, it is common to use args.spellId when there is only one possible value for it anyway. It's an extra table lookup every time that's done instead of hardcoding the spell ID instead. There is value to readability and ease of writing code.
I'm not saying it's okay because it's done elsewhere, just that it is done for a reason that can apply to this as well.

…alling a function, and add optional time argument
@funkydude funkydude closed this Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants