Improve HashMap memory usage, add sorting capability #2556
Merged
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.
The
HashMap
class template currently stores both keys and values using double-indirection. This is necessary for object types (e.g. String) but for simple (scalar) types it is very wasteful of memory.This PR delegates key/value storage to separate list class optimised for either scalar or pointer (class) types.
Also, the allocation algorithm has been adjusted to expand storage by 4 or 16 entries, instead of only one.
The
stdc++
library has also been added so thatstd::map
andstd::vector
can be used to compare memory usage.The
Wiring
module inHostTests
has been updated to show this.A
HashMap<MimeType, uint16_t>
containing 13 items now uses 96 bytes instead of 182.std::map
uses 312 bytes.NB. enum declarations default to 32-bits, so reducing storage class for
MimeType
to uint8_t reduces map further to 48 bytes, whilststd::map
reduces to 260 bytes.One reason for the reduced memory usage is that HashMap uses two lists: one for keys, and a second for values, whilst
std::map
uses a single list ofstd::pair
structures. These structures must be aligned so with small data types there is unused space.Note, however, that a
std::map
will likely out-perform a simple HashMap for larger data sets as they use more sophisticated storage mechanisms https://en.cppreference.com/w/cpp/container/map. HashMap is much simpler, does not sort the keys and does lookups by iterating from the start of the map.This PR also adds a basic
sort()
method so entries can be re-ordered, perhaps as part of post-processing or display.