Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mem shrink rlottie. #516

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

Mem shrink rlottie. #516

wants to merge 19 commits into from

Conversation

X-Ryl669
Copy link

@X-Ryl669 X-Ryl669 commented Jan 6, 2022

I'm trying to use rlottie on ESP32.
It's a very capable embedded system but with few memory (hundred of kB of heap space only)

This PR contains many work to reduce the memory consumption of rlottie:

Using partial rendering

This is in a separate PR #512 . Basically, it allow to use a much smaller rendering buffer

Replacing RapidJSON

RapidJSON might be rapid (not sure about this true), it's not memory efficient. Parsing with RapidJSON implies allocating a buffer that's the same size as the lottie's JSON file in the heap. On memory constrained system, it's a no-no.
So I'm replacing it with my own JSON SAX parser that's able to parse a read only string. The parsing is much simpler and a lot more memory efficient since you can use a flash based filesystem to map the rlottie JSON file in address space (like ESPFS on ESP32) and avoid consuming the heap at all.
My parser is slower than RapidJSON on AMD64 system, but faster on ESP32. I guess it's due to not implementing SIMD optimization on AMD64/x86 in my parser.

Avoiding typical STL memory dumbness.

When using std::getline(), internally, it append char by char on a std::string which will allocate a buffer that's twice the required size due to exponential allocation algorithm in std::string (and std::vector as well). So if you have a JSON file that's 16385 bytes, you'll end up using 32768 bytes of heap. I've removed this since the file size is known beforehand so we can allocate the exact amount required.

Fix a off-by-one error in frame handling

Rlottie does not allow rendering the last frame of an animation. This PR fixes it.

…ndering in passes

and does not require a `width * height * 4` bytes buffer anymore. On limited memory MCU,
this will allow rendering a large animation by trading off memory for CPU consumption instead.
…se exponential growth on memory allocation.

Only allocate what's required and not more.
…tion for copying the data.

The JSON file can now come from ROM/Flash
A Lottie file says the number of frames (from start to end, included) but rlottie was using start to end (excluded), leaving away the last frame of an animation.
This commit fixes this so that all frames are included.
@X-Ryl669
Copy link
Author

X-Ryl669 commented Jan 6, 2022

Using valgrind as a memory allocation benchmark, on a 24kB lottie JSON file, I've shrinked the heap memory usage from 380kB down to 290kB with my patch.

@X-Ryl669 X-Ryl669 mentioned this pull request Jan 20, 2022
@X-Ryl669 X-Ryl669 force-pushed the memShrink branch 2 times, most recently from fe888f5 to 270c6ba Compare January 21, 2022 17:47
@X-Ryl669 X-Ryl669 force-pushed the memShrink branch 2 times, most recently from d7a822b to c7915c6 Compare March 16, 2022 06:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant