Skip to content

A window reads its display's refresh grid without the registry's lock - #616

Merged
wingleeio merged 1 commit into
mainfrom
flush/align-guard-trim
Oct 26, 2026
Merged

wingleeio merged 1 commit into
mainfrom
flush/align-guard-trim

Conversation

@wingleeio

@wingleeio wingleeio commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

This is the guard trim deferred from #552. DisplayLink::refresh_phase runs on every steady frame: once for the latch's grid and once for the align wait before handover. Each call locked the link registry, the mutex the CoreVideo thread holds through every callback, and read the clock twice.

What changes:

  • The grid, meaning the last refresh's host time and the period, moves into an Arc<Grid> of two atomics. The display's entry and the windows on it share it, and follow_display swaps it when a window changes screens.
  • refresh_phase(fresh) reads it without the lock and with one mach_absolute_time. The phase is that of the moment of the call; both callers passed Instant::now() there anyway.
  • Its at parameter is gone. It was crate-internal, so no public API changes.

Same behaviour: the same grid and the same phase, give or take the nanoseconds between the two clock reads it used to make. Without the lock, a reader can see a period from one callback beside a refresh time from the next. That can only matter while the display changes mode, and the comment says so.

Measured: animate, dist, 6 alternating 4 s runs each, /usr/bin/time -l.

main this
instructions per frame shown 3,179 K 3,182 K
fps (120 Hz panel) 116.55 117.00

Both are within run-to-run noise (about 2.5% between runs). The cut is about 100–200 instructions a frame plus one lock the link thread contends for, too small to resolve here. It removes UI-thread versus link-thread lock contention from the frame's handover path rather than buying a measurable instruction win.

Tests: cargo test -p zgx-desktop, clippy 1.99 -D warnings, fmt.

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

`DisplayLink::refresh_phase` runs once or twice a frame (the latch's grid
and the align wait before handover). It locked the link registry, the
mutex the CoreVideo thread holds through every callback, and read the
clock twice. The grid (last refresh, period) is now its own pair of
atomics shared between the display's entry and the windows on it, read
lock-free, with one host-clock read: the phase is of the moment of the
call, which both callers already passed as `Instant::now()`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Instruction counts (cachegrind, exact) against 4132a64d; the gate fails above +1%.

scene base head change
heavy-both 1,411.8 M 1,411.9 M +0.00%
heavy-stream 1,355.8 M 1,355.8 M -0.00%
heavy-scroll 80.3 M 80.3 M -0.00%
lab-stream 462.0 M 462.0 M -0.00%
lab-scroll 70.1 M 70.1 M +0.00%
animate 394.8 M 394.8 M +0.00%
micro-state 59.7 M 59.7 M -0.01%
micro-vscroll-fixed 48.7 M 48.7 M -0.01%
micro-vscroll-variable 89.0 M 89.1 M +0.06%
micro-state-luau 82.9 M 82.9 M +0.01%
micro-vscroll-fixed-luau 66.2 M 66.2 M -0.02%
micro-vscroll-variable-luau 134.6 M 134.6 M -0.02%
mount 327.4 M 327.4 M +0.01%
script-update 1,698.5 M 1,698.2 M -0.02%
anime 4,672.7 M 4,672.7 M +0.00%
script-layout 3,244.3 M 3,243.2 M -0.04%
list-state 893.5 M 893.7 M +0.02%
style 2,084.3 M 2,084.3 M +0.00%
widgets 2,684.5 M 2,684.5 M +0.00%
app-mail 318.8 M 318.5 M -0.09%
app-chat 53.2 M 53.2 M +0.06%
app-dashboard 189.5 M 189.5 M -0.03%
app-dashboard-sort 126.3 M 126.3 M +0.01%
app-sketch 48.4 M 48.5 M +0.08%
app-code 99.8 M 99.9 M +0.04%
app-code-search 87.6 M 87.6 M +0.04%

@wingleeio
wingleeio merged commit dea3019 into main Oct 26, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant