perf(android): Avoid exception-driven control flow in getResourceId#5631
Open
runningcode wants to merge 2 commits into
Open
perf(android): Avoid exception-driven control flow in getResourceId#5631runningcode wants to merge 2 commits into
runningcode wants to merge 2 commits into
Conversation
📲 Install BuildsAndroid
|
0xadam-brown
approved these changes
Jun 26, 2026
0xadam-brown
left a comment
Member
There was a problem hiding this comment.
Always a win! One optional nit if you want it 💯
| * @param view - the view whose id is being retrieved | ||
| * @return human-readable view id, or {@code null} if it cannot be resolved | ||
| */ | ||
| public static @Nullable String resolveResourceId(final @NotNull View view) { |
Member
There was a problem hiding this comment.
l: Thoughts about getResourceIdOrNull() to parallel getResourceId()?
Up to you, of course...
Contributor
Author
There was a problem hiding this comment.
good idea. will add!
Contributor
Author
There was a problem hiding this comment.
on second though I also removed the other method that throws since it is unused, internal and not part of our public api. gonna ask for a re-review
eeff1d6 to
9f7943e
Compare
Contributor
Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 4e3e79d | 369.55 ms | 418.39 ms | 48.83 ms |
| 22ff2c7 | 306.60 ms | 336.65 ms | 30.05 ms |
| 092f017 | 353.13 ms | 433.84 ms | 80.71 ms |
| 0eaac1e | 322.53 ms | 389.31 ms | 66.78 ms |
| d15471f | 322.58 ms | 396.08 ms | 73.50 ms |
| fc5ccaf | 322.49 ms | 405.25 ms | 82.76 ms |
| e2dce0b | 315.85 ms | 369.20 ms | 53.35 ms |
| 22f4345 | 314.79 ms | 375.02 ms | 60.23 ms |
| 9770665 | 315.64 ms | 378.00 ms | 62.36 ms |
| 7314dbe | 437.83 ms | 505.64 ms | 67.81 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 4e3e79d | 0 B | 0 B | 0 B |
| 22ff2c7 | 0 B | 0 B | 0 B |
| 092f017 | 0 B | 0 B | 0 B |
| 0eaac1e | 1.58 MiB | 2.19 MiB | 619.17 KiB |
| d15471f | 1.58 MiB | 2.13 MiB | 559.54 KiB |
| fc5ccaf | 1.58 MiB | 2.13 MiB | 557.54 KiB |
| e2dce0b | 0 B | 0 B | 0 B |
| 22f4345 | 1.58 MiB | 2.29 MiB | 719.83 KiB |
| 9770665 | 0 B | 0 B | 0 B |
| 7314dbe | 1.58 MiB | 2.10 MiB | 533.45 KiB |
ViewUtils.getResourceId threw Resources.NotFoundException for views with no id or a generated id, and callers caught and discarded it. During a view-hierarchy snapshot and on every gesture this ran per view, so in Compose-heavy apps where most views have generated ids the SDK constructed an exception (and a native stack trace fill) per view on the main thread. Add a non-throwing resolveResourceId that returns null for unresolved ids and route the hot callers through it. The public getResourceId remains as a throwing wrapper for backward compatibility. Behavior (emitted identifiers and fallbacks) is unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
9f7943e to
e484785
Compare
runningcode
commented
Jun 26, 2026
| * @throws Resources.NotFoundException in case the view id was not found | ||
| * @return human-readable view id, or {@code null} if it cannot be resolved | ||
| */ | ||
| public static String getResourceId(final @NotNull View view) throws Resources.NotFoundException { |
Contributor
Author
There was a problem hiding this comment.
this old method signature was removed so it is a breaking change but not part of the public api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves JAVA-586
📜 Description
ViewUtils.getResourceIdused exception-driven control flow: it threwResources.NotFoundExceptionfor views withView.NO_IDor a generated id, and every caller caught and discarded it. This ran per view during view-hierarchy snapshots and gesture target resolution, so in Compose-heavy apps — where most views have generated ids — the SDK constructed an exception (and a nativefillInStackTracestack walk) for nearly every view, on the main thread.This adds a non-throwing
ViewUtils.resolveResourceId(View)that returnsnullfor unresolved ids, and routes the hot callers (ViewHierarchyEventProcessor.viewToNode,AndroidViewGestureTargetLocator.createUiElement,getResourceIdWithFallback) through it. The publicgetResourceId(...) throws Resources.NotFoundExceptionstays as a thin wrapper for backward compatibility. Emitted identifiers and fallbacks are unchanged.💡 Motivation and Context
Found while analyzing a customer-provided Perfetto trace: in a single view-hierarchy snapshot, 24 of 72
getResourceIdcalls threwResources.NotFoundExceptionon the main thread — pure overhead with no functional result.💚 How did you test it?
Unit tests in
ViewUtilsTestcover the newresolveResourceId(generated id,NO_ID, resource-not-found, and success) plus the existinggetResourceId/fallback behavior;ViewHierarchyEventProcessorTestandAndroidViewGestureTargetLocatorTestpass unchanged.On-device A/B on a Pixel 3 (Android 12) via ART method tracing, analyzed in Perfetto
trace_processor, over 2048 calls onNO_IDviews:getResourceId)resolveResourceId)Resources$NotFoundException.<init>Throwable.fillInStackTraceExactly one exception (+ native stack fill) per unresolved view is removed; ~60% fewer executed methods and ~4.7× cheaper per unresolved view (the absolute ns is inflated by tracing; the call counts and ratio are the reliable signal).
📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps
Follow-ups identified in the same trace but intentionally out of scope here: per-touch
LinkedList→ArrayDequeinViewUtils.findTarget, and background JSON serialization cost.