Skip to content

Pace Wayland clipboard polling by the cost of the last capture - #20

Open
capybaraonchain wants to merge 1 commit into
standardagents:mainfrom
capybaraonchain:wayland-capture-pacing
Open

capybaraonchain wants to merge 1 commit into
standardagents:mainfrom
capybaraonchain:wayland-capture-pacing

Conversation

@capybaraonchain

Copy link
Copy Markdown

Problem

On Wayland the daemon has no change notification, so it captures the whole clipboard every poll_interval_ms (75 ms by default), reading and hashing every format. When the clipboard holds something large, one capture takes longer than the interval and the captures run back to back.

Seen on an Arch Linux / Hyprland machine paired with a Mac (v0.2.18): after a screenshot was copied on the Mac, the Linux daemon used about 1.2 CPU cores continuously until the clipboard changed. With text on the clipboard it idles at 0%.

Change

After a capture that took more than half the poll interval, the next poll waits nine times that cost, three seconds at most. Cheap captures are not affected and keep the configured interval. The wait is kept across a cancelled select! round, so another branch running first does not end it early.

It is a small change in Daemon::watch_clipboard plus a capture_pause helper. Change detection, formats and X11 are untouched; the only cost is that a change to an already large clipboard is noticed up to three seconds later.

Measurements

Same machine, Mac screenshot on the clipboard:

daemon CPU
v0.2.18 about 123% of one core
with this change about 12%
text on the clipboard (both) 0%

Sync in both directions still works.

Tests

Two tests in src/daemon/tests.rs drive the real watch loop with a polled backend: an expensive capture (60 ms at a 20 ms interval) is captured about 3 times in 1.5 s (25 times without the change, so the test fails on main), and a cheap one keeps its interval. cargo fmt --all -- --check and cargo test --all-targets pass.

Disclosure

The diagnosis, the patch and this description were written by an AI assistant (Claude) working on my machine; I ran it there and the numbers above are from that machine. A better long-term fix may be a real change notification on Wayland (for example the data-control protocol's selection events), which would remove the polling entirely; this change only bounds its cost.

Wayland has no usable change notification, so the daemon captures the whole
clipboard every poll interval (75 ms by default): every format is read and
hashed. With a large clipboard, such as a macOS screenshot synced with its PNG
and TIFF forms, one capture takes longer than the interval, so captures run
back to back and the daemon uses more than one CPU core until the clipboard
changes.

After a capture that took more than half the poll interval, wait nine times
its cost (three seconds at most) before the next one. Cheap captures keep the
configured interval.

Co-Authored-By: Claude Fable 5.2 <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