Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Masking: Avoid the repeated calls to closest when recursing through the DOM #1349

Merged
merged 10 commits into from
Nov 24, 2023

Conversation

eoghanmurray
Copy link
Contributor

Ref #1337

This is an alternative take on #1338

Copy link

changeset-bot bot commented Nov 8, 2023

🦋 Changeset detected

Latest commit: 36618ab

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 8 packages
Name Type
rrweb-snapshot Patch
rrweb Patch
rrdom Patch
rrdom-nodejs Patch
rrweb-player Patch
@rrweb/types Patch
@rrweb/web-extension Patch
rrvideo Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

…correct as a newly added text node needs to check all ancestors as well as just the parentNode. This problem suggested a new approach which facilitated the removal of the ugly `needsMask: false` initialization:

 - needsMask===true means that an ancestor has tested positively for masking, and so this node and all descendends should be masked
 - needsMask===false means that no ancestors have tested positively for masking, we should check each node encountered
 - needsMask===undefined means that we don't know whether ancestors are masked or not (e.g. after a mutation) and should look up the tree
… text mutation - modify the test to show where this would have failed
@eoghanmurray eoghanmurray changed the title Masking Avoid the repeated calls to closest when recursing through the DOM Masking: Avoid the repeated calls to closest when recursing through the DOM Nov 8, 2023
@eoghanmurray eoghanmurray merged commit 07ac5c9 into rrweb-io:master Nov 24, 2023
5 of 6 checks passed
billyvg added a commit to getsentry/rrweb that referenced this pull request Apr 26, 2024
eoghanmurray added a commit to eoghanmurray/rrweb that referenced this pull request Jun 17, 2024
…ements' optimisation wasn't being applied as `n.childNodes` was always truthy even when there were no childNodes.

Changing it to `n.childNodes.length` directly there (see rrweb-io#1402) actually caused a bug as during a mutation, we serialize the text node directly, and need to jump to the parentElement to do the check.
This is why I've reimplemented this optimisation inside `needMaskingText` where we are already had an `isElement` test
eoghanmurray added a commit that referenced this pull request Jun 17, 2024
* Minor fixup for #1349; the 'we can avoid the check on leaf elements' optimisation wasn't being applied as `n.childNodes` was always truthy even when there were no childNodes.

Changing it to `n.childNodes.length` directly there (see #1402) actually caused a bug as during a mutation, we serialize the text node directly, and need to jump to the parentElement to do the check.
This is why I've reimplemented this optimisation inside `needMaskingText` where we are already had an `isElement` test

Thanks to @Paulhejia (https://github.com/Paulhejia/rrweb/) for spotting that `Boolean(n.childNodes)` is aways true.
jeffdnguyen pushed a commit to pendo-io/rrweb that referenced this pull request Jul 29, 2024
* Minor fixup for rrweb-io#1349; the 'we can avoid the check on leaf elements' optimisation wasn't being applied as `n.childNodes` was always truthy even when there were no childNodes.

Changing it to `n.childNodes.length` directly there (see rrweb-io#1402) actually caused a bug as during a mutation, we serialize the text node directly, and need to jump to the parentElement to do the check.
This is why I've reimplemented this optimisation inside `needMaskingText` where we are already had an `isElement` test

Thanks to @Paulhejia (https://github.com/Paulhejia/rrweb/) for spotting that `Boolean(n.childNodes)` is aways true.
jeffdnguyen pushed a commit to pendo-io/rrweb that referenced this pull request Jul 29, 2024
* Minor fixup for rrweb-io#1349; the 'we can avoid the check on leaf elements' optimisation wasn't being applied as `n.childNodes` was always truthy even when there were no childNodes.

Changing it to `n.childNodes.length` directly there (see rrweb-io#1402) actually caused a bug as during a mutation, we serialize the text node directly, and need to jump to the parentElement to do the check.
This is why I've reimplemented this optimisation inside `needMaskingText` where we are already had an `isElement` test

Thanks to @Paulhejia (https://github.com/Paulhejia/rrweb/) for spotting that `Boolean(n.childNodes)` is aways true.
jxiwang pushed a commit to amplitude/rrweb that referenced this pull request Jul 31, 2024
… the DOM (rrweb-io#1349)

* masking performance: avoid the repeated calls to `closest` when recursing through the DOM
 - needsMask===true means that an ancestor has tested positively for masking, and so this node and all descendents should be masked
 - needsMask===false means that no ancestors have tested positively for masking, we should check each node encountered
 - needsMask===undefined means that we don't know whether ancestors are masked or not (e.g. after a mutation) and should look up the tree
* Add tests including an explicit characterData mutation tests 
* Further performance improvement: avoid calls to `el.matches` when on a leaf node, e.g. a `<br/>`
---------

Authored-by: eoghanmurray <eoghan@getthere.ie>
Based on initial PR rrweb-io#1338 by Alexey Babik <alexey.babik@noibu.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants