[webkit-dev] WeakHashMap

Ryosuke Niwa rniwa at webkit.org
Fri Jun 11 22:36:28 PDT 2021


Hi all,

I've added WeakHashMap to WTF in https://commits.webkit.org/r278803, which
allows the use of WeakPtr as keys. Like WeakHashSet, WeakHashMap does not
immediately delete the key or the value when the object pointed by
WeakPtr goes away (i.e. WeakPtrImpl::m_ptr becomes nullptr).

Instead, it relies on rehashing and amortized deletion to remove these
entries. This happens either when enough items are inserted or removed such
that the underlying HashMap goes through rehashing or when WeakHashMap's
entry is accessed (get, find, advancing an iterator, etc...) or mutation
(inserting, removing, etc...) more than twice the number of items in the
underlying HashMap.

Due to this amortized deletion behavior, there is no unbound growth of
HashMap even if no attempt to cleanup null WeakPtr is made. However, if
WeakHashMap is never accessed or mutated, all WeakPtrImpl as well as the
corresponding hash map values will be kept alive. In the situation where
this is undesirable (e.g. WeakHashMap could retain a large object as its
values or a large number of keys could become stale), call
WeakHashMap::nullReferences() to trigger the cleanup manually.

- R. Niwa
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20210611/36c70391/attachment.htm>


More information about the webkit-dev mailing list