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

[QA] Offload System.loadLibrary call to background thread #3670

Merged
merged 5 commits into from
Oct 14, 2024

Conversation

markushi
Copy link
Member

@markushi markushi commented Aug 30, 2024

📜 Description

Perform System.loadLibrary() on a background thread.

💡 Motivation and Context

Calling System.loadLibrary() causes StrictMode violations, as it performs disk reads, but any JNI calls like initSentryNative needs the libraries to be loaded beforehand.

Initializing the whole NDK off the main thread would be an option, but it has two main disadvantages:

  • there's no guarantee that the NDK is initialized after SentryAndroid.init(), thus we could miss early startup crashes
  • all NDK communication (setUser, setTag) needs to be done on a background thread as well

So the trick is to perform System.loadLibrary() as early as possible on a background thread, but still perform NDK init synchronous on the main thread. In my testing this works well, as the SDK init in-between takes longer than the .loadLibrary() calls, making the loadLibraryLatch.await(2000, TimeUnit.MILLISECONDS) return immediatelly.

💚 How did you test it?

Just manually, haven't found a better way yet.

📝 Checklist

  • I reviewed the submitted code.
  • 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.
  • 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

Copy link
Contributor

github-actions bot commented Aug 30, 2024

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 5c35128

Copy link
Contributor

github-actions bot commented Aug 30, 2024

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 396.45 ms 443.06 ms 46.61 ms
Size 1.70 MiB 2.35 MiB 667.99 KiB

Previous results on branch: feat/async-ndk-loadlib

Startup times

Revision Plain With Sentry Diff
f813acb 422.98 ms 461.52 ms 38.54 ms

App size

Revision Plain With Sentry Diff
f813acb 1.70 MiB 2.35 MiB 667.56 KiB

@markushi
Copy link
Member Author

markushi commented Sep 3, 2024

Comparing the performance metrics with other PRs, we can see a nice 35-45% improvement of the startup time 🚀

@kahest
Copy link
Member

kahest commented Sep 3, 2024

@markushi sounds very promising 👍 any creative ways to test this end-to-end? basically (how) can we guarantee this doesn't break any part of startup/initialization?

@kahest
Copy link
Member

kahest commented Sep 3, 2024

for reference: this is related to/fixes (partially) #3585 and #3654

Copy link
Member

@stefanosiano stefanosiano left a comment

Choose a reason for hiding this comment

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

Looks good!
Not sure how/if we can test it, but "logically" it works

Copy link
Member

@romtsn romtsn left a comment

Choose a reason for hiding this comment

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

I think we wanted to make it part of the next major (8.x.x) - do we still wanna do that?

Aside, this looks good, and I think eventually we could offload the native init to the background thread as well, because we wanted to move setTag, setBreadcrumbs to background anyway.

@markushi markushi added this to the 8.0.0 milestone Sep 4, 2024
@markushi markushi changed the base branch from main to 8.x.x September 4, 2024 13:17
@markushi markushi changed the base branch from 8.x.x to main October 4, 2024 06:38
@markushi markushi removed this from the 8.0.0 milestone Oct 4, 2024
@markushi markushi changed the title Offload System.loadLibrary call to background thread [QA] Offload System.loadLibrary call to background thread Oct 4, 2024
@markushi markushi merged commit 6259a9f into main Oct 14, 2024
26 checks passed
@markushi markushi deleted the feat/async-ndk-loadlib branch October 14, 2024 13:53
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.

4 participants