From 94489d563d4346516bbaee7220fea552600880ae Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Wed, 11 Sep 2024 08:22:08 -0700 Subject: [PATCH] Add trace markers for onScrollChanged / updateClippingRect (#46412) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46412 These are quite useful when investigating scroll performance. Changelog: [internal] Reviewed By: rshest Differential Revision: D62213724 fbshipit-source-id: 3293aaf82584eae8508c748638c47c8b010db58f --- .../scroll/ReactHorizontalScrollView.java | 53 ++++++++++++------- .../react/views/scroll/ReactScrollView.java | 52 +++++++++++------- .../views/scroll/ReactScrollViewHelper.kt | 3 +- 3 files changed, 67 insertions(+), 41 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java index 1d7800d648a..f9ab9492e75 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java @@ -62,6 +62,7 @@ import com.facebook.react.views.scroll.ReactScrollViewHelper.HasSmoothScroll; import com.facebook.react.views.scroll.ReactScrollViewHelper.HasStateWrapper; import com.facebook.react.views.scroll.ReactScrollViewHelper.ReactScrollViewScrollState; import com.facebook.react.views.view.ReactViewBackgroundManager; +import com.facebook.systrace.Systrace; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; @@ -490,24 +491,30 @@ public class ReactHorizontalScrollView extends HorizontalScrollView FLog.i(TAG, "onScrollChanged[%d] x %d y %d oldx %d oldy %d", getId(), x, y, oldX, oldY); } - super.onScrollChanged(x, y, oldX, oldY); + Systrace.beginSection( + Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "ReactHorizontalScrollView.onScrollChanged"); + try { + super.onScrollChanged(x, y, oldX, oldY); - mActivelyScrolling = true; + mActivelyScrolling = true; - if (mOnScrollDispatchHelper.onScrollChanged(x, y)) { - if (mRemoveClippedSubviews) { - updateClippingRect(); + if (mOnScrollDispatchHelper.onScrollChanged(x, y)) { + if (mRemoveClippedSubviews) { + updateClippingRect(); + } + if (mPreventReentry) { + return; + } + mPreventReentry = true; + ReactScrollViewHelper.updateStateOnScrollChanged( + this, + mOnScrollDispatchHelper.getXFlingVelocity(), + mOnScrollDispatchHelper.getYFlingVelocity(), + mEnableSyncOnScroll); + mPreventReentry = false; } - if (mPreventReentry) { - return; - } - mPreventReentry = true; - ReactScrollViewHelper.updateStateOnScrollChanged( - this, - mOnScrollDispatchHelper.getXFlingVelocity(), - mOnScrollDispatchHelper.getYFlingVelocity(), - mEnableSyncOnScroll); - mPreventReentry = false; + } finally { + Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); } } @@ -783,12 +790,18 @@ public class ReactHorizontalScrollView extends HorizontalScrollView return; } - Assertions.assertNotNull(mClippingRect); + Systrace.beginSection( + Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "ReactHorizontalScrollView.updateClippingRect"); + try { + Assertions.assertNotNull(mClippingRect); - ReactClippingViewGroupHelper.calculateClippingRect(this, mClippingRect); - View contentView = getContentView(); - if (contentView instanceof ReactClippingViewGroup) { - ((ReactClippingViewGroup) contentView).updateClippingRect(); + ReactClippingViewGroupHelper.calculateClippingRect(this, mClippingRect); + View contentView = getContentView(); + if (contentView instanceof ReactClippingViewGroup) { + ((ReactClippingViewGroup) contentView).updateClippingRect(); + } + } finally { + Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java index e49a25a44c3..3995b563300 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java @@ -63,6 +63,7 @@ import com.facebook.react.views.scroll.ReactScrollViewHelper.HasSmoothScroll; import com.facebook.react.views.scroll.ReactScrollViewHelper.HasStateWrapper; import com.facebook.react.views.scroll.ReactScrollViewHelper.ReactScrollViewScrollState; import com.facebook.react.views.view.ReactViewBackgroundManager; +import com.facebook.systrace.Systrace; import java.lang.reflect.Field; import java.util.List; @@ -414,24 +415,29 @@ public class ReactScrollView extends ScrollView @Override protected void onScrollChanged(int x, int y, int oldX, int oldY) { - super.onScrollChanged(x, y, oldX, oldY); + Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "ReactScrollView.onScrollChanged"); + try { + super.onScrollChanged(x, y, oldX, oldY); - mActivelyScrolling = true; + mActivelyScrolling = true; - if (mOnScrollDispatchHelper.onScrollChanged(x, y)) { - if (mRemoveClippedSubviews) { - updateClippingRect(); + if (mOnScrollDispatchHelper.onScrollChanged(x, y)) { + if (mRemoveClippedSubviews) { + updateClippingRect(); + } + if (mPreventReentry) { + return; + } + mPreventReentry = true; + ReactScrollViewHelper.updateStateOnScrollChanged( + this, + mOnScrollDispatchHelper.getXFlingVelocity(), + mOnScrollDispatchHelper.getYFlingVelocity(), + mEnableSyncOnScroll); + mPreventReentry = false; } - if (mPreventReentry) { - return; - } - mPreventReentry = true; - ReactScrollViewHelper.updateStateOnScrollChanged( - this, - mOnScrollDispatchHelper.getXFlingVelocity(), - mOnScrollDispatchHelper.getYFlingVelocity(), - mEnableSyncOnScroll); - mPreventReentry = false; + } finally { + Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); } } @@ -543,12 +549,18 @@ public class ReactScrollView extends ScrollView return; } - Assertions.assertNotNull(mClippingRect); + Systrace.beginSection( + Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "ReactScrollView.updateClippingRect"); + try { + Assertions.assertNotNull(mClippingRect); - ReactClippingViewGroupHelper.calculateClippingRect(this, mClippingRect); - View contentView = getContentView(); - if (contentView instanceof ReactClippingViewGroup) { - ((ReactClippingViewGroup) contentView).updateClippingRect(); + ReactClippingViewGroupHelper.calculateClippingRect(this, mClippingRect); + View contentView = getContentView(); + if (contentView instanceof ReactClippingViewGroup) { + ((ReactClippingViewGroup) contentView).updateClippingRect(); + } + } finally { + Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.kt index f0f7e914d45..47203a7b497 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.kt @@ -30,8 +30,9 @@ import java.util.concurrent.CopyOnWriteArrayList /** Helper class that deals with emitting Scroll Events. */ public object ReactScrollViewHelper { - private val TAG = ReactHorizontalScrollView::class.java.simpleName + private val TAG = ReactScrollView::class.java.simpleName private val DEBUG_MODE = false // ReactBuildConfig.DEBUG + private const val CONTENT_OFFSET_LEFT = "contentOffsetLeft" private const val CONTENT_OFFSET_TOP = "contentOffsetTop" private const val SCROLL_AWAY_PADDING_TOP = "scrollAwayPaddingTop"