An open source design system that's fully customizable and agent ready
TypeScript
fix(sandbox): the DiffViewer perf page mounts files on screen, so a phone can run it (#8487)
Cindy Zhang51a0d3cca95f
diff --git a/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/page.tsx b/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/page.tsxindex 1b1bf6da31..1f6ec54517 100644--- a/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/page.tsx+++ b/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/page.tsx@@ -8,12 +8,23 @@* and syntax highlighting on or off* @output Interactive DiffViewer benchmark: diff-model time, first file on* screen, total render, long tasks, DOM size, heap, scroll frames, and the- * cost of collapse, expand, and view switches+ * cost of collapse, expand, and view switches; files mount on screen by+ * default, or all at once+* @position Fullscreen sandbox tool for evaluating the Lab DiffViewer on large* change sets*/-import {memo, useCallback, useMemo, useRef, useState} from 'react';+import {+ memo,+ useCallback,+ useEffect,+ useMemo,+ useRef,+ useState,+ type ComponentProps,+ type RefObject,+} from 'react';import {flushSync} from 'react-dom';import {AppShell} from '@astryxdesign/core/AppShell';import {Badge} from '@astryxdesign/core/Badge';@@ -48,6 +59,11 @@ import {type Scenario = 'multi' | 'single';type Syntax = 'on' | 'off';+/**+ * `visible` mounts a file's DiffViewer only while it is near the viewport, so+ * a phone can hold any workload; `all` mounts the whole change set at once.+ */+type Mount = 'visible' | 'all';/** What a run measured, captured when it started so later control changes don't relabel it. */interface RunSubject {@@ -55,6 +71,7 @@ interface RunSubject {totalLines: number;viewMode: DiffViewerViewMode;syntax: Syntax;+ mount: Mount;}interface RunMetrics {@@ -101,6 +118,11 @@ const EMPTY_RUN: RunMetrics = {};const SCROLL_DURATION_MS = 4000;+/** How far outside the viewport a file mounts, so scrolling meets rendered files. */+const MOUNT_MARGIN_PX = 800;+/** Placeholder estimate for a file never measured: its header and each row. */+const FILE_HEADER_PX = 48;+const DIFF_ROW_PX = 20;/** Stop looking for the first rendered file after this long. */const FIRST_FILE_TIMEOUT_MS = 60_000;@@ -203,6 +225,103 @@ const BenchmarkFile = memo(function BenchmarkFile({);});+/** A file's height before it has rendered: its header, plus each diff row. */+function estimateFileHeight(+ file: DiffViewerFile,+ isCollapsed: boolean,+): number {+ if (isCollapsed) {+ return FILE_HEADER_PX;+ }+ let rows = 0;+ for (const hunk of file.hunks) {+ rows += hunk.lines.length + 1;+ }+ return FILE_HEADER_PX + rows * DIFF_ROW_PX;+}++/**+ * Mounts its file only while it is within MOUNT_MARGIN_PX of the scroll+ * viewport, and keeps the file's last rendered height in its place otherwise,+ * so the list's scroll height and position do not jump.+ */+function OnScreenFile({+ viewportRef,+ ...props+}: ComponentProps<typeof BenchmarkFile> & {+ viewportRef: RefObject<HTMLDivElement | null>;+}) {+ const {file, isCollapsed, viewMode, language} = props;+ const slotRef = useRef<HTMLDivElement>(null);+ // The last rendered height, kept only for the layout it was measured in.+ const layout = `${isCollapsed}:${viewMode}:${language ?? ''}`;+ const [measured, setMeasured] = useState<{+ layout: string;+ height: number;+ } | null>(null);+ const layoutRef = useRef(layout);+ useEffect(() => {+ layoutRef.current = layout;+ }, [layout]);+ const [isNear, setIsNear] = useState(false);++ useEffect(() => {+ const slot = slotRef.current;+ if (slot == null) {+ return;+ }+ const observer = new IntersectionObserver(+ entries => {+ for (const entry of entries) {+ setIsNear(entry.isIntersecting);+ }+ },+ {root: viewportRef.current, rootMargin: `${MOUNT_MARGIN_PX}px 0px`},+ );+ observer.observe(slot);+ const sizes = new ResizeObserver(entries => {+ for (const entry of entries) {+ const child = entry.target.firstElementChild;+ if (child != null && !child.hasAttribute('data-placeholder')) {+ const next = {+ layout: layoutRef.current,+ height: entry.contentRect.height,+ };+ setMeasured(previous =>+ previous?.layout === next.layout && previous.height === next.height+ ? previous+ : next,+ );+ }+ }+ });+ sizes.observe(slot);+ return () => {+ observer.disconnect();+ sizes.disconnect();+ };+ }, [viewportRef]);++ return (+ <div ref={slotRef}>+ {isNear ? (+ <BenchmarkFile {...props} />+ ) : (+ <div+ aria-hidden+ data-placeholder=""+ style={{+ height:+ measured?.layout === layout+ ? measured.height+ : estimateFileHeight(file, isCollapsed),+ }}+ />+ )}+ </div>+ );+}+function Metric({label, value}: {label: string; value: string}) {return (<VStack gap={0.5}>@@ -219,6 +338,7 @@ export default function DiffViewerPerfPage() {const [change, setChange] = useState<SingleFileChange>('sparse');const [viewMode, setViewMode] = useState<DiffViewerViewMode>('unified');const [syntax, setSyntax] = useState<Syntax>('on');+ const [mount, setMount] = useState<Mount>('visible');const [files, setFiles] = useState<ReadonlyArray<DiffViewerFile>>([]);const [collapsed, setCollapsed] = useState<ReadonlySet<string>>(new Set());const [run, setRun] = useState<RunMetrics>(EMPTY_RUN);@@ -248,6 +368,7 @@ export default function DiffViewerPerfPage() {totalLines: workload.totalLines,viewMode,syntax,+ mount,};setIsBusy(true);setScroll(null);@@ -296,15 +417,29 @@ export default function DiffViewerPerfPage() {// Looks for a rendered diff now and then once per frame, so a file// that renders late is caught at the frame it appears.const watchFirstFile = () => {- if (surfaceRef.current?.querySelector('table') != null) {+ // Unified view renders a <table>; split view, a role="table" grid.+ if (+ surfaceRef.current?.querySelector('table, [role="table"]') != null+ ) {afterNextPaint(renderStart, ms => {firstFileMs = ms;+ // On screen, the files near the viewport mount together with+ // the first one, so that paint is the full render.+ if (subject.mount === 'visible') {+ rendered = {renderMs: ms, longTasks: stopLongTasks()};+ }publish();});return;}if (performance.now() - renderStart > FIRST_FILE_TIMEOUT_MS) {firstFileMs = null;+ if (subject.mount === 'visible') {+ rendered = {+ renderMs: performance.now() - renderStart,+ longTasks: stopLongTasks(),+ };+ }publish();return;}@@ -316,13 +451,15 @@ export default function DiffViewerPerfPage() {setRunKey(key => key + 1);});watchFirstFile();- afterNextPaint(renderStart, renderMs => {- rendered = {renderMs, longTasks: stopLongTasks()};- publish();- });+ if (subject.mount === 'all') {+ afterNextPaint(renderStart, renderMs => {+ rendered = {renderMs, longTasks: stopLongTasks()};+ publish();+ });+ }}, 0);});- }, [workload, viewMode, syntax]);+ }, [workload, viewMode, syntax, mount]);// `view` is what the timed update leaves on screen, which a switch changes.const timeInteraction = (@@ -423,12 +560,16 @@ export default function DiffViewerPerfPage() {};const controlsDisabled = isBusy || isScrolling;+ // The list keeps the mount mode its run was measured in, so switching Mount+ // after a run changes the next run, not the readings on screen.+ const listMount = run.subject?.mount ?? mount;// Badges describe the measured run once there is one, else the selection.const shown: RunSubject = run.subject ?? {label: workload.label,totalLines: workload.totalLines,viewMode,syntax,+ mount,};return (@@ -516,6 +657,15 @@ export default function DiffViewerPerfPage() {<SegmentedControlItem value="on" label="Highlighted" /><SegmentedControlItem value="off" label="Plain" /></SegmentedControl>+ <SegmentedControl+ label="Mount"+ value={mount}+ onChange={value => setMount(value as Mount)}+ isDisabled={controlsDisabled}+ size="sm">+ <SegmentedControlItem value="visible" label="On screen" />+ <SegmentedControlItem value="all" label="All" />+ </SegmentedControl><Buttonlabel="Run benchmark"onClick={runBenchmark}@@ -524,7 +674,7 @@ export default function DiffViewerPerfPage() {</HStack></Section>- <Grid columns={2} gap={4}>+ <Grid columns={{minWidth: 360, max: 2}} gap={4}><Card padding={3}><VStack gap={3}><Heading level={3}>Run metrics</Heading>@@ -538,6 +688,13 @@ export default function DiffViewerPerfPage() {<Badgelabel={shown.syntax === 'on' ? 'highlighted' : 'plain'}/>+ <Badge+ label={+ shown.mount === 'visible'+ ? 'mounted on screen'+ : 'all mounted'+ }+ /></HStack><Grid columns={2} gap={3}><Metric@@ -615,7 +772,12 @@ export default function DiffViewerPerfPage() {heap reading that does not move) shows as a dash. Badges namethe workload the run was measured on; the scroll and interactionreadings name the view they were taken in. The scroll test moves- the list from top to bottom over four seconds.+ the list from top to bottom over four seconds. With Mount on+ screen, a file renders only near the viewport: DOM size and heap+ cover what is on screen, full render is the paint the first+ files appear in, and scrolling includes mounting files. Mount+ all renders the whole change set at once, which can exhaust a+ phone's memory.</Text></VStack></Card>@@ -628,15 +790,21 @@ export default function DiffViewerPerfPage() {<VStack gap={3}>{files.map(file => {const path = file.newPath ?? file.oldPath ?? '';- return (- <BenchmarkFile+ const fileProps = {+ file,+ viewMode,+ language,+ isCollapsed: collapsed.has(path),+ onToggle: toggleFile,+ };+ return listMount === 'visible' ? (+ <OnScreenFilekey={path}- file={file}- viewMode={viewMode}- language={language}- isCollapsed={collapsed.has(path)}- onToggle={toggleFile}+ viewportRef={surfaceRef}+ {...fileProps}/>+ ) : (+ <BenchmarkFile key={path} {...fileProps} />);})}</VStack>