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

Various memory/efficiency improvements for control/PCM ROM loader #19

Merged
merged 6 commits into from
Dec 3, 2022

Conversation

dwhinham
Copy link
Contributor

@dwhinham dwhinham commented Dec 2, 2022

std::vector is a container which can trigger requests for more memory from the operating system every time its heap-allocated storage is filled. When performing thousands of push_back() operations, this can result in many expensive calls to malloc()/free() (and copy/move operations) behind the scenes, as larger chunks of memory are requested, data is moved from the old location to the new location, and the old location is freed.

On modern machines with operating systems that provide sophisticated memory allocators, memory fragmentation isn't a problem and we can get away with this.

However, on embedded/baremetal systems which aren't able to avoid memory fragmentation, this can cause problems, so we should try to avoid unnecessary reallocations by reserving the space up-front whenever it is known.

This PR reduces overall memory allocations inside libemusc.so from ~13000 to ~3000 as shown by the following heaptrack trace:
image

There are probably more places where this can be improved further, but this should take care of the low-hanging fruit.

Cheers!

@skjelten skjelten merged commit 7a2112c into skjelten:master Dec 3, 2022
@skjelten
Copy link
Owner

skjelten commented Dec 3, 2022

Nice and clean, thanks a lot!

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

2 participants