mitchwadair / sidescroller-framework

A framework for CraftStudio (https://sparklinlabs.itch.io/craftstudio) to make creating 2D games simpler and easier
https://www.sframework.net
MIT License
0 stars 0 forks source link

Minor SpriteRenderer optimization #9

Closed mitchwadair closed 3 years ago

mitchwadair commented 3 years ago

https://github.com/mitchwadair/sidescroller-framework/blob/f09439b8cada66cef1238bfe3b8d2aae1ecbc54c/src/SFramework/Components/SpriteRenderer.lua#L161

Could improve this by eliminating the division operation (which can be expensive, especially when lots of SpriteRenderers are present).

-- if 'self.animSpeed - 1' is equal to 'self.animCounter' then their division will equal 1
-- so no need to perform that operation
if ((self.animSpeed-1) == self.animCounter ) or self.animSpeed == 1 then

Even further, we could make it less costly by using an addition operation instead of subtraction

-- add one to both sides so we eliminate the subtraction (which costs more compute than addition)
if (self.animSpeed == self.animCounter + 1) or self.animSpeed == 1 then

and finally reordering the condition so the least costly option comes first

-- if self.animSpeed == 1, it shouldn't check the other part of the condition because it is an or
-- thus eliminating an addition operation
if self.animSpeed == 1 or (self.animSpeed == self.animCounter + 1) then
mitchwadair commented 3 years ago

could also just go one further and remove the first half of the condition since that is a relatively rare case, and only have the right side.

-- since self.animSpeed being 1 is pretty rare, might as well just tank it when it occurs and
-- eliminate the extra check
if self.animSpeed == self.animCounter + 1 then