Possible to object pool to load gameobjects

Discussion and help for Easy Save 3
Post Reply
osteon1992
Posts: 3
Joined: Thu Sep 16, 2021 6:18 pm

Possible to object pool to load gameobjects

Post 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.
User avatar
Joel
Moodkie Staff
Posts: 4825
Joined: Wed Nov 07, 2012 10:32 pm

Re: Possible to object pool to load gameobjects

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
osteon1992
Posts: 3
Joined: Thu Sep 16, 2021 6:18 pm

Re: Possible to object pool to load gameobjects

Post 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/
Attachments
2nd.PNG
2nd.PNG (73.1 KiB) Viewed 1150 times
1st.PNG
1st.PNG (63.83 KiB) Viewed 1150 times
User avatar
Joel
Moodkie Staff
Posts: 4825
Joined: Wed Nov 07, 2012 10:32 pm

Re: Possible to object pool to load gameobjects

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
osteon1992
Posts: 3
Joined: Thu Sep 16, 2021 6:18 pm

Re: Possible to object pool to load gameobjects

Post 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.
Attachments
4th.PNG
4th.PNG (9.79 KiB) Viewed 1131 times
3rd.PNG
3rd.PNG (78.88 KiB) Viewed 1131 times
User avatar
Joel
Moodkie Staff
Posts: 4825
Joined: Wed Nov 07, 2012 10:32 pm

Re: Possible to object pool to load gameobjects

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply