EsotericSoftware / spine-runtimes

2D skeletal animation runtimes for Spine.
http://esotericsoftware.com/
Other
4.35k stars 2.89k forks source link

[spine-cpp] Reduce the number of calls to malloc #2179

Open zhiyangyou opened 1 year ago

zhiyangyou commented 1 year ago

String strPathForAtt(path.buffer(), true); RegionAttachment region = _attachmentLoader->newRegionAttachment(skin, String(name), strPathForAtt); strPathForAtt.unown();

badlogic commented 1 year ago

Thanks, those are some great suggestions. Ideally, the allocator could be self-defined, so one could e.g. use a bump allocator for loading skeletons, which would significantly cut the number of mallocs.

zhiyangyou commented 1 year ago

Thanks, those are some great suggestions. Ideally, the allocator could be self-defined, so one could e.g. use a bump allocator for loading skeletons, which would significantly cut the number of mallocs.

bump allocator , this is an excellent suggestion for me! ^_^,thanks a lot !

I am a newbie in c++ :( , can you please recommend 1 such allocator to me.

badlogic commented 1 year ago

I don't know of any 3rd party libraries for that I'm afraid.

zhiyangyou commented 1 year ago

I have noticed the SpineExtension class, but I don't know how to introduce some high-performance allocators to avoid some memory fragmentation. I don't know how other applications integrate the spine-cpp runtime library.

:( I checked a lot of blogs and they told me that malloc calls can only be minimized, not avoided. Ask if there is a better deal for me..

zhiyangyou commented 1 year ago

Do you think it's a good idea to encapsulate the deallocate and allocate methods of std::allocator, and add some cookie information to the header of the requested memory block?

badlogic commented 1 year ago

I don't see how that'd help. Implementing a bump allocator that can beat malloc as we use it now is not hard. The issue is with object life times. A first step would be for anything allocated during loading of SkeletonData via SkeletonJson or SkeletonBinary to be allocated by a bump allocator, while any other object type like Skeleton uses malloc. This requires some refactoring of the runtime. There are a lot of reallocations happening during parsing of data, which doesn't lend itself well to bump allocation. I'm afraid there's no simple, immediate fix for this.

zhiyangyou commented 1 year ago

Ok, my doubt is gone, thanks for your reply. :)

Thanks to the spine-cpp runtime, it solved a performance hotspot in my game.