Avoid allocating empty lists

Summary: Changelog: [Internal] Optimize Fabric memory allocations on Android

Reviewed By: genkikondo

Differential Revision: D36098912

fbshipit-source-id: 8d34a652db019865a8306bc070a08f5ddfef96bf
This commit is contained in:
Pieter De Baets
2022-05-04 04:22:40 -07:00
committed by Facebook GitHub Bot
parent b9bb30b1ad
commit 5de0f145ec
2 changed files with 14 additions and 7 deletions
@@ -133,9 +133,7 @@ public class NativeAnimatedModule extends NativeAnimatedModuleSpec
@UiThread
void executeBatch(long maxBatchNumber, NativeAnimatedNodesManager nodesManager) {
List<UIThreadOperation> operations;
if (mSynchronizedAccess) {
synchronized (this) {
operations = drainQueueIntoList(maxBatchNumber);
@@ -143,13 +141,19 @@ public class NativeAnimatedModule extends NativeAnimatedModuleSpec
} else {
operations = drainQueueIntoList(maxBatchNumber);
}
for (UIThreadOperation operation : operations) {
operation.execute(nodesManager);
if (operations != null) {
for (UIThreadOperation operation : operations) {
operation.execute(nodesManager);
}
}
}
@UiThread
private List<UIThreadOperation> drainQueueIntoList(long maxBatchNumber) {
private @Nullable List<UIThreadOperation> drainQueueIntoList(long maxBatchNumber) {
if (mQueue.isEmpty() && mPeekedOperation == null) {
return null;
}
List<UIThreadOperation> operations = new ArrayList<>();
while (true) {
// Due to a race condition, we manually "carry-over" a polled item from previous batch
@@ -356,13 +356,16 @@ public class MountItemDispatcher {
@Nullable
private static <E extends MountItem> List<E> drainConcurrentItemQueue(
ConcurrentLinkedQueue<E> queue) {
if (queue.isEmpty()) {
return null;
}
List<E> result = new ArrayList<>();
while (!queue.isEmpty()) {
do {
E item = queue.poll();
if (item != null) {
result.add(item);
}
}
} while (!queue.isEmpty());
if (result.size() == 0) {
return null;
}