Skip to content

perf(connectivity): Cache network capabilities and status to reduce IPC calls #4560

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

romtsn
Copy link
Member

@romtsn romtsn commented Jul 16, 2025

📜 Description

  • Register network callback on a bg thread upon AndroidConnectionStatusProvider instantiation
  • Cache latest available NetworkCapabilities for 2 minutes
  • Use the capabilities to determine the current network status and type
    • If it's not available (cache expired), we'd fallback to retrieving it proactively and caching it
    • pre-API-23 we'd be always querying it, but it's a very small number of devices
  • onLost/onUnavailable would immediately send DISCONNECTED now as opposed to retrieving connectionStatus again
  • Implement a isSignificantChange check, because onNetworkCapabilitiesChanged can be called many times due to bandwidth change/etc. and we're only interested in transport/connectivity changes
  • Unregister the network callback when the SDK is closing
  • Cache ConnectivityManager instance too (it should be safe to cache once per process as it's not dependant on any particular Context)

Ran a test with perfetto to count the number of binder calls, and these are the results:

  • Send app to background
  • Freeze it
  • Turn wifi on and off 5 times,
  • Unfreeze it back

Before (83 binder calls from SentryExecService)

image

After (5 binder calls from SentryExecService)

image

💡 Motivation and Context

Fixes #4200

💚 How did you test it?

Manually + automated

📝 Checklist

  • 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

  • Refactor NetworkBreadcrumbsIntegration so we register only one callback in AndroidConnectionStatusProvider and multiplex it
  • Register/unregister connectivity callback in foreground/background similar to SystemEvents integration

}
}
lastCacheUpdateTime = timeProvider.getCurrentTimeMillis();

options
Copy link
Member Author

Choose a reason for hiding this comment

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

could drop this log call, but I thought it might be very useful to investigate potential issues. Under the hood getConnectionStatus and getConnectionType are very lightweight for a cache NetworkCapabilities instance, they just do bitmask operations to get the transport and capability

Copy link
Contributor

github-actions bot commented Jul 16, 2025

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 383.29 ms 403.25 ms 19.96 ms
Size 1.58 MiB 2.09 MiB 520.53 KiB

Previous results on branch: rz/perf/less-ipc

Startup times

Revision Plain With Sentry Diff
91ac06a 386.02 ms 407.53 ms 21.51 ms

App size

Revision Plain With Sentry Diff
91ac06a 1.58 MiB 2.09 MiB 520.52 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.

Cache current network connection state
1 participant