Skip to content

Commit 59e95cc

Browse files
authored
perf(app): stop restyling the side panel on every move of a resize drag (#61560)
1 parent ba084e6 commit 59e95cc

5 files changed

Lines changed: 154 additions & 8 deletions

File tree

‎packages/app/perf/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build on the e2e mock server, so they need no backend.
77

88
| File | Cases | Workload |
99
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------ |
10-
| `timeline.bench.ts` | scroll history · type in composer · find in session · expand an edit | A 120-turn session of markdown, code, tools and diffs. |
10+
| `timeline.bench.ts` | scroll history · type in composer · find in session · expand an edit · drag the review's edge | A 120-turn session of markdown, code, tools and diffs. |
1111
| `stream.bench.ts` | stream a reply · into a tool-heavy history · with review open · wait on a running session | 160 markdown deltas at 40 a second from a server that never waits. |
1212
| `tabs.bench.ts` | switch tab (warm, review open, after a resize, 1,200 models) · open an unvisited tab (± review) | Two tabs of 200 long markdown exchanges each, loaded in full. |
1313
| `navigation.bench.ts` | Home → session · close the last tab · new session (Home, titlebar) · new session page in and out · child session · restore 8 tabs · activate a restored tab · command palette (session, Home) | The stress fixture, paginated, from a fresh document every run. |

‎packages/app/perf/fixtures/drag.ts‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import type { Page } from "@playwright/test"
2+
3+
/** A divider to drag: its centre, how far it can travel each way (px), and the display's frame period (ms). */
4+
export type Divider = { x: number; y: number; left: number; right: number; period: number }
5+
6+
type Leg = { seconds: number; at: (progress: number) => number }
7+
8+
const ease = (from: number, to: number, seconds: number): Leg => ({
9+
seconds,
10+
at: (progress) => from + ((to - from) * (1 - Math.cos(Math.PI * progress))) / 2,
11+
})
12+
13+
// Whole cycles, so a jiggle ends where it began.
14+
const jiggle = (around: number, amplitude: number, cycles: number, seconds: number): Leg => ({
15+
seconds,
16+
at: (progress) => around + amplitude * Math.sin(2 * Math.PI * cycles * progress),
17+
})
18+
19+
/**
20+
* How a hand drags a divider, as offsets from where the drag starts against time, scaled by the room each way: a slow
21+
* stretch one way, a jiggle there, a fast throw the other way, a slow pull back, a small jiggle and home. 4.95 s.
22+
*/
23+
function legs(divider: Divider) {
24+
const l = -divider.left
25+
const r = divider.right
26+
27+
return [
28+
ease(0, 0.8 * l, 1.2),
29+
jiggle(0.8 * l, 0.15 * divider.left, 3, 0.75),
30+
ease(0.8 * l, r, 0.6),
31+
ease(r, l, 1.5),
32+
ease(l, 0.4 * l, 0.4),
33+
jiggle(0.4 * l, 0.1 * divider.left, 3, 0.5),
34+
ease(0.4 * l, 0, 0.5),
35+
]
36+
}
37+
38+
/**
39+
* Presses the divider, moves the pointer once per display frame along the drag above without letting go, and releases
40+
* it where it started. strobe's driver has no pointer drag (`page.move` does not hold a button, and its `until:
41+
* "frame"` waits two animation frames and a round trip per move), so Playwright's mouse drives the drag, from the
42+
* runner, paced by the display's frame period on the wall clock. A frame the page misses is skipped, as a hand keeps
43+
* moving while the screen lags.
44+
*/
45+
export async function dragDivider(mouse: Page["mouse"], divider: Divider) {
46+
const path = legs(divider)
47+
const total = path.reduce((sum, leg) => sum + leg.seconds, 0)
48+
49+
const offset = (seconds: number) => {
50+
let left = seconds
51+
52+
for (const leg of path) {
53+
if (left <= leg.seconds) return leg.at(left / leg.seconds)
54+
left -= leg.seconds
55+
}
56+
57+
return 0
58+
}
59+
60+
await mouse.move(divider.x, divider.y)
61+
await mouse.down()
62+
const began = performance.now()
63+
64+
for (let frame = 1; ; frame++) {
65+
const wait = began + frame * divider.period - performance.now()
66+
67+
if (wait > 0) await new Promise((resolve) => setTimeout(resolve, wait))
68+
frame = Math.max(frame, Math.floor((performance.now() - began) / divider.period))
69+
const seconds = (performance.now() - began) / 1000
70+
71+
if (seconds >= total) break
72+
await mouse.move(divider.x + offset(seconds), divider.y)
73+
}
74+
75+
await mouse.move(divider.x, divider.y)
76+
await mouse.up()
77+
}
78+
79+
/**
80+
* Reads, in the page, where the divider `selector` sits, the room its panel `panel` has to shrink to `min` and grow to
81+
* the row `row` less `rest` (px), and the display's frame period over 30 frames. Only in setup: it reads layout.
82+
*/
83+
export function measureDivider(input: { selector: string; panel: string; row: string; min: number; rest: number }) {
84+
return new Promise<Divider>((resolve) => {
85+
const box = document.querySelector(input.selector)!.getBoundingClientRect()
86+
const width = document.querySelector(input.panel)!.getBoundingClientRect().width
87+
const row = document.querySelector(input.row)!.getBoundingClientRect().width
88+
const times: number[] = []
89+
90+
const tick = (time: number) => {
91+
times.push(time)
92+
93+
if (times.length < 31) return requestAnimationFrame(tick)
94+
resolve({
95+
x: box.left + box.width / 2,
96+
y: box.top + box.height / 2,
97+
left: Math.max(0, Math.min(360, width - input.min - 10)),
98+
right: Math.max(0, Math.min(160, row - input.rest - width - 10)),
99+
period: (times[30]! - times[0]!) / 30,
100+
})
101+
}
102+
103+
requestAnimationFrame(tick)
104+
})
105+
}

‎packages/app/perf/timeline.bench.ts‎

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// One long session (120 turns of markdown, code fences, tool calls and diffs) at normal speed.
22
import { bench, web } from "@hona/strobe"
3-
import { editTools } from "./fixtures/diffs"
3+
import { editTools, reviewDiffs } from "./fixtures/diffs"
4+
import { dragDivider, measureDivider, type Divider } from "./fixtures/drag"
45
import { cached } from "./fixtures/mock"
6+
import { openReview } from "./fixtures/review"
57
import { composer, mockTimelineSession, oldestPrompt, timeline } from "./fixtures/timeline"
68

79
const find = '[data-component="timeline-search-bar"] input'
@@ -13,25 +15,47 @@ const edit = '[data-scope="apply-patch"] [data-slot="accordion-trigger"]'
1315

1416
const diff = '[data-component="apply-patch-file-diff"] diffs-container >>> [data-line]'
1517

18+
const drag = "drag the review's edge"
19+
20+
// The divider between the session and the side panel, read once the review is open. The session panel keeps at least
21+
// 400px, the side panel 320px and the 8px gap.
22+
let divider: Divider | undefined
23+
1624
export default bench({
1725
targets: {
1826
web: web({
1927
viewport: { width: 1366, height: 768 },
20-
prepare: async ({ case: name, playwright, mock, url }) =>
21-
mockTimelineSession(cached(playwright.page, mock), {
28+
prepare: async ({ case: name, playwright, mock, url }) => ({
29+
...(await mockTimelineSession(cached(playwright.page, mock), {
2230
server: new URL(url).origin,
2331
// The edits alone, so the case measures the diff, not the history around it.
2432
historyTurns: name === "expand an edit" ? 0 : 120,
2533
collapsedEdits: name === "expand an edit",
2634
tools: name === "expand an edit" ? await editTools() : undefined,
27-
}),
35+
vcsDiff: name === drag ? reviewDiffs() : undefined,
36+
})),
37+
mouse: playwright.page.mouse,
38+
}),
2839
url: ({ prepared }) => prepared.path,
2940
}),
3041
},
3142

32-
async setup({ page }) {
43+
async setup({ case: name, page }) {
3344
await page.waitFor(timeline, { timeout: 60_000 })
3445
await page.waitFor(composer)
46+
47+
if (name === drag) {
48+
await openReview(page)
49+
await page.idle()
50+
divider = await page.eval(measureDivider, {
51+
selector: '[data-slot="session-chat-panel"] > div > [data-component="resize-handle"]',
52+
panel: '[data-slot="session-chat-panel"]',
53+
row: "[data-pane-row]",
54+
min: 400,
55+
rest: 328,
56+
})
57+
}
58+
3559
await page.idle()
3660
},
3761

@@ -85,5 +109,16 @@ export default bench({
85109
await page.idle()
86110
},
87111
},
112+
113+
// The divider beside the review (72 changed files) held for five seconds and dragged as a hand does, a pointer
114+
// move every display frame: a slow stretch toward the session, jiggles, a fast throw toward the review and a slow
115+
// pull back (fixtures/drag.ts). The timeline narrows and widens on every frame, so the rows in view rewrap; each
116+
// frame should keep up with the display.
117+
[drag]: {
118+
primary: "frames.dropped.pct",
119+
metrics: ["main.work", "main.layout", "main.style", "main.script", "frames.interval.p95"],
120+
run: ({ prepared }) => dragDivider(prepared.mouse, divider!),
121+
reset: ({ page }) => page.idle(),
122+
},
88123
},
89124
})

‎packages/app/src/runtime/plugin/side-region.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ export function SideRegion(props: {
5353
sidebar: PanelSidebar
5454
present?: boolean
5555
size: Sizing
56+
/**
57+
* The width the contents keep, the side region's open width, which a close keeps as the far end of its travel. Set
58+
* on the content box alone: an inherited custom property on an ancestor restyled every element in the region, the
59+
* review's diffs included, on every frame of a resize drag.
60+
*/
61+
contentWidth: Accessor<string>
5662
drag: PaneDrag
5763
}) {
5864
const layout = useLayout()
@@ -122,7 +128,7 @@ export function SideRegion(props: {
122128
<div
123129
data-slot="session-review-content"
124130
class="h-full flex shrink-0 contain-strict"
125-
style={{ width: "var(--panel-hold-width, var(--session-side-content-width, 100%))" }}
131+
style={{ width: `var(--panel-hold-width, ${props.contentWidth()})` }}
126132
>
127133
<div
128134
ref={stack}

‎packages/app/src/session/screen.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,6 @@ function SessionScreenContent(props: {
532532
ref={sideContent}
533533
data-slot="session-side-panel-content"
534534
class="absolute inset-y-0 start-0 size-full [contain:layout_style] will-change-[opacity]"
535-
style={{ "--session-side-content-width": screen.side.contentWidth() }}
536535
>
537536
<div data-slot="session-side-region" class="absolute inset-0 min-h-0 overflow-visible">
538537
<Show when={store.sideMounted}>
@@ -544,6 +543,7 @@ function SessionScreenContent(props: {
544543
sidebar={sidebar}
545544
present={sideMotion.present()}
546545
size={screen.size}
546+
contentWidth={screen.side.contentWidth}
547547
drag={drag}
548548
/>
549549
</div>

0 commit comments

Comments
 (0)