Skip to content

Commit

Permalink
ref: isParentRemoved to cache subtree (#1543)
Browse files Browse the repository at this point in the history
* ref: isParentRemoved to cache subtree
* ref: cache at insertion too
* ref: remove wrapper function

---------

Co-authored-by: Justin Halsall <[email protected]>
  • Loading branch information
JonasBa and Juice10 authored Oct 4, 2024
1 parent 02cc62d commit 53b83bb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-pots-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rrweb": minor
---

Optimize isParentRemoved check
45 changes: 26 additions & 19 deletions packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export default class MutationBuffer {
private addedSet = new Set<Node>();
private movedSet = new Set<Node>();
private droppedSet = new Set<Node>();
private removesSubTreeCache = new Set<Node>();

private mutationCb: observerParam['mutationCb'];
private blockClass: observerParam['blockClass'];
Expand Down Expand Up @@ -367,7 +368,7 @@ export default class MutationBuffer {

for (const n of this.movedSet) {
if (
isParentRemoved(this.removes, n, this.mirror) &&
isParentRemoved(this.removesSubTreeCache, n, this.mirror) &&
!this.movedSet.has(dom.parentNode(n)!)
) {
continue;
Expand All @@ -378,7 +379,7 @@ export default class MutationBuffer {
for (const n of this.addedSet) {
if (
!isAncestorInSet(this.droppedSet, n) &&
!isParentRemoved(this.removes, n, this.mirror)
!isParentRemoved(this.removesSubTreeCache, n, this.mirror)
) {
pushAdd(n);
} else if (isAncestorInSet(this.movedSet, n)) {
Expand Down Expand Up @@ -514,6 +515,7 @@ export default class MutationBuffer {
this.addedSet = new Set<Node>();
this.movedSet = new Set<Node>();
this.droppedSet = new Set<Node>();
this.removesSubTreeCache = new Set<Node>();
this.movedMap = {};

this.mutationCb(payload);
Expand Down Expand Up @@ -740,6 +742,7 @@ export default class MutationBuffer {
? true
: undefined,
});
processRemoves(n, this.removesSubTreeCache);
}
this.mapRemoves.push(n);
});
Expand Down Expand Up @@ -803,29 +806,33 @@ function deepDelete(addsSet: Set<Node>, n: Node) {
dom.childNodes(n).forEach((childN) => deepDelete(addsSet, childN));
}

function isParentRemoved(
removes: removedNodeMutation[],
n: Node,
mirror: Mirror,
): boolean {
if (removes.length === 0) return false;
function processRemoves(n: Node, cache: Set<Node>) {
const queue = [n];

while (queue.length) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const next = queue.pop()!;
if (cache.has(next)) continue;
cache.add(next);
dom.childNodes(next).forEach((n) => queue.push(n));
}

return;
}

function isParentRemoved(removes: Set<Node>, n: Node, mirror: Mirror): boolean {
if (removes.size === 0) return false;
return _isParentRemoved(removes, n, mirror);
}

function _isParentRemoved(
removes: removedNodeMutation[],
removes: Set<Node>,
n: Node,
mirror: Mirror,
_mirror: Mirror,
): boolean {
let node: ParentNode | null = dom.parentNode(n);
while (node) {
const parentId = mirror.getId(node);
if (removes.some((r) => r.id === parentId)) {
return true;
}
node = dom.parentNode(node);
}
return false;
const node: ParentNode | null = dom.parentNode(n);
if (!node) return false;
return removes.has(node);
}

function isAncestorInSet(set: Set<Node>, n: Node): boolean {
Expand Down

0 comments on commit 53b83bb

Please sign in to comment.