Look up Android image resources without throwing an exception (#59456)

Summary:
In Release builds on Android, an image from the bundle (`<Image source={require('./icon.png')} />`) is a drawable resource, and its source is the resource name, for example `assets_icon`. `ImageSource` turns that name into a URI with `ResourceDrawableIdHelper.getResourceDrawableId`. The same lookup runs for `defaultSource` and `loadingIndicatorSource`, `Image.getSize` of a bundled image, and TextInput's `inlineImageLeft`. Images loaded from Metro or from a bundle outside the APK have a URI scheme and don't use it.

**Current behavior.** `getResourceDrawableId` first tries the name as a numeric resource id with `toInt()` and catches the `NumberFormatException`. Resource names are not numbers, so every such image source throws an exception and fills in a stack trace as deep as the mount call stack. This happens before the cache of looked-up names, so names that were already looked up throw too. On a Samsung Galaxy A14 the lookup takes about 0.24 ms for each image source, almost all of it in the exception.

**With this change.** The name is parsed with `toIntOrNull()`, which accepts the same numbers as `toInt()` and returns `null` instead of throwing. Numeric names still return the same id, and other names are still looked up as drawables, so every name gives the same result as before.

## Changelog:

[ANDROID] [CHANGED] - Images from the app bundle look up their resource without throwing an exception

Pull Request resolved: https://github.com/react/react-native/pull/59456

Test Plan:
- The pinned ktfmt (`--dry-run --set-exit-if-changed`) passes on both files.
- Added `ResourceDrawableIdHelperTest` (5 tests: empty name, numeric names, a name looked up as a drawable after normalization, a number too big for an `Int`, an unknown name). It passes with this change and with the old code, so the results are the same.
- **Device:** Samsung Galaxy A14 (SM-A145R, Android 15), RNTester built with Gradle in Release (`assembleRelease`, arm64-v8a) and compiled with `cmd package compile -m speed`. The scenario is a local Playground screen, not part of this PR: a long feed with avatars and photos from the bundle that scrolls by itself.
- **A/B setup:** a single APK with a local switch (an `adb shell setprop`, not part of this PR) that runs either the old `toInt()` with `try`/`catch` or `toIntOrNull()`, and times every call. Launches alternate A B B A A B B A, 4 per arm. Every launch made 1,300 lookups.

| | before | this PR | change |
|---|--:|--:|--:|
| Time per `getResourceDrawableId` | 239–243 µs | 16.4–17.3 µs | −93% |

- **Perfetto** (30 s of the same feed): `ReactImageManager.setSource` went from 3.5% to 1.6% of the main thread's samples, and the samples in exception handling under it from 237 to 0.
- **Rendering:** screenshots of the feed in both arms are byte-identical.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Reviewed By: javache

Differential Revision: D127722121

Pulled By: Abbondanzo

fbshipit-source-id: 3fadd8cfe62f335ffcfd917f903e51ef940082c7
This commit is contained in:
Gregory Moskaliuk authored and meta-codesync[bot] committed 2026-10-27 07:21:41 -07:00
1 parent ed15270c6c
commit 98291bcb99
2 files changed
+79 -5

No files matched your search

@@ -31,11 +31,10 @@ public object ResourceDrawableIdHelper {
}
val normalizedName = name.lowercase().replace("-", "_")
// name could be a resource id.
try {
return normalizedName.toInt()
} catch (e: NumberFormatException) {
// Do nothing.
// name could be a resource id. Most names are not numbers, so this must not throw: an
// exception would build a stack trace for every image source.
normalizedName.toIntOrNull()?.let {
return it
}
synchronized(this) {
@@ -0,0 +1,75 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.views.imagehelper
import android.content.Context
import org.assertj.core.api.Assertions.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
@RunWith(RobolectricTestRunner::class)
class ResourceDrawableIdHelperTest {
private lateinit var context: Context
@Before
fun setUp() {
context = RuntimeEnvironment.getApplication()
ResourceDrawableIdHelper.clear()
}
@After
fun tearDown() {
ResourceDrawableIdHelper.clear()
}
@Test
fun returnsZeroForMissingName() {
assertThat(ResourceDrawableIdHelper.getResourceDrawableId(context, null)).isEqualTo(0)
assertThat(ResourceDrawableIdHelper.getResourceDrawableId(context, "")).isEqualTo(0)
}
@Test
fun returnsNumericNameAsId() {
assertThat(ResourceDrawableIdHelper.getResourceDrawableId(context, "123")).isEqualTo(123)
assertThat(ResourceDrawableIdHelper.getResourceDrawableId(context, "+42")).isEqualTo(42)
}
@Test
fun looksUpNonNumericNameAsDrawable() {
val expected =
context.resources.getIdentifier(
"redbox_top_border_background",
"drawable",
context.packageName,
)
assertThat(expected).isGreaterThan(0)
assertThat(
ResourceDrawableIdHelper.getResourceDrawableId(
context,
"Redbox-Top-Border-Background",
)
)
.isEqualTo(expected)
}
@Test
fun looksUpNumberThatDoesNotFitInAnIntAsDrawable() {
assertThat(ResourceDrawableIdHelper.getResourceDrawableId(context, "99999999999")).isEqualTo(0)
}
@Test
fun returnsZeroForUnknownDrawable() {
assertThat(ResourceDrawableIdHelper.getResourceDrawableId(context, "unknown_image"))
.isEqualTo(0)
}
}