Skip to content

macOS: linger before stopping a display link to stop io-thread churn - #16

Open
neo773 wants to merge 1 commit into
zeronsh:mainfrom
neo773:fix/display-link-churn
Open

neo773 wants to merge 1 commit into
zeronsh:mainfrom
neo773:fix/display-link-churn

Conversation

@neo773

@neo773 neo773 commented Oct 8, 2026 •

Copy link
Copy Markdown

Summary

  • Every CVDisplayLinkStart spawns a fresh io thread. Windows unsubscribe from their display's link whenever frame requests park (pause_frame_requests). A timer-driven animation, like a 30 Hz spinner, parks between every frame, so the link was stopped and restarted once per frame: one new thread per frame.
  • A display's link now keeps running for 250 ms (STOP_LINGER) after its last subscriber leaves:
    • A subscriber that returns within that window reuses the running link, with no restart and no new thread.
    • Idle windows still stop the link shortly after parking.
    • While the link lingers, its output callback finds no subscribers and does nothing.
  • The running/stop logic moves into a small LinkState:
    • A generation counter makes a deferred stop a no-op if anyone subscribed after it was scheduled.
    • The deferred stop runs on the main queue, so the registry's main-thread-only rule and lock ordering are unchanged.

Measurements

A probe window redrawing on a 30 Hz timer, sampled for 3 s with /usr/bin/sample and counting CVDisplayLink threads:

Run 1 Run 2 Run 3
Before 89 88 87
After 1 1 1

In a real app (Zeron) with a sidebar spinner running, a 2 s sample showed 25 CVDisplayLink threads come and go.

Test plan

  • cargo test -p gpui_macos --lib display_link: 5 new LinkState tests covering:
    • starting the link once
    • stopping after the linger
    • resubscribing during the linger without a restart
    • only the latest deferred stop applying
    • no stop scheduled when the link never ran
  • cargo clippy -p gpui_macos: no warnings in display_link.rs
  • The 30 Hz probe above, before and after the change

🤖 Generated with Claude Code


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

Every CVDisplayLinkStart spawns a fresh io thread, and windows unsubscribe
whenever frame requests park. A timer-driven animation (a 30 Hz spinner)
parks between every frame, so the link was stopped and restarted per frame:
a new thread per frame (~88 in 3 s in a 30 Hz probe).

Keep a display's link running for 250 ms after its last subscriber leaves.
A subscriber that returns within the linger reuses the running link; a
generation counter makes stale deferred stops no-ops. Idle windows still
stop the link shortly after parking.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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