Skip to content

perf(replay): Defer ReplayIntegration.start() off the main thread#5474

Draft
runningcode wants to merge 2 commits into
mainfrom
no/defer-replay-start
Draft

perf(replay): Defer ReplayIntegration.start() off the main thread#5474
runningcode wants to merge 2 commits into
mainfrom
no/defer-replay-start

Conversation

@runningcode
Copy link
Copy Markdown
Contributor

📜 Description

Move the expensive work in ReplayIntegration.start() (capture strategy creation, recorder start, root view listener registration) to the executor service background thread. The lightweight state checks and lifecycle transition (lifecycle.currentState = STARTED) remain synchronous so that isRecording() returns the correct value immediately.

registerRootViewListeners() is posted to the main looper handler since it triggers View operations (e.g. addOnLayoutChangeListener) via OnRootViewsChangedListener.

💡 Motivation and Context

Perfetto traces on a Pixel 3 show ReplayIntegration.start() taking ~16ms on the main thread during SentryAndroid.init(). Since start() is called after init is complete and doesn't need to block the caller, the heavy work can be deferred to a background thread.

💚 How did you test it?

  • Ran :sentry-android-core:testDebugUnitTest — all 22 tests pass
  • Ran :sentry-android-replay:testDebugUnitTest — all 209 tests pass (including ReplayIntegrationTest, ReplayIntegrationWithRecorderTest, ReplaySmokeTest)

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

runningcode and others added 2 commits May 28, 2026 11:28
Move the expensive work in ReplayIntegration.start() (capture strategy
creation, recorder start, root view listener registration) to the
executor service, keeping only the lightweight state checks and
lifecycle transition synchronous. This saves ~16ms on the main thread
during SentryAndroid.init() (measured on Pixel 3).

The registerRootViewListeners() call is posted to the main looper
handler since it triggers View operations via OnRootViewsChangedListener.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment thread CHANGELOG.md

### Improvements

- Improve SDK init performance by deferring `ReplayIntegration.start()` off the main thread ([#5474](https://github.com/getsentry/sentry-java/pull/5474))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 🚫 The changelog entry seems to be part of an already released section ## 8.43.0.
    Consider moving the entry to the ## Unreleased section, please.

@sentry
Copy link
Copy Markdown

sentry Bot commented May 28, 2026

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.43.0 (1) release

⚙️ sentry-android Build Distribution Settings

@github-actions
Copy link
Copy Markdown
Contributor

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 320.43 ms 327.96 ms 7.52 ms
Size 0 B 0 B 0 B

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
a5ab36f 316.83 ms 394.54 ms 77.71 ms
e63ad34 336.55 ms 406.79 ms 70.23 ms
ad8da22 362.98 ms 453.94 ms 90.96 ms
70118e9 380.00 ms 475.72 ms 95.72 ms
2124a46 319.19 ms 415.04 ms 95.85 ms
b3d8889 371.84 ms 447.49 ms 75.65 ms
cf708bd 434.73 ms 502.96 ms 68.22 ms
ab8a72d 316.24 ms 356.38 ms 40.14 ms
d5a29b6 298.62 ms 391.78 ms 93.16 ms
d15471f 379.40 ms 470.76 ms 91.36 ms

App size

Revision Plain With Sentry Diff
a5ab36f 1.58 MiB 2.12 MiB 555.26 KiB
e63ad34 0 B 0 B 0 B
ad8da22 1.58 MiB 2.29 MiB 719.83 KiB
70118e9 1.58 MiB 2.29 MiB 719.84 KiB
2124a46 1.58 MiB 2.12 MiB 551.51 KiB
b3d8889 1.58 MiB 2.10 MiB 535.07 KiB
cf708bd 1.58 MiB 2.11 MiB 539.71 KiB
ab8a72d 1.58 MiB 2.12 MiB 551.55 KiB
d5a29b6 1.58 MiB 2.12 MiB 549.37 KiB
d15471f 1.58 MiB 2.13 MiB 559.54 KiB

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.

1 participant