From 5de0f145eca36d3263cb8a9928add278cdfffa2b Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Wed, 4 May 2022 04:22:40 -0700 Subject: [PATCH] Avoid allocating empty lists Summary: Changelog: [Internal] Optimize Fabric memory allocations on Android Reviewed By: genkikondo Differential Revision: D36098912 fbshipit-source-id: 8d34a652db019865a8306bc070a08f5ddfef96bf --- .../react/animated/NativeAnimatedModule.java | 14 +++++++++----- .../react/fabric/mounting/MountItemDispatcher.java | 7 +++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedModule.java b/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedModule.java index 89cf5c36aab..7d9c6421171 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedModule.java @@ -133,9 +133,7 @@ public class NativeAnimatedModule extends NativeAnimatedModuleSpec @UiThread void executeBatch(long maxBatchNumber, NativeAnimatedNodesManager nodesManager) { - List 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 drainQueueIntoList(long maxBatchNumber) { + private @Nullable List drainQueueIntoList(long maxBatchNumber) { + if (mQueue.isEmpty() && mPeekedOperation == null) { + return null; + } + List operations = new ArrayList<>(); while (true) { // Due to a race condition, we manually "carry-over" a polled item from previous batch diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountItemDispatcher.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountItemDispatcher.java index 240c0e203dc..ca2e85ac26e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountItemDispatcher.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountItemDispatcher.java @@ -356,13 +356,16 @@ public class MountItemDispatcher { @Nullable private static List drainConcurrentItemQueue( ConcurrentLinkedQueue queue) { + if (queue.isEmpty()) { + return null; + } List 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; }