fix(init): Reduce allocations and bytecode instructions during Sentry.init#5135
fix(init): Reduce allocations and bytecode instructions during Sentry.init#5135
Conversation
Semver Impact of This PR🟢 Patch (bug fixes) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨
Bug Fixes 🐛
Internal Changes 🔧Deps
🤖 This preview updates automatically when you update the PR. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| e59e22a | 329.74 ms | 383.31 ms | 53.57 ms |
| fc5ccaf | 270.49 ms | 363.90 ms | 93.41 ms |
| d15471f | 361.89 ms | 378.07 ms | 16.18 ms |
| d15471f | 322.58 ms | 396.08 ms | 73.50 ms |
| abfcc92 | 304.04 ms | 370.33 ms | 66.29 ms |
| 91bb874 | 310.68 ms | 359.24 ms | 48.56 ms |
| ab8a72d | 316.24 ms | 356.38 ms | 40.14 ms |
| d15471f | 286.65 ms | 314.68 ms | 28.03 ms |
| 6727e14 | 337.22 ms | 373.94 ms | 36.71 ms |
| f064536 | 329.00 ms | 395.62 ms | 66.62 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| e59e22a | 1.58 MiB | 2.20 MiB | 635.34 KiB |
| fc5ccaf | 1.58 MiB | 2.13 MiB | 557.54 KiB |
| d15471f | 1.58 MiB | 2.13 MiB | 559.54 KiB |
| d15471f | 1.58 MiB | 2.13 MiB | 559.54 KiB |
| abfcc92 | 1.58 MiB | 2.13 MiB | 557.31 KiB |
| 91bb874 | 1.58 MiB | 2.13 MiB | 559.07 KiB |
| ab8a72d | 1.58 MiB | 2.12 MiB | 551.55 KiB |
| d15471f | 1.58 MiB | 2.13 MiB | 559.54 KiB |
| 6727e14 | 1.58 MiB | 2.28 MiB | 718.64 KiB |
| f064536 | 1.58 MiB | 2.20 MiB | 633.90 KiB |
|
|
||
| readDefaultOptionValues(options, finalContext, buildInfoProvider); | ||
| AppState.getInstance().registerLifecycleObserver(options); | ||
| options.activate(); |
There was a problem hiding this comment.
l this still as a small potential of leaking a thread if we never end up using the options, e.g. due to lower priority and SDK already being initialized. We could invoke InitUtil.shouldInit here to check in case this becomes a problem.
There was a problem hiding this comment.
can't really do that since sentry-android-core does not depend on sentry :( I guess will keep it like this, probably it's a rare case
Use AtomicBoolean to ensure activate() only loads SpotlightIntegration once, so users can remove it in their configuration callback without it being re-added by the second activate() call from Sentry.init(). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
54ef748 to
53c1348
Compare
Reduces unnecessary allocations and bytecode instructions during
Sentry.initto improve startup performance, particularly on Android.Key changes:
options.activate()at the end ofAndroidOptionsInitializer.loadDefaultAndMetadataOptions()so the executor service is available sooner — before integrations likeAndroidConnectionStatusProvidertry to use itnew Thread(() -> getOrLoadModules()).start()with the shared executor service, avoiding an extra thread allocation during initactivate(): Moves the reflective class loading from theSentryOptionsconstructor intoactivate(), so it only runs when options are actually usedObjects.requireNonNullchecks: Several internal constructors (MainEventProcessor,DuplicateEventDetectionEventProcessor,SentryExceptionFactory,SentryThreadFactory,UncaughtExceptionHandlerIntegration) usedObjects.requireNonNullon parameters that are always non-null internal calls. Removing these saves bytecode and allocations on every initMainEventProcessorinternal constructor: The 3-arg constructor was dead code