ID:2833446
 
(See the best response by Ter13.)
This seems pretty dumb but I wanted to ask to be sure.
If I have a proc that's updating icon_state for an object based on various critera, is there any performance benefit to having a 'new_state' var and comparing that to icon_state's current value, and only updating icon_state if they're not the same?

Or more simply put, if you make icon_state = whatever it already is, does byond actually do anything with that?
Good question lol
Best response
mob
verb
TestOne()
for(var/i in 1 to 1000000)
icon_state = "herp"

TestTwo()
for(var/i in 1 to 500000)
icon_state = "derp"
icon_state = "herp"

TestThree()
for(var/i in 1 to 1000000)
if(icon_state!="herp")
icon_state = "herp"

TestFour()
for(var/i in 1 to 500000)
if(icon_state!="derp")
icon_state = "derp"
if(icon_state!="herp")
icon_state = "herp"



                     Profile results (total time)
Proc Name Self CPU Total CPU Real Time Overtime Calls
------------------- --------- --------- --------- --------- ---------
/mob/verb/TestFour 0.322 0.322 0.321 0.322 1
/mob/verb/TestTwo 0.217 0.217 0.218 0.217 1
/mob/verb/TestOne 0.154 0.154 0.154 0.154 1
/mob/verb/TestThree 0.109 0.109 0.109 0.109 1


TestThree is best case, where the icon_state never changes. Approximately 66% of the CPU usage is directly from the structure of the loop/if statement.

TestFour is worst case, where the icon_state always changes. It performs approximately 50% worse than Test2.

TestOne is the naive case.

With a small number of unique appearances, you will likely do more harm than good. In a game with a large number of appearances, there may be some benefit to testing before reassigning. My tests do not indicate that there is much, if any benefit, but testing in a game that has a lot more going on than a blank test environment would be a better indicator.
Cool thanks for running those tests, good to have the numbers.

This is mainly around some SS13 gun code, where some weapons have icon states based on their ammo remaining.
This means they currently get checked every time they fire, with an automatic weapon shooting up to 10 times a second, with 50+ dudes running around. The question was raised if icon updates could have any kind of performance impact, so in this case I'll probably add the check.

Edit: feature request I guess, but if there's a perf impact to updating an icon_state with its existing value, that seems like a useful thing to be done byondside, maybe.
I highly doubt that's gonna be a painpoint for you. It's nice to have tho.
Unless you're tgmc in which case you're owned.
In response to LemonInTheDark
LemonInTheDark wrote:
Unless you're tgmc in which case you're owned.

Well, funny you should say that...