Skip to content

feat(x11): MIT-SHM 1.2 fd-passing present fast path (measured win, PutImage fallback) - #4

Merged
tannevaled merged 1 commit into
mainfrom
feat/x11-mit-shm
Aug 9, 2026
Merged

feat(x11): MIT-SHM 1.2 fd-passing present fast path (measured win, PutImage fallback)#4
tannevaled merged 1 commit into
mainfrom
feat/x11-mit-shm

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Task 2 — MIT-SHM present for X11 (measure-first)

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 passed 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 (800×600, depth 24, 300 iters)

Reproduced by TestLiveX11SHMMeasure, uploaded as shm-measure.txt:

update path bytes/present ms/present
full window PutImage 1,920,192 2.96
full window ShmPutImage 40 1.76
small damage (32×32) PutImage 4,120 0.044
small damage (32×32) ShmPutImage 40 0.042

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 PutImage stays as the fallback whenever the
server lacks the extension, lacks fd-passing, or is < 1.2.

What changed

  • internal/x11: QueryExtension; MIT-SHM QueryVersion/AttachFd/Detach/
    ShmPutImage; Presenter.EncodeRectInto/SegmentSize; NewSegment
    (shared mmap); WrapUnix transport adapter + exported FDSender.
    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 the
    damage rect into the segment and blits with ShmPutImage; the segment grows
    on resize; Close detaches. Silent fallback to PutImage on any failure.
  • Live proof: TestLiveX11 now draws through ShmPutImage (asserted active by
    TestLiveX11SHMActive), so its captured-pixel assertions prove SHM present
    renders correctly — nothing faked.
  • CI: live X11 lane renamed (Xvfb, +MIT-SHM); uploads shm-measure.txt.

internal/x11 and internal/wayland both at 100.0 % coverage under -race.

🤖 Generated with Claude Code

…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>
@tannevaled
tannevaled merged commit 2f0988f into main Aug 9, 2026
11 checks passed
@tannevaled
tannevaled deleted the feat/x11-mit-shm branch August 9, 2026 08:56
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