Tessil / sparse-map

C++ implementation of a memory efficient hash map and hash set
MIT License
334 stars 36 forks source link

Question about the shrinking logic of sparse_map #7

Closed freak82 closed 5 years ago

freak82 commented 5 years ago

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.

  1. 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?
  2. The sparse_map/hash seems to clear_deleted_buckets upon insertion if the m_load_threshold_clear_deleted is reached. However, clear_deleted_buckets calls rehash_impl with the current buckets count m_bucket_count. So, it seems to me that the bucket array also doesn't shrink back?
Tessil commented 5 years ago

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.

freak82 commented 5 years ago

Thanks for the response. I'm closing the issue.