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
DOMPurify: convert to a service #27260
Conversation
8c9850f
to
efb10c0
Compare
efb10c0
to
ead7834
Compare
This pull request introduces 1 alert when merging ead7834 into b3f1936 - view on LGTM.com new alerts:
|
This is a great change, can you please add flamecharts from the mid-range Nokia device as well? |
@choumx: I'm confused by the lint error suggesting I switch to Note that I'm only using the services plumbing to create a Singleton, the service is never used by other extensions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aghh github
#22414 (comment) for context on the sync service raciness issue. |
Re: flame chart screenshots, can we keep the time interval the same across the before/after? |
000c3cf
to
ffcc2ac
Compare
2a7bacc
to
424e9ec
Compare
9e3aeb8
to
e2ece3d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just one question.
src/purifier/purifier.js
Outdated
this.doc_ = doc; | ||
constructor(opt_config, opt_attrRewrite) { | ||
/** @private {Document} */ | ||
this.doc_ = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why move this out of the constructor and into each function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How else could it be a window level service that could operate on all the ampdocs of the page unless it took the doc
as a function param?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
definitely the messiest part of this PR, since the purifier hooks are registered in the constructor but the doc it operates on changes each invocation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, does this.doc_
actually change across invocations?
A bit confusing but there can be multiple AmpDoc per window but only one Document. I.e. PWAs have one AmpDoc per shadow AMP but share a single Document. AMP ads have their own window since they're iframed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm so glad for this question then :). I can basically revert all of the changes to purifier.js
now. I had assumed they had different document
s.
Let's also add some context in the PR description for posterity: why not a doc service (sync or async)? |
Love the MS Paint vibe scribbles. |
f7dd8b7
to
04c6788
Compare
Once we get this in, I'll create a followup PR to fix test warnings in the tests related to unexpected logging (not introduced by this PR). Writing this here as a reminder |
@jridgewell / @choumx: I reverted most of the changes, this is a pretty small PR now. Let me know if it still looks good and if so I'll 🚢 |
* DOMPurify: convert to a service * bad parenthesezing * newed fns must be reg * lint * move purifier to window level service * fix types and add TODO * lint * tests + lints * update .d.ts * move ivar back to its old place * presubmit fix * restore global state * revert all changes related to mistaking multiple ampdocs with multiple docs * 'this' strikes again :O
summary
Addresses #27249.
Prior to this PR,
DOMPurify
was initialized for each and every<amp-script>
and<amp-mustache>
element on the page. For mustache templates, this could sometimes be a significant bottleneck. This PR opts to instead create a single instance of DOMPurify per window. Initially I had moved it to a doc-level service, but that proved troublesome seeing as we ban the sync retrieval of doc services (#22414 (comment)), and all usages of the purifier are currently sync (converting to async would have required a lot of plumbing).In the flamegraph below you can see that a page with ~30
<amp-mustache>
elements introduces 200ms of work. After this fix, that is brought down to 26ms.Note: screenshots were taken with 6x CPU Throttling.
before
after