feat(x11): MIT-SHM 1.2 fd-passing present fast path (measured win, PutImage fallback) - #4
Merged
Merged
Conversation
…tImage fallback)
Plain PutImage ships every pixel of an update through the X socket. This adds
the MIT-SHM 1.2 path: the client writes pixels into a shared-memory segment
both peers map and sends a tiny ShmPutImage request instead. The segment
descriptor is handed to the server via AttachFd over SCM_RIGHTS, reusing the
same sovereign fd-passing machinery as the Wayland backend (memfd-style
shm_open+ftruncate+mmap; no SysV shmget).
Measured on the Xvfb runner (800x600, depth 24, 300 iters), reproduced by
TestLiveX11SHMMeasure and uploaded as shm-measure.txt:
full window: PutImage 1920192 B/present 2.96 ms ->
ShmPutImage 40 B/present 1.76 ms (~48000x fewer bytes, ~40% faster)
small damage: PutImage 4120 B/present 0.044 ms ->
ShmPutImage 40 B/present 0.042 ms (~103x fewer bytes)
This backend presents the whole framebuffer after input, so the full-window
win dominates: SHIP. The plain PutImage path stays as the fallback whenever
the server lacks the extension, lacks fd-passing, or is < 1.2.
- internal/x11: QueryExtension; MIT-SHM QueryVersion/AttachFd/Detach/
ShmPutImage; Presenter.EncodeRectInto/SegmentSize; NewSegment (shared mmap);
WrapUnix transport adapter + exported FDSender. 100%-covered (both endians,
real SCM_RIGHTS fd path via socketpair, all syscall failure branches).
- window: Open() auto-enables SHM when available; present() mirrors the damage
rect into the segment and blits with ShmPutImage; segment grows on resize;
Close detaches. Silent fallback to PutImage on any SHM setup failure.
- live proof: TestLiveX11 now draws via ShmPutImage (asserted active by
TestLiveX11SHMActive), so its captured-pixel assertions prove SHM present
renders correctly.
- ci: live X11 lane renamed "(Xvfb, +MIT-SHM)"; uploads shm-measure.txt.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Task 2 — MIT-SHM present for X11 (measure-first)
Plain
PutImageships every pixel of an update through the X socket. This addsthe MIT-SHM 1.2 path: the client writes pixels into a shared-memory segment
both peers map and sends a tiny
ShmPutImagerequest instead. The segmentdescriptor is passed via
AttachFdover SCM_RIGHTS, reusing the samesovereign fd-passing machinery as the Wayland backend (memfd-style
shm_open+ftruncate+mmap; no SysVshmget).Measured on the Xvfb runner (800×600, depth 24, 300 iters)
Reproduced by
TestLiveX11SHMMeasure, uploaded asshm-measure.txt:Full-window: ~48,000× fewer bytes over the socket and ~40 % lower latency.
This backend presents the whole framebuffer after input, so the full-window
win dominates → SHIP. Plain
PutImagestays as the fallback whenever theserver lacks the extension, lacks fd-passing, or is
< 1.2.What changed
internal/x11:QueryExtension; MIT-SHMQueryVersion/AttachFd/Detach/ShmPutImage;Presenter.EncodeRectInto/SegmentSize;NewSegment(shared
mmap);WrapUnixtransport adapter + exportedFDSender.100 %-covered — both endian paths, the real SCM_RIGHTS fd path over a
socketpair, and every syscall failure branch.
window:Open()auto-enables SHM when available;present()mirrors thedamage rect into the segment and blits with
ShmPutImage; the segment growson resize;
Closedetaches. Silent fallback toPutImageon any failure.TestLiveX11now draws throughShmPutImage(asserted active byTestLiveX11SHMActive), so its captured-pixel assertions prove SHM presentrenders correctly — nothing faked.
(Xvfb, +MIT-SHM); uploadsshm-measure.txt.internal/x11andinternal/waylandboth at 100.0 % coverage under-race.🤖 Generated with Claude Code