Optimizing performance when deleting objects #5488
Unanswered
tristanbob
asked this question in
Technical questions about the codebase
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have done some load-testing of GDevelop, including creating up to 1 million sprites. I know this should never be used in a game, but this process can help find performance problems and memory leaks. (See the project I used here: GDevelopApp/GDevelop-examples#544)
What I noticed is that deleting sprites takes a lot longer than creating them. We have two use cases to investigate:
1) Deleting objects in a running scene
How is this performed? The user uses the "delete object" action, which is defined
in
Core\GDCore\Extensions\Builtin\BaseObjectExtension.cpp
:That "Delete" is defined in
GDJS\GDJS\Extensions\Builtin\BaseObjectExtension.cpp
The
deleteFromScene
function is defined inGDJS\Runtime\runtimeobject.ts
:markObjectForDeletion
is inGDJS\Runtime\RuntimeInstanceContainer.ts
This step performs several tasks, including:
allInstances
array usingallInstances.splice(i, 1);
expression. This will removes one element at a time, and change the indexes of all items to the right of the selected object. This method has a time complexity of O(n).If the order of elements in the array doesn't matter, we can improve the efficiency of the removal by swapping the element to remove with the last element, and then popping it off the end of the array. This method will have a time complexity of O(1). https://www.30secondsofcode.org/js/s/fast-remove-array-element/
instancesRemoved
array. It seems that this array is used along with theinstancesCache
array to reuse objects. I'm not sure about the full implementation of this, so I can't try to identify performance optimizations.GDJS\Runtime\runtimeobject.ts
2) Deleting objects when changing scenes
In theory, this should be faster because we don't care about existing object instances and can erase the entire structure. @D8H suggested this is being done in
GDevelop/GDJS/Runtime/RuntimeInstanceContainer.ts
, by assigning the instanceContainer array to an empty array[]
.However, I want to understand how that code gets executed when an event uses the "Change the Scene" action.
The "Change the scene" action is defined in:
Core\GDCore\Extensions\Builtin\SceneExtension.cpp
:That
scene
function is defined inGDJS\GDJS\Extensions\Builtin\SceneExtension.cpp
:That "replaceScene" function is defined in
GDJS\Runtime\events-tools\runtimescenetools.ts
:requestChange
andSceneChangeRequest
are described inGDJS\Runtime\runtimescene.ts
:Now that a requestedChange has been set, I think it is handled by the
step
function inGDJS\Runtime\scenestack.ts
:Our requestedChange (
replace
) is performed in that same file:The
unloadScene
function is defined inGDJS\Runtime\runtimescene.ts
:The
destroy
function is defined right below that:And finally, the
super._destroy()
function is what I listed at the start of this section.Did I follow the code logic correctly?
Beta Was this translation helpful? Give feedback.
All reactions