Good evening, everyone,
I am currently working on this project:
Interactive Lattice Visualizer, an interactive lattice visualizer. While it is not yet ready for a GSDEMO submission, I hope to submit it once I’ve refined it further. However, I’m facing a major issue that I can’t resolve on my own and would greatly appreciate your help.
The Issue:
After multiple scene reloads, the looping and rendering of the scene slows to an unacceptable level. Each time the scene parameters (e.g., the number of nodes or lattice type) are changed, the active scene is deleted using scene.delete() and a new one is created. This process occurs around line 286 in my load_it() method. According to the documentation, scene.delete() should remove all objects in the canvas.
To reproduce this issue, try setting the number of nodes to 4000 a couple of times, then revert it to 40. You’ll notice that the performance of the 40-node lattice after these reloads is significantly worse than the performance of the initial 40-node lattice when the page first loads.
Possible Cause:
I suspect that while scene.delete() removes objects from the canvas, it doesn’t clean up all associated data, including pointers to objects stored in self.obj (from my Node class). If that’s the case, these stale pointers might accumulate in memory, numbering in the tens of thousands after repeated reloads, which could explain the stagnation. If this hypothesis is correct, I may need to explicitly clear or overwrite all nodes whenever I reload the lattice/scene.
Request for Help:
Does anyone have ideas on how to efficiently reload the scene and/or completely clear all stored data without requiring a manual page refresh?
Additionally, if you have any comments or suggestions regarding my code in general, I’d greatly appreciate your feedback!
Thank you in advance for your time and help!
This is interesting. Based on your suggestion, I created a new program called Object Deletion Test, which simply loads a cube of simple_sphere objects. Initially, after testing larger grid sizes and reloading numerous times with the scene.delete() method, I couldn’t detect any performance decrease compared to the very first render upon page or program loading.
This result was puzzling, so I incorporated the attach_arrow objects into the new test program. I use these attach_arrow objects to render node connections in my main Lattice program. After adding them to the test program, I was able to reproduce the performance drop I had originally observed after multiple reloads.
System Information:I also tested the program on another machine and managed to replicate the behavior there as well.
Observations:In the new Object Deletion Test, the performance degradation becomes noticeable under the following conditions:
Each reload regenerates the 4^3 simple_sphere objects with attach_arrow connections. After 10 reloads, a drop in performance becomes apparent. By 20 reloads, the simulation appears to run at half speed.
Importantly, when performing the same actions without enabling the arrows, there is no noticeable drop in performance, even after numerous reloads.
Additional Notes:I had intended to use the rendering tools to monitor updates per second and frame times (and possibly other metrics), but I’ve forgotten the syntax for enabling these tools. Unfortunately, I couldn’t find any relevant information in the documentation. If this feature is documented, I’d appreciate a pointer to the relevant section.
Thanks for the help,
-Maximillian
I wanted to document my solution for future readers of this thread who may encounter the same problem. In my original implementation, when reloading the scene with new parameters (such as the number of nodes or sphere objects), I would call the scene.delete() method, break my main while loop that updates the scene's objects, and then create a new scene with new objects. Over time, this approach led to performance issues after multiple reloads, especially during simulations with a high quantity of objects. Although this problem might not affect everyone, I wanted to resolve it.
Instead of relying on the scene.delete() method, I redesigned my classes and other methods to reuse the objects created in the scene. My solution eliminates the use of the scene.delete() method and instead "recycles" the objects within the scene. When certain objects are no longer needed (e.g., reducing the number of spheres), I set their visibility to False and remove them from my active list of objects. When increasing the number of objects, I retrieve the new objects from this list of previously recycled objects, before going to create new ones. Although I am only using simple spheres and cylinder edges, this approach has proven effective.
I tested and implemented this solution in the Object Deletion Test program mentioned above and subsequently integrated it into my main program. With this method, I observe no performance degradation, regardless of the number of scene reloads.
Best regards,
Maximillian