Hello, I was trying to optimize my enemy system for my TD game and while i was commenting out lines of code in my system i discovered that just by commenting out 2 lines of code that had SetAttribute in them made my FPS increase from 50 for 2000 enemies to 73!
So i decided to benchmark SetAttribute vs Setting a value in a dictionary and i discovered that SetAttribute is quite bad to use repeatedly and these are the results i got for changing 10000 values:
local AmazingDictionary = {
Health = 0,
Speed = 0,
}
local AttributeHolder = Instance.new("Part",workspace)
local AttributeTime = os.clock()
for i = 1,10000 do
AttributeHolder:SetAttribute("Health",i)
AttributeHolder:SetAttribute("Speed",i)
end
print(os.clock() - AttributeTime)
local DictionaryTime = os.clock()
for i = 1,10000 do
AmazingDictionary.Health = i
AmazingDictionary.Speed = i
end
print(os.clock() - DictionaryTime)
I decided to make this post just as a heads up for anyone wanting to optimize their game.
(also i am not sure if i put this post under the correct topic, so if i did put it under the wrong topic please tell me!)
Thank you, this should really be talked about more. I just applied this to my own entity system and I went from 40 FPS on 1.2K enemies to 60 FPS! Bravo Omrez!
may you include numbervalue into benchmark? im curious to see the result
although i am surprised attributes are far more unoptimized to overwrite than a dictionary
Well if you are using it every once and a while of course it won’t effect much performance wise, but i mainly made this post just to give a heads up for people who use attributes in while or run service loops
I think something similar to BulkMoveTo which CFrames a table of parts and doesn’t fire any events (like changed) but for attributes (BulkSetAttribute maybe?) would improve this. Of course its a pretty rare situation where you’re setting this many attributes.
I don’t think setting attributes would ever be more performant than just setting the value in a dictionary, just because of the events fired and overhead.
Who would call set attributes 20,000 times in a single loop lmao, hmm yes let me just summon 20,000 event signals thats DEFINITELY good for performance :DDDDDDDD.
my alternative to this is create a dictionary using GetAttributes() and then updating the dictionary when the attribute is changed, might be convoluted but i idk
misread the setAttribute
I believe the intention of this benchmark is to serve as an accurate simulation of the performance overhead of systems written by those who methodically neglect favorable design choices.
Performance is proportional to the design quality of a system, not the luau VM.
Well, when i was first making my system i wasn’t aware of these things. I only learned about this when you made this comment. At the end of the day we all learn from mistakes, That is what programming is all about isn’t it? Sure, to you this seems stupid but with my experience it wasn’t. At the end of the day we all act and learn.