Page 1 of 1

Possible to object pool to load gameobjects

Posted: Thu Sep 16, 2021 6:24 pm
by osteon1992
Currently I'm using ES3 to save game objects in the scene upon exiting and loading them when the scene comes back (using ES3.Save<List<GameObject>> and ES3.Load<List<GameObject>>). It works pretty well except that the performance chugs due to instantiating 200+ objects and destroying them. As much as possible I don't want this to happen especially for people who might have weaker hardware. Is it possible to have some kind of object pooling system that works for different game objects with different components or is there a better way for me to save and load everything? Additional note, I can't add a load screen when saving or loading because this is a multiplayer game where the server has to load everything for the client.

Re: Possible to object pool to load gameobjects

Posted: Thu Sep 16, 2021 8:02 pm
by Joel
Hi there,

Just to check, have you used Unity’s profiler to check that it is the instantiation of GameObjects causing the slow down? Generally this should be insignificant in comparison to deserialising the data.

All the best,
Joel

Re: Possible to object pool to load gameobjects

Posted: Thu Sep 16, 2021 9:08 pm
by osteon1992
According to the profiler, it happens when I load tries to read the list of objects. It also shows that the reader generates a lot of garbage from strings because it has to read 200+ objects. Is there a way to reduce the GC allocations.

According to this article, StringBuilder.ToString() generates garbage
https://www.sebaslab.com/zero-allocation-code-in-unity/

Re: Possible to object pool to load gameobjects

Posted: Fri Sep 17, 2021 7:28 am
by Joel
Hi there,

Deserialising objects inherently generates garbage because it needs to read the data, so is unavoidable.

Just to check, have you followed the Improving Performance guide?

https://docs.moodkie.com/easy-save-3/es ... rformance/

All the best,
Joel

Re: Possible to object pool to load gameobjects

Posted: Fri Sep 17, 2021 8:33 am
by osteon1992
Yes I read that. I replaced the StringBuilder with ZString from https://github.com/Cysharp/ZString.
Also I commented out the ES3Debug.Log since it was generating GC from string concat for every read. This is from ES3JsonReader ReadPropertyName. Results are attached below, pretty good.

Will ES3Debug be included when making builds or is it safe for me to comment it out?

Additional note:
Using ES3.CacheFile() adds about ~350ms and 3.5mb according to the profiler without improvement on the deserializing speed.

Re: Possible to object pool to load gameobjects

Posted: Fri Sep 17, 2021 1:32 pm
by Joel
Hi there,

Please could you produce an example script which I can drop into a new scene which demonstrates what you’re trying to achieve so that I can benchmark it at my end and provide suitable recommendations?

All the best,
Joel