Closed Bug 2066058 Opened 1 month ago Closed 1 month ago

Make nsDOMMutationObserver receiver lookup O(1)

Categories

(Core :: DOM: Core & HTML, enhancement)

enhancement

Tracking

()

RESOLVED FIXED
156 Branch
Tracking Status
firefox156 --- fixed

People

(Reporter: mayankleoboy1, Assigned: mayankleoboy1)

References

(Blocks 1 open bug)

Details

Attachments

(1 file)

No description provided.

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.

See Also: → 1952616
Assignee: nobody → mayankleoboy1
Status: NEW → ASSIGNED
Blocks: 1980560
Attachment #9630058 - Attachment description: WIP: Bug 2066058 - Make nsDOMMutationObserver receiver lookup O(1). → Bug 2066058 - Make nsDOMMutationObserver receiver lookup O(1). r=jjaschke
Status: ASSIGNED → RESOLVED
Closed: 1 month ago
Resolution: --- → FIXED
Target Milestone: --- → 156 Branch
QA Whiteboard: [qa-triage-done-c157/b156]
See Also: → 1988776
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: