0 votes

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++

Godot version 3.2.3
in Engine by (246 points)

2 Answers

0 votes

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.

by (217 points)

object pooling doesn't deliver benefits in Godot

Why not?

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

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?

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

I'm using C++ instead of GDScript

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.

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?

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.

0 votes

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.

by (106 points)

they say instance and free are pretty optimized

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.