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

Replay hinders performance on Angular apps #11661

Closed
6 tasks
billyvg opened this issue Apr 17, 2024 · 7 comments · Fixed by getsentry/rrweb#176
Closed
6 tasks

Replay hinders performance on Angular apps #11661

billyvg opened this issue Apr 17, 2024 · 7 comments · Fixed by getsentry/rrweb#176
Labels
Package: angular Issues related to the Sentry Angular SDK Package: replay Issues related to the Sentry Replay SDK Stale

Comments

@billyvg
Copy link
Member

billyvg commented Apr 17, 2024

Related to #6946 (comment) - Replay can still cause performance regressions for Angular apps when calling the Angular-patched versions of certain native functions that results in trigger a massive amount of Angular's change detectors.

We only changed to use the non-patched version of requestAnimationFrame, but it's possible this happens with other timer-related functions as well. From looking at [the minified] Angular source:

Image

I don't know Angular at all, but we may want to try to use the unpatched versions of these functions:

  • setTimeout
  • clearTimeout
  • setInterval
  • clearInterval
  • setImmediate
  • clearImmediate

Ideally, this would happen only in the angular package as to not bloat the rest of the packages.

Closes https://github.com/getsentry/team-replay/issues/424 (Internal ticket)

@billyvg billyvg added the Package: replay Issues related to the Sentry Replay SDK label Apr 17, 2024
@mydea
Copy link
Member

mydea commented Apr 18, 2024

Hmm, that kind of sucks 😅

So, if this is the same issue as with requestAnimationFrame, it is only relevant to do this in rrweb functions that are related to capturing etc, I guess. So we don't necessarily have to wrap all invocations, only the ones in "hot paths" I guess 🤔

setImmediate / clearImmediate are not used in rrweb, so we can disregard these for now.

setInterval / clearInterval is also not used in any package we use, so probably also fine to ignore.

setTimeout / clearTimeout are used quite a lot 😬 So maybe we can just use these... I can give it a try, maybe we can make the implementation generic so the bundle size impact is negligible 🤔

mydea added a commit to getsentry/rrweb that referenced this issue Apr 19, 2024
…t` (#176)

Let's see if that helps with Angular performance some more...!

Closes getsentry/sentry-javascript#11661
(hopefully...)
billyvg pushed a commit to getsentry/rrweb that referenced this issue Apr 26, 2024
…t` (#176)

Let's see if that helps with Angular performance some more...!

Closes getsentry/sentry-javascript#11661
(hopefully...)
@billyvg billyvg reopened this Apr 30, 2024
@billyvg
Copy link
Member Author

billyvg commented Apr 30, 2024

Re-opening this as it was auto-closed from a PR that only partially addresses this issue. We have some setTimeout usage in our Replay SDK that needs to also call the unpatched version of setTimeout as to not conflict with Angular's change detection.

@natebundy
Copy link

@billyvg @mydea Is it possible for you all to run this code outside of the Angular zone to avoid this issue entirely? "Third-party libraries commonly trigger unnecessary change detection cycles because they weren't authored with Zone.js in mind. Avoid these extra cycles by calling library APIs outside the Angular zone"

See the Angular docs:
https://angular.io/guide/change-detection-zone-pollution
https://angular.io/api/core/NgZone#runoutsideangular

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 May 7, 2024
@natebundy
Copy link

natebundy commented May 8, 2024

Thinking about this more, since Sentry is initializing outside Angular, you could possibly look for the global Zone object existing and call things in Zone.root.run(() => {...}) to avoid change detection cycles. https://github.com/angular/angular/blob/7287fefe7a750a1ae584d9bb9e0378aa047b5d95/packages/zone.js/lib/zone-impl.ts#L129C1-L133C40

However, since I'm assuming Sentry is generally initializing before Zone.js, that may not be any better than keeping around copies of non-monkey patched functions, unless you have a single or few entry points that you could call within Zone.root.run(() => {...}).

Another (not great) option would be to wrap Sentry in its own zone from Zone.js during initialization. As long as the zone isn't "angular" (e.g. a new "sentry" zone) it won't mess with change detection. It seems like Angular needs a better way to opt-out of zones for JavaScript libraries.

(The app I'm working on has a performance degradation in a virtual scrolling table with Sentry enabled due to all of the extra change detection cycles, so I'm invested!)

@billyvg
Copy link
Member Author

billyvg commented May 22, 2024

@natebundy we just released version 8.3.0 that includes changes to use an unpatched setTimeout, would you be able to see if it helps with your virtual table?

Unfortunately I don't think our SDKs are flexible enough to run certain code paths outside of Angular's zones.

@natebundy
Copy link

Thanks, I'll give it a try!

I just found out Angular is experimenting with zoneless change detection, so hopefully at some point in the very far future this will stop being an issue.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 May 22, 2024
@AbhiPrasad AbhiPrasad added the Package: angular Issues related to the Sentry Angular SDK label Aug 2, 2024
@getsantry getsantry bot moved this to Waiting for: Community in GitHub Issues with 👀 3 Sep 13, 2024
@getsantry getsantry bot added the Stale label Oct 5, 2024
@getsantry
Copy link

getsantry bot commented Oct 5, 2024

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you remove the label Waiting for: Community, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@getsantry getsantry bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: angular Issues related to the Sentry Angular SDK Package: replay Issues related to the Sentry Replay SDK Stale
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants