Optimization using Servers NOT living up to its promise?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By toblin

I’m optimizing using servers for a rather simple application of having cells, represented by 3D spheres, simply being born and attracted to each other when they are in close proximity. Either using custom physics or by using Area gravity.

Because I want to have up to thousands of instances, I have read read about optimization here, which promises the following regarding optimization using servers:

“For large amount of instances (in the thousands), that need to be constantly processed (and certain amount of control needs to be retained), using servers directly is the recommended optimization.”

Now, I’ve done that, and I get around 2 FPS already at ~100 instances. This seems to me equivalent to the case when I used scenes. Hence, no qualitative difference in user experience between using servers vs using scenes. And far from the promise of being able to handle thousands of instances.

I’m hoping that the reason is my incompetence. It would be greatly appreciated if you could spot my folly by copying and/or reviewing the code I’ve pasted here. There are only two scripts: 1) Spatial.gd attached to a main scene with a single Spatial node, and 2) Cell.gd as an unattached script that exist in the FileSystem. It should be trivial to create a new project, add a Spatial node as the main scene with the Spatial.gd attached, and create a new custom script with the code from Cell.gd. Then simply run the project, whereby you will see 125 cells spawned with low FPS. The cells consist of 1) a spherical rigid body, 2) a spherical visual mesh, and 3) a spherical Area that is used to attract other cells through gravity.

What am I doing wrong? What have I misunderstood?

Best regards,

Tobias

There’s something off, I can’t run your scripts. There is no physics_process for Cell.gd. Also transmission_area is undefined.

omggomb | 2020-04-27 18:34

Note that using servers directly won’t provide an enormous gain, it just removes some overhead, but it’s not a lot. I’d expect 20% memory reduction, and eventually 5% speedup (more in other cases not involving GDScript). In addition, if you end up doing more per frame for bookeeping, sometimes it might even be worse due to the overhead of GDScript itself. So depending on what you are doing, using servers directly might not always be the lowest hanging fruit when it comes to speed.

Also, about performance: profile. Find where most of the time is spent first, before doing anything. Did you see where it was?

Also: 125 spherical rigidbodies colliding with each other surely will bring fps down, from what I’ve seen in the past. They may run fine if they aren’t clumped interacting into one place, though.

Zylann | 2020-04-27 19:28

Consider reducing the detail level for spheres as well, especially if they’re displayed from far away.

Calinou | 2020-04-27 19:34

@omggomb

There’s something off, I can’t run your scripts. There is no physics_process for Cell.gd. Also transmission_area is undefined.

Damn, sorry. Was apparently a bit sloppy when simplifying the code for demonstration. I’ve tested the code now and edited the pastelink. It should work now. ################################################ MAIN SCENE SCRIPT # - Pastebin.com

toblin | 2020-04-27 20:32

@Zylann, that spells bad news for my project I’m afraid. What takes the most time are the cell functions (not mentioned here), and not the physics, but I can run the cell functions on a longer time scale than the physics, so I considered it worth optimizing.

Also, about performance: profile. Find where most of the time is spent first, before doing anything. Did you see where it was?

Hm… I have not see where most time is spent when it comes to Physics. Do you mean there’s a way to find out…?

@Calinoue

Consider reducing the detail level for spheres as well, especially if they’re displayed from far away.

Yes, that I will definitely do. I don’t need high resolution of the spheres at all.

toblin | 2020-04-27 20:37

Take a look at the profiler: Overview of debugging tools — Godot Engine (stable) documentation in English

Zylann | 2020-04-27 20:51

If you don’t need physics simulations to be very accurate and low-latency, you could decrease the physics FPS to 30 in the Project Settings and use lawnjelly’s smoothing add-on for interpolation.

Calinou | 2020-04-28 07:49

@Zylann, I’m using the profiler, and I’m liking it a lot B-). It has lead me to understand that setting the radius of the objects is taking strangely long.

@Calinou, cool with the smoothing add-on! Any ideas on how to implement whilst spawning objects using the Physics and Visual servers directly…?

toblin | 2020-04-28 16:49

@toblin I answered to your new question

Zylann | 2020-04-28 17:41