Skip to content

Add init(Context, ExternalSoMapping, int flags) - #160

Open
tomekzaw wants to merge 1 commit into
meta:mainfrom
tomekzaw:init-flags-with-external-mapping
Open

tomekzaw wants to merge 1 commit into
meta:mainfrom
tomekzaw:init-flags-with-external-mapping

Conversation

@tomekzaw

@tomekzaw tomekzaw commented Nov 17, 2026 •

Copy link
Copy Markdown

Summary

Apps that use SoMerging through an ExternalSoMapping (React Native in OSS) can only call init(Context, ExternalSoMapping), which always passes flags = 0, so none of the SOLOADER_* flags are available to them. Calling init(Context, int flags) first does not work either: without the mapping, initEnableConfig honours the com.facebook.soloader.enabled=false meta-data that SoLoader's own manifest contributes, SoLoader disables itself, and the merged libraries then fail to load (dlopen failed: library "libreact_featureflagsjni.so" not found).

This adds init(Context, @Nullable ExternalSoMapping, int flags); init(Context, ExternalSoMapping) delegates to it with 0. The parameter order avoids an ambiguity with init(Context, int, @Nullable SoFileLoader) for callers passing null.

Motivation

With SOLOADER_ENABLE_SYSTEMLOAD_WRAPPER_SOSOURCE, each merged library is loaded with one System.loadLibrary call and the dynamic linker resolves its DT_NEEDED dependencies. Today DirectApkSoSource reads the dependency list in Java (SoLoader[NativeDeps]: Falling back to custom ELF parsing when loading libreactnative.so, this can be slow) and loads libc++_shared, libjsi, libfbjni, libhermesvm, … one by one through SoLoader. In a React Native app this is 20–30 ms of Application.onCreate on the main thread of a Galaxy A16 (atrace: SoLoader.loadLibrary[react_featureflagsjni] 20.4 ms, of which ELF parsing + the four dependency loads ≈ 15 ms; hermesinstancejni 8.6 ms).

React Native can get most of that today with SoLoader.prependSoSource(new SystemLoadWrapperSoSource()) after init (react/react-native#60066). Measured with that: RNTester release, arm64, real phones, cold starts, verify dex state, 18 interleaved launches per arm, medians with bootstrap 95 % CIs of the median difference. TTFD = time to full display (reportFullyDrawn() on the first frame with React content), TTID = time to initial display (am start -W).

Phone Cold TTFD before → after Δ TTFD (CI95) Δ TTID (CI95)
Galaxy A16 (Helio G99, Android 16) 340 → 322 ms −18 ms, −5.4 % [−30, −10] −22 ms [−31, −8]
Galaxy A57 5G (Exynos, Android 17) 200 → 191 ms −10 ms, −4.7 % [−16, −3] −14 ms [−18, −6]
moto g57 power (Dimensity, Android 16) 266 → 258 ms −8 ms, −2.8 % [−14, +1] −12 ms [−19, −2]
Pixel 9a (Tensor G4, Android 16) 150 → 142 ms −7 ms, −4.7 % [−11, 0] −10 ms [−14, −6]
OPPO CPH2269 (Helio G35, Android 11) 1122 → 1110 ms −12 ms, −1.1 % [−58, +27] (noise floor ≈ ±50 ms) −90 ms [−103, −55]

The prepend approach still constructs and prepares the DirectApk/Application/Backup sources it will never consult; with this overload React Native can pass the flag at init and skip them.

Test plan

  • ./gradlew formatCheck :soloader:assembleDebug passes.
  • No behaviour change for existing callers: init(Context, ExternalSoMapping) → init(context, mapping, 0) is the same sequence as before (mapping set under the class lock, then init(context, flags)).
  • Exercised the wrapper path end to end from React Native (RNTester release, arm64, Galaxy A16 / Galaxy A57 / moto g57 power / OPPO CPH2269 / Android 16 emulator): libraries load, JNI_OnLoad of the merged libraries runs through ExternalSoMapping.invokeJniOnload, and the NativeDeps fallback lines disappear from logcat.

🤖 Generated with Claude Code

Apps that use SoMerging through an ExternalSoMapping (React Native in OSS)
can only call init(Context, ExternalSoMapping), which always passes flags = 0,
so none of the SOLOADER_* flags are available to them. This adds an overload
that takes the flags as well; the existing one delegates to it.

The motivating flag is SOLOADER_ENABLE_SYSTEMLOAD_WRAPPER_SOSOURCE: with it,
each merged library is loaded with one System.loadLibrary call and the dynamic
linker resolves its dependencies, instead of DirectApkSoSource reading the ELF
DT_NEEDED entries in Java and loading each dependency through SoLoader. On a
Galaxy A16 that is 20-30 ms on the main thread during Application.onCreate of
a React Native app.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant