Made a pretty awesome change to the module.
Although still perhaps overkill, it no longer wastes CPU.
The issue:
The reason it was wasting CPU was very simple.
Every frame we iterate over every part and check if it needs to be updated. There are no parts that were active (aka everything was 0 FPS and being refreshed manually when burning the image), so we always iterated through them all and did nothing but update some numbers in them.
This was my initial change in an attempt to solve the issue. I've removed this, for reasons discussed in the end of this dropdown.
I made a simple(ish) change.
function ViewportHandler:_ValidateRefresher()
Anytime you make a change to the handler or an object within the handler, this gets called.
This function goes through all the objects. If there’s an active object, we make sure we have our binding to renderstep. If there are no active objects (such as our flashbang case), we remove that binding.
This way, when no objects are active, there’s no iteration happening.
Why I removed this:
There’s still a downside: if you have even one object active, it iterates through all the inactive ones too.
My final solution fixed this, and also reduces the cost of the module since it doesn’t need to call :_ValidateRefresher()
all the time.
The solution:
Instead of iterating over every part, we only iterate over the active ones!
We now have 2 dictionaries: AllObjects
and ObjectsRenderQueue
.
When an object is inactive, it’s removed from the render queue, and added back in when made active again.
Our render logic only iterates through the render queue, which is empty in the flashbang case.
This also makes our render logic faster in it of itself, since it no longer has to check if the object is active. If it’s in that queue, we know it is!
This is a huge performance gain.
The previous version would be impacted by static objects. In our flashbang example, everything was static. But it matters in other uses too! If you have a large static map, you’d have rendered them at 0 FPS too! Now that won’t crush your performance!