Make nsDOMMutationObserver receiver lookup O(1)
Categories
(Core :: DOM: Core & HTML, enhancement)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox156 | --- | fixed |
People
(Reporter: mayankleoboy1, Assigned: mayankleoboy1)
References
(Blocks 1 open bug)
Details
Attachments
(1 file)
| Assignee | ||
Comment 1•1 month ago
|
||
GetReceiverFor linear-scanned mReceivers. It runs once per observe() and,
via GetAllSubtreeObserversFor, once per ancestor that has an observer on
every mutation, so a single MutationObserver watching N nodes was O(N^2) on
setup and paid O(N) per mutation. This is the more idiomatic usage than the
many-observers-on-one-node case in bug 1952616: frameworks share one
observer across many targets.
mReceivers stays the owning, ordered storage and mReceiverMap is added
purely as a lookup index. Replacing the array outright would be smaller, but
three loops over mReceivers - the CC unlink body, Disconnect, and
RemoveClones - call into receiver code that can run arbitrary destructors,
which is safe while indexing an array and is mutation-during-iteration on a
hashtable. The array also gives GetObservingInfo (ChromeOnly) a stable
order. So the array keeps its job and the map only answers "which receiver
watches this node".
Keys are raw nsINode*. They cannot dangle: a target that dies runs
nsMutationReceiver::NodeWillBeDestroyed, which calls Disconnect(true), which
removes the entry before the node is gone.
RemoveReceiver takes the target explicitly because Disconnect clears mTarget
before calling it. Its mReceivers.RemoveObject is still O(N) and is left
that way deliberately: it is only reached when one target dies, while the
bulk paths (Disconnect, CC unlink) drop every receiver at once and are O(N)
in total. Making it O(1) would need either unordered removal plus an index,
reintroducing the two-structures-in-sync problem this patch avoids, or
dropping the array, which the iteration hazard above rules out.
Unmeasured: no profile drove this, it fell out of reviewing the bug 1952616
patch. The complexity claim is from source, not from a benchmark.
| Assignee | ||
Comment 2•1 month ago
|
||
Updated•1 month ago
|
Updated•1 month ago
|
Comment 4•1 month ago
|
||
| bugherder | ||
Updated•1 month ago
|
Description
•