Question about the shrinking logic of sparse_map
Closed this issue · 2 comments
I'm using the sparse_map in scenario where the lower memory consumption is more important than the slightly increased CPU.
My current usage is like this:
tsl::sparse_map<
some_key
some_value,
boost::hash<some_key>,
std::equal_to<some_key>,
std::allocator<std::pair<some_key, some_value>>,
tsl::sh::prime_growth_policy,
tsl::sh::exception_safety::basic,
tsl::sh::sparsity::high> map;
And then in the parent object constructor I set
map.max_load_factor(0.8f);
This seems to be working fine when the sparse_map needs to grow.
However, I'm not sure how the sparse_map behaves when it shrinks and if it shrinks?
Here are my observations from the source code of the sparse_map.
Please, correct me if I'm wrong.
- The sparse_array doesn't seem to have shrinking logic unless it's explicitly cleared which only happens at sparse_map.clear where all buckets are cleared. Is this correct?
- The sparse_map/hash seems to
clear_deleted_buckets
upon insertion if them_load_threshold_clear_deleted
is reached. However,clear_deleted_buckets
callsrehash_impl
with the current buckets countm_bucket_count
. So, it seems to me that the bucket array also doesn't shrink back?
Hello,
You're correct in your observations. The map behaves in the same way as std::unordered_map
and thus never shrinks except if rehash(0)
is explicitly called.
It would be possible for me to implement a min_load_factor
similar to google::sparse_hash_map
and spp::sparse_hash_map
if needed.
Thanks for the response.
I'm closing the issue.