perf(present): zero-dispatch RGBA pack + zero-alloc fallback (measured 4-4.6x) - #6
Merged
Merged
Conversation
… alloc (measured 4-4.6x) Audit of the per-frame present/pack path (2026-08-09 12:25 CEST). Both live paths were already zero-alloc — X11 MIT-SHM packs straight into the mmap'd segment, Wayland into the wl_shm pool — so the remaining cost was pure CPU in the RGBA->wire conversion. Measured before optimizing. Real hotspots found + fixed: - Wayland PackARGB8888 called NativeOrder.PutUint32 through the binary.ByteOrder INTERFACE once per pixel (~2M indirect calls/frame at 1080p). Switched to the concrete binary.NativeEndian (inlines to a word store) + per-row reslicing for bounds-check elimination. Endianness behaviour is identical (native ARGB for a same-machine compositor). - X11 EncodeRectInto/encodeRegion packed per pixel via pixel()+putValue() method calls with per-byte inner loops. Added a shared packRect with three byte-identical strategies chosen once at NewPresenter time: a zero-arithmetic fast path for the ubiquitous 32-bpp 8:8:8 TrueColor visual, a word-wise generic-shift path for other 32-bpp visuals, and the original per-channel path for non-32-bpp depths. Writes go through binary.Big/Little Endian keyed by the server image byte order (host-independent, s390x-safe). - Non-SHM PutImage fallback allocated an 8.3 MB wire buffer per frame; now reuses a persistent per-Presenter scratch (grows only when the frame grows) -> 0 allocs/present. Measured (Apple M4 Max, 1920x1080, -benchmem), before -> after: x11 EncodeRectInto full 7.50 ms -> 1.64 ms (4.6x), 0 allocs x11 EncodeRectInto 64x64 14.9 us -> 3.43 us (4.3x), 0 allocs x11 encodeRegion full (fb) 7.96 ms -> 1.66 ms + 8.3 MB/present -> 0 wl PackARGB8888 full 4.35 ms -> 1.09 ms (4.0x), 0 allocs wl PackARGB8888 64x64 8.43 us -> 2.22 us (3.8x), 0 allocs Committed Benchmark* (full-frame + damage-rect) added for both backends. internal/x11 + internal/wayland stay at 100% statement coverage; -race clean; CGO=0 cross-build clean on all six supported arches + darwin stub. No syscall usage touched (stays behind the existing linux gating). 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.
Performance audit of the per-frame present/pack path
Audit horodate: 2026-08-09 12:25 CEST. Measure-first: benchmarked before changing anything, fixed only proven costs, behaviour byte-identical.
Findings
Both live present paths were already zero-alloc per frame — X11 MIT-SHM packs directly into the mmap'd shared segment, Wayland directly into the wl_shm pool. The double-buffer swap allocates nothing. So the only remaining cost was CPU in the RGBA->wire conversion:
PackARGB8888wrote each pixel through thebinary.ByteOrderinterface (NativeOrder.PutUint32) — ~2M indirect calls/frame at 1080p. Now uses the concretebinary.NativeEndian(inlines to a word store) + per-row reslicing for BCE.EncodeRectInto/encodeRegionpacked per pixel viapixel()+putValue()method calls with per-byte inner loops. New sharedpackRectpicks one of three byte-identical strategies once atNewPresentertime: zero-arithmetic fast path for the ubiquitous 32-bpp 8:8:8 TrueColor visual, word-wise generic-shift path for other 32-bpp visuals, per-channel path for non-32-bpp depths. Writes keyed by server image byte order (host-independent; s390x-safe).Presenterscratch -> 0 allocs/present.Measured (Apple M4 Max, 1920x1080,
-benchmem), before -> afterCommitted
Benchmark*(full-frame + damage-rect) added for both backends.Conformance
internal/x11+internal/waylandstay at 100% statement coverage.-race(CGO=1) clean; whole module tests pass.🤖 Generated with Claude Code