GDNative: Is it possible to use an Object Pool in Godot for instancing Scenes and Objects?

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

Can I use object pool for scenes? For i.e. I want to instance a lot of game units to have ability quickly create and delete it in game and don’t worry about performance. Can I just use static array for all objects or there can be gotchas?

I mean in GDNative c++

:bust_in_silhouette: Reply From: DaddyMonster

It is possible but Juan is clear that object pooling doesn’t deliver benefits in Godot like it does in Unity, etc:

https://twitter.com/reduzio/status/1073284242086551552?lang=en

What can be worthwhile is making a singleton to make the first instance of objs as this can make it hang for a moment on first load during runtime but this isn’t the same as object pooling which is not necessary.

object pooling doesn’t deliver benefits in Godot

Why not?

Robotex | 2021-01-29 16:13

Because Juan says so. He explains the technical details on twitter I linked.

DaddyMonster | 2021-01-29 18:06

She says just:

You don’t need to do that in Godot because allocating/freeing scenes/classes is fast and there is no garbage collector in GDScript/Godot.

I don’t see any technical details here. How did they increased the speed of memory allocations? How did they solved memory fragmentation? Is cache misses possible in Godot?

Robotex | 2021-01-29 18:20

For i.e. if I create RTS game with 100 000 units on map, will I have a problems with performance?

Robotex | 2021-01-29 18:23

I’m using C++ instead of GDScript

Robotex | 2021-01-29 18:24

What Juan says is correct but this is for GDScript only as it has no garbage collector. Object pooling is a solution for Garbage collector. So for a language like C# you need object pooling and this is said in Godot Docs under Optimization/CPU Optimization/Languages/C#. And for C++ if you use new and delete keywords for instancing then you won’t need to implement object pooling as garbage collecting in C++ is only available when you are not doing dynamic memory allocation.
Hope this helps.

Karim | 2021-02-05 11:22

Object pooling is a solution for Garbage collector

It is also a solution for low memory allocation and for memory fragmentation. Are this promlems not exists in Godot?

Robotex | 2021-02-05 12:09

And for C++ if you use new and delete keywords for instancing then you won’t need to implement object pooling

You are not right. new and delete is very slow in C++. So, you can’t do it inside game loop, you should to allocate all memory before the game loop and delete everything when it finishes.

Robotex | 2021-02-05 12:10

:bust_in_silhouette: Reply From: lofi

they say instance and free are pretty optimized, i remember reading a post from 2018 or so here or on reddit from a dude making a gdscript pooling system for a bullet hell shmup, he gained performance instead of relying on instance and free, you could search for it,

bullet hell pooling or something like that

so for asking your question if you want you can, the how is up to you.

they say instance and free are pretty optimized

Do they use object pool somewhere inside Godot for this optimizations?

Robotex | 2021-01-29 18:22