Smoother on Xorg and Live Starfield: draw one frame ahead, cache a backdrop's unchanging start
CI / test (push) Successful in 2m33s

Tested on a Vast GTX 1650 rig (8-core Xeon E5-2620 v4, NVIDIA Xorg, --x11 --fullscreen):
GPU detection says hardware (nvidia), CHANDRA_RENDER=software switches.

- Frame pacing: at most one frame is drawn ahead of the output instead of waiting until the
  output is ready. Waiting serialised drawing with the X server's copy of the last frame; overview
  on Xorg went from 24-27 fps to 41-48 fps (before the first round: 16-17 fps).
- MoonBit host: a backdrop whose ops change every frame but begin the same (a desktop module:
  Live Starfield's sky and nebulae under moving stars) keeps that common start as cached pixels
  and draws only the rest over a copy of it. Starfield raster 13 -> 1.7 ms a frame and CPU 46% ->
  11% in the cloud; it no longer clears, redraws and re-saves the whole backdrop every frame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hkTQp5uN5FtwpzrXUA
This commit is contained in:
Claude committed 2026-11-26 03:15:55 +00:00
1 parent 1154437050
commit 02c831576a
3 files changed
+74 -16

No files matched your search

+59 -6
View File
@@ -528,8 +528,33 @@ struct Frame {
///| The last frame drawn, and the wallpaper ops the cached backdrop was drawn from.
let prev_frame : Ref[Frame?] = Ref(None)
///| The backdrop ops last sent (`wallpaper`, None before any), and how many of them, from the
/// start, the cached backdrop pixels hold. Usually all of them; when the ops change every frame
/// but begin the same (a desktop module such as Live Starfield: the sky and nebulae stay, the stars
/// move), only that common start is kept, and the rest is drawn over a copy of it each frame.
let backdrop_ops : Ref[Array[@wt.Val]?] = Ref(None)
///|
let backdrop_ops : Ref[@wt.Val?] = Ref(None)
let backdrop_kept : Ref[Int] = Ref(0)
///| How many ops `a` and `b` share from the start.
fn common_prefix(a : Array[@wt.Val], b : Array[@wt.Val]) -> Int {
let n = @cmp.minimum(a.length(), b.length())
let mut i = 0
while i < n && a[i] == b[i] {
i = i + 1
}
i
}
///| The backdrop ops past what the cached pixels hold, drawn over them (within `clip`).
fn draw_backdrop_rest(clip : R?) -> Unit {
match backdrop_ops.val {
Some(ops) if backdrop_kept.val < ops.length() =>
draw_ops(ops[backdrop_kept.val:].to_array(), 0, 0, clip)
_ => ()
}
}
///| Room around a window for its drop shadow (4 px right, 6 px down) and a little slack.
const SHADOW_PAD : Int = 8
@@ -668,7 +693,7 @@ fn submit(v : @wt.Val) -> Unit {
let backdrop_ready = rs_has_backdrop() != 0 &&
(match (f.wallpaper, backdrop_ops.val) {
(None, Some(_)) => true
(Some(ops), Some(o)) => o == ops
(Some(ops), Some(o)) => o == ops.to_list()
_ => false
})
let clip : R? = match prev_frame.val {
@@ -688,7 +713,8 @@ fn submit(v : @wt.Val) -> Unit {
}
_ => None
}
if !resized && !backdrop_ready {
// (A new backdrop clears the buffer itself, below, unless its kept start can be copied in.)
if !resized && !backdrop_ready && f.wallpaper is None {
rs_clear()
}
last_drawn.val = match clip {
@@ -718,15 +744,42 @@ fn submit(v : @wt.Val) -> Unit {
if backdrop_ready {
set_clip(clip)
rs_copy_backdrop()
draw_backdrop_rest(clip)
} else {
draw_ops(ops.to_list(), 0, 0, None)
rs_save_backdrop()
backdrop_ops.val = Some(ops)
let ops = ops.to_list()
let kept = backdrop_kept.val
match backdrop_ops.val {
Some(old) if rs_has_backdrop() != 0 && kept > 0 && common_prefix(old, ops) >= kept => {
// The cached start still holds: copy it, draw the rest.
set_clip(None)
rs_copy_backdrop()
backdrop_ops.val = Some(ops)
draw_backdrop_rest(None)
}
old => {
// Keep only the start these ops share with the last ones when they share one (they
// will likely change again next frame), else all of them.
let shared = match old {
Some(o) => common_prefix(o, ops)
None => 0
}
let keep = if shared > 0 && shared < ops.length() { shared } else { ops.length() }
if !resized {
rs_clear()
}
draw_ops(ops[:keep].to_array(), 0, 0, None)
rs_save_backdrop()
backdrop_ops.val = Some(ops)
backdrop_kept.val = keep
draw_backdrop_rest(None)
}
}
}
None =>
if rs_has_backdrop() != 0 && !occluded {
set_clip(clip)
rs_copy_backdrop()
draw_backdrop_rest(clip)
}
}
if !occluded {