diff --git a/packages/rn-tester/js/examples/Playground/RNTesterPlayground.js b/packages/rn-tester/js/examples/Playground/RNTesterPlayground.js index 8e6b10479de7..8c6b5fb1d30c 100644 --- a/packages/rn-tester/js/examples/Playground/RNTesterPlayground.js +++ b/packages/rn-tester/js/examples/Playground/RNTesterPlayground.js @@ -12,22 +12,103 @@ import type {RNTesterModuleExample} from '../../types/RNTesterTypes'; import RNTesterText from '../../components/RNTesterText'; import * as React from 'react'; -import {StyleSheet, View} from 'react-native'; +import {useEffect, useState} from 'react'; +import {ScrollView, StyleSheet, View} from 'react-native'; + +// Repro for https://github.com/react/react-native/issues/60227 +// +// The ScrollView below holds 2^11 = 2048 leaf views (about 4k views in all), +// and none of them sets `removeClippedSubviews`. A small view above it updates +// on every frame, so every frame produces one mounting transaction. +// +// On iOS (Fabric), after every mounting transaction +// `-[RCTScrollViewComponentView mountingTransactionDidMount:withSurfaceTelemetry:]` +// calls `_remountChildren`, which walks the whole content of the ScrollView +// with `updateClippedSubviewsWithClipRect:relativeToView:` and one +// `convertRect:toView:` per view. Nothing can be clipped, so the walk changes +// nothing, but it costs main-thread time on every frame. +// +// To see it: run RNTester (Release) on iOS, open this Playground, and record +// the main thread with Instruments (Time Profiler) or `sample `. +// `-[UIView(ComponentViewProtocol) updateClippedSubviewsWithClipRect:relativeToView:]` +// and `-[CALayer convertRect:toLayer:]` are under +// `-[RCTScrollViewComponentView mountingTransactionDidMount:withSurfaceTelemetry:]`. +// Tap "Stop" to stop the per-frame updates: the cost goes away. + +const DEPTH = 11; + +function Nested({depth}: {depth: number}): React.Node { + if (depth === 0) { + return ; + } + return ( + + + + + ); +} + +function Ticker({running}: {running: boolean}): React.Node { + const [frame, setFrame] = useState(0); + useEffect(() => { + if (!running) { + return; + } + let id = requestAnimationFrame(function tick() { + setFrame(value => value + 1); + id = requestAnimationFrame(tick); + }); + return () => cancelAnimationFrame(id); + }, [running]); + return ( + + + + ); +} function Playground() { + const [running, setRunning] = useState(true); return ( - - Edit "RNTesterPlayground.js" to change this file + setRunning(value => !value)}> + {running + ? 'Updating every frame. Tap to stop.' + : 'Stopped. Tap to update every frame.'} + + + + ); } const styles = StyleSheet.create({ + bar: { + backgroundColor: 'tomato', + height: '100%', + }, container: { + flex: 1, padding: 10, }, + leaf: { + backgroundColor: '#ccc', + height: 4, + margin: 1, + }, + node: { + padding: 1, + }, + scrollView: { + flex: 1, + }, + ticker: { + height: 8, + marginVertical: 8, + }, }); export default {