Repository navigation
perf(android): avoid animation stalls in capture and viewport reads #3790
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
Merged
+338
−60
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
de41f12
perf(android): batch accessibility child reads between animation frames
thymikee 8c7575c
fix(android): keep batched reads compatible with API 23
thymikee ed6b08e
perf(android): avoid idle waits for viewport geometry and simplify bo…
thymikee 37318ee
test(android): observe blocked batch cleanup before releasing reads
thymikee 7a74fb8
fix(android): preserve keyboard settling in shared read preparation
thymikee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
99 changes: 99 additions & 0 deletions
99
...helper/src/main/java/com/callstack/agentdevice/snapshothelper/AccessibilityReadBatch.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package com.callstack.agentdevice.snapshothelper; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.FutureTask; | ||
|
|
||
| /** | ||
| * Reads a bounded group of children concurrently and retains their traversal order. | ||
| * | ||
| * <p>Accessibility cache misses are served on the app's UI thread. Queuing siblings together lets | ||
| * the app serve multiple reads between animation frames. Workers are reused because Android keeps | ||
| * an AccessibilityInteractionClient for each calling thread for the lifetime of the process. | ||
| * Closing waits for every read before releasing nodes, including unread results after truncation | ||
| * or failure, so no worker outlives the parent node it is reading. | ||
| */ | ||
| final class AccessibilityReadBatch<T> implements AutoCloseable { | ||
| interface Reader<T> { | ||
| T read(int index); | ||
| } | ||
|
|
||
| interface Releaser<T> { | ||
| void release(T value); | ||
| } | ||
|
|
||
| static final int MAX_READS = 8; | ||
| private static final ExecutorService READERS = | ||
| Executors.newFixedThreadPool( | ||
| MAX_READS, | ||
| task -> { | ||
| Thread thread = new Thread(task, "agent-device-accessibility-read"); | ||
| thread.setDaemon(true); | ||
| return thread; | ||
| }); | ||
|
|
||
| private final List<FutureTask<T>> reads = new ArrayList<>(); | ||
| private final Releaser<T> release; | ||
|
|
||
| AccessibilityReadBatch(int start, int count, Reader<T> read, Releaser<T> release) { | ||
| if (count < 1 || count > MAX_READS) { | ||
| throw new IllegalArgumentException("Accessibility read batch must contain 1 to 8 reads"); | ||
| } | ||
| this.release = release; | ||
| for (int offset = 0; offset < count; offset += 1) { | ||
| int index = start + offset; | ||
| FutureTask<T> task = new FutureTask<>(() -> read.read(index)); | ||
| reads.add(task); | ||
| READERS.execute(task); | ||
| } | ||
| } | ||
|
|
||
| T get(int offset) { | ||
| return await(reads.get(offset)); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| for (FutureTask<T> read : reads) { | ||
| T node; | ||
| try { | ||
| node = await(read); | ||
| } catch (RuntimeException | Error ignored) { | ||
| // get() propagates read failures. Other reads must finish before the parent is released. | ||
| continue; | ||
| } | ||
| if (node != null) { | ||
| release.release(node); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static <T> T await(FutureTask<T> read) { | ||
| boolean interrupted = false; | ||
| try { | ||
| while (true) { | ||
| try { | ||
| return read.get(); | ||
| } catch (InterruptedException error) { | ||
| interrupted = true; | ||
| } catch (ExecutionException error) { | ||
| Throwable cause = error.getCause(); | ||
| if (cause instanceof RuntimeException) { | ||
| throw (RuntimeException) cause; | ||
| } | ||
| if (cause instanceof Error) { | ||
| throw (Error) cause; | ||
| } | ||
| throw new IllegalStateException("Android accessibility child read failed", cause); | ||
| } | ||
| } | ||
| } finally { | ||
| if (interrupted) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| } | ||
| } | ||
| } |
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: This says viewport uses the same idle wait as snapshots, but viewport reads always call
prepare(automation)with the 100 ms/500 ms defaults while snapshots honor the supplied wait arguments. Describe the viewport values as fixed defaults to avoid implying that-e waitForIdleTimeoutMsor-e waitForIdleQuietMsconfigures both modes.Prompt for AI agents