Various memory/efficiency improvements for control/PCM ROM loader #19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 ofpush_back()
operations, this can result in many expensive calls tomalloc()
/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
![image](https://user-images.githubusercontent.com/5890747/205401893-841d7be7-33ec-4b50-a949-df08a6fd16a6.png)
libemusc.so
from ~13000 to ~3000 as shown by the following heaptrack trace:There are probably more places where this can be improved further, but this should take care of the low-hanging fruit.
Cheers!