Details about the memory usage of each map
Hi,
It is said in the doc that
When a weak pointer expires, its mapping is lazily removed.
Could you give more details in the documentation about the conditions under which expired mappings are physically removed from the map, and what is the expected latency between the expiration and removal? A naive implementation I have in mind would only remove the binding when the key is fetched after it expired, and if the key is never fetched, then it will never be deleted, resulting in a memory leak.
In other words, do you guarantee that every expired binding will eventually be removed?
Sorry for being so slow on this!
It is guaranteed that an expired entry will be replaced when there is sufficient memory pressure. When you insert a new entry, the table has to find a bucket to store it in. It favors removing expired entries over growing the size of the backing array. Does that make sense? And does it sufficiently answer your question?
If you need entries to expire sooner, you there’s a method remove_expired, but this takes time linear in the table’s capacity, which means you need to be careful how often you do it (or you might as well not be using a hash table at all).