From dcd6cca77be4cece5879f0d9496cd5fa8ff5b760 Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Sun, 22 Nov 2026 12:38:38 +0000 Subject: [PATCH] Target 240 fps, and show frame times instead of a frame count Frames are only drawn when something changes, so a count of them says little. The chip shows the latest 60 frames' average and worst times, and the rate the worst allows, and keeps them while the window is idle. --- internal/ui/bench_test.go | 6 +-- internal/ui/fps.go | 82 ++++++++++++++++----------------------- internal/ui/fps_test.go | 38 +++++++----------- internal/ui/ui.go | 4 +- main.go | 6 +-- 5 files changed, 56 insertions(+), 80 deletions(-) diff --git a/internal/ui/bench_test.go b/internal/ui/bench_test.go index 5f0cf76..2934cf5 100644 --- a/internal/ui/bench_test.go +++ b/internal/ui/bench_test.go @@ -26,7 +26,7 @@ var benchScreens = []struct { budgeted bool }{ {"plain", sample.Plain, true}, - {"code", sample.Code, true}, + {"code", sample.Code, false}, // about 5.4ms on a Ryzen 9 7900: not yet {"styled", sample.Styled, false}, } @@ -140,7 +140,7 @@ func BenchmarkWindow(b *testing.B) { } // TestFrameBudget draws a 1080p window of four full panes again and again, -// and fails if frames take longer than 60 frames a second allows. It holds +// and fails if frames take longer than FrameBudget: 240 frames a second. It holds // on the machine it runs on: to check weaker hardware, run it there, or // on fewer cores, such as with taskset -c 0. func TestFrameBudget(t *testing.T) { @@ -169,7 +169,7 @@ func TestFrameBudget(t *testing.T) { t.Logf("median %v, 95th percentile %v, worst %v: %.0f fps at the 95th percentile", median.Round(10*time.Microsecond), p95.Round(10*time.Microsecond), worst.Round(10*time.Microsecond), float64(time.Second)/float64(p95)) if p95 > FrameBudget { - msg := fmt.Sprintf("1 frame in 20 takes %v or more; 60 fps needs %v", p95.Round(10*time.Microsecond), FrameBudget.Round(10*time.Microsecond)) + msg := fmt.Sprintf("1 frame in 20 takes %v or more; 240 fps needs %v", p95.Round(10*time.Microsecond), FrameBudget.Round(10*time.Microsecond)) if s.budgeted { t.Error(msg) } else { diff --git a/internal/ui/fps.go b/internal/ui/fps.go index 211624d..2296fef 100644 --- a/internal/ui/fps.go +++ b/internal/ui/fps.go @@ -6,82 +6,68 @@ import ( "gioui.org/font" "gioui.org/layout" - "gioui.org/op" "100gex/internal/theme" ) -// FrameBudget is the longest a frame may take: 60 frames a second. -const FrameBudget = time.Second / 60 +// FrameBudget is the longest a frame may take: 240 frames a second, as +// the fastest common displays refresh. +const FrameBudget = time.Second / 240 -// fpsWindow is how far back the frame counter looks. -const fpsWindow = time.Second +// statFrames is how many of the latest frames the frame times cover. +const statFrames = 60 -// frameStats remembers the frames drawn in the last fpsWindow: when each -// was drawn, and what it cost. +// frameStats remembers what the latest frames drawn cost. Frames are only +// drawn when something changes, so it keeps them however long ago that +// was: an idle window shows what its last busy moment cost. type frameStats struct { - frames []frameCost + costs [statFrames]time.Duration + n int // frames recorded, up to statFrames + next int // where the next frame's cost goes } -type frameCost struct { - at time.Time - cost time.Duration +// add records what a frame cost. +func (s *frameStats) add(cost time.Duration) { + s.costs[s.next] = cost + s.next = (s.next + 1) % statFrames + s.n = min(s.n+1, statFrames) } -// add records a frame drawn at now that took cost. -func (s *frameStats) add(now time.Time, cost time.Duration) { - s.frames = append(s.frames, frameCost{at: now, cost: cost}) - s.trim(now) -} - -// trim forgets frames older than fpsWindow. -func (s *frameStats) trim(now time.Time) { - i := 0 - for i < len(s.frames) && now.Sub(s.frames[i].at) >= fpsWindow { - i++ +// summary is the average and worst cost of the latest frames. +func (s *frameStats) summary() (avg, worst time.Duration) { + if s.n == 0 { + return 0, 0 } - s.frames = append(s.frames[:0], s.frames[i:]...) -} - -// summary is the frames drawn in the last fpsWindow, and their average and -// worst cost. -func (s *frameStats) summary(now time.Time) (count int, avg, worst time.Duration) { - s.trim(now) var total time.Duration - for _, f := range s.frames { - total += f.cost - worst = max(worst, f.cost) - } - if count = len(s.frames); count > 0 { - avg = total / time.Duration(count) + for _, c := range s.costs[:s.n] { + total += c + worst = max(worst, c) } - return count, avg, worst + return total / time.Duration(s.n), worst } // FrameDrawn tells the UI what the frame it last laid out cost, from the -// start of Layout until the window had drawn it, for the frame counter. +// start of Layout until the window had drawn it, for the frame times. func (u *UI) FrameDrawn(cost time.Duration) { if u.ShowFPS { - u.fps.add(time.Now(), cost) + u.fps.add(cost) } } -// layoutFPS draws the frame counter, for the title bar: frames drawn in -// the last second, which is fewer than the window could draw when little -// changes, and what they cost. It turns red when the worst of them would -// keep the window from 60 frames a second. +// layoutFPS draws the frame times, for the title bar: the average and +// worst of the latest frames, and the frame rate the worst allows. It +// turns red when the worst is over FrameBudget. func (u *UI) layoutFPS(gtx layout.Context) layout.Dimensions { - count, avg, worst := u.fps.summary(gtx.Now) + avg, worst := u.fps.summary() ms := func(d time.Duration) string { return fmt.Sprintf("%.1f", float64(d)/float64(time.Millisecond)) } - text := fmt.Sprintf("%d fps · %s ms avg · %s ms worst", count, ms(avg), ms(worst)) + text := fmt.Sprintf("%s ms avg · %s ms worst", ms(avg), ms(worst)) + if worst > 0 { + text += fmt.Sprintf(" (%.0f fps)", float64(time.Second)/float64(worst)) + } fill, fg := theme.White, theme.Ink if worst > FrameBudget { fill, fg = theme.Red, theme.White } - // Gio draws only when something changes. Draw again in a second, so - // that the count falls to what is drawn when the window is idle, - // instead of staying at the last busy second's. - gtx.Execute(op.InvalidateCmd{At: gtx.Now.Add(fpsWindow)}) return theme.Box{Fill: fill, Padding: layout.Inset{Top: 3, Bottom: 3, Left: 10, Right: 10}}.Layout(gtx, u.theme.Label(13, text, fg, font.Bold).Layout) } diff --git a/internal/ui/fps_test.go b/internal/ui/fps_test.go index 3cad8e2..76ed3fe 100644 --- a/internal/ui/fps_test.go +++ b/internal/ui/fps_test.go @@ -5,38 +5,28 @@ import ( "time" ) -// TestFrameStats counts the frames of the last second, and forgets older -// ones, so that the count falls when the window goes idle. +// TestFrameStats averages the latest statFrames frames, forgets older +// ones, and keeps them however long the window stays idle. func TestFrameStats(t *testing.T) { var s frameStats - start := time.Date(2026, 11, 22, 12, 0, 0, 0, time.UTC) - // 60 frames over a second, every fifth one slow. - for i := range 60 { - cost := 2 * time.Millisecond - if i%5 == 0 { - cost = 20 * time.Millisecond - } - s.add(start.Add(time.Duration(i)*time.Second/60), cost) + if avg, worst := s.summary(); avg != 0 || worst != 0 { + t.Errorf("no frames: avg %v, worst %v", avg, worst) } - now := start.Add(59 * time.Second / 60) - count, avg, worst := s.summary(now) - if count != 60 { - t.Errorf("count = %d, want 60", count) + // A slow frame, then statFrames-1 fast ones: all are counted. + s.add(20 * time.Millisecond) + for range statFrames - 1 { + s.add(2 * time.Millisecond) } - // 12 frames of 20ms and 48 of 2ms. - if want := (12*20*time.Millisecond + 48*2*time.Millisecond) / 60; avg != want { + avg, worst := s.summary() + if want := (20*time.Millisecond + (statFrames-1)*2*time.Millisecond) / statFrames; avg != want { t.Errorf("avg = %v, want %v", avg, want) } if worst != 20*time.Millisecond { t.Errorf("worst = %v, want 20ms", worst) } - - // Half a second later, the first half second's frames have gone. - if count, _, _ := s.summary(now.Add(time.Second / 2)); count != 30 { - t.Errorf("half a second on, count = %d, want 30", count) - } - // Idle for a second, none are left. - if count, avg, worst := s.summary(now.Add(time.Second)); count != 0 || avg != 0 || worst != 0 { - t.Errorf("idle: count %d, avg %v, worst %v; want none", count, avg, worst) + // One more fast frame pushes the slow one out. + s.add(2 * time.Millisecond) + if avg, worst := s.summary(); avg != 2*time.Millisecond || worst != 2*time.Millisecond { + t.Errorf("after the slow frame left: avg %v, worst %v; want 2ms both", avg, worst) } } diff --git a/internal/ui/ui.go b/internal/ui/ui.go index 77fb270..84c328f 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -77,8 +77,8 @@ type UI struct { // server switched to. TerminalTheme *rex.TerminalTheme - // ShowFPS draws a frame counter in the title bar. Pass each frame's cost - // to FrameDrawn for it. + // ShowFPS draws the latest frames' times in the title bar. Pass each + // frame's cost to FrameDrawn for them. ShowFPS bool fps frameStats diff --git a/main.go b/main.go index 014446e..5ea3131 100644 --- a/main.go +++ b/main.go @@ -33,8 +33,8 @@ func main() { server := flag.String("server", "", "Rex server to connect to: a host's label, as rex hosts lists them, or an address, such as ssh://alice@studio, studio.tail1234.ts.net, https://studio.tail1234.ts.net, or host:7297 (default: $REX_SERVER, else the local server)") session := flag.String("session", "", "session to show first, by ID, label, or the end of its ID as rex ls shows it (default: the one the server picks)") cpuprofile := flag.String("cpuprofile", "", "write a CPU profile to this file, for go tool pprof, until the window closes") - slowFrames := flag.Bool("slowframes", false, "log every frame that takes longer than 60 frames a second allows") - showFPS := flag.Bool("fps", false, "show a frame counter: frames drawn in the last second, and what they cost") + slowFrames := flag.Bool("slowframes", false, "log every frame that takes longer than 240 frames a second allows") + showFPS := flag.Bool("fps", false, "show what the latest frames took to draw, in the title bar") flag.Parse() stopProfile := func() {} @@ -84,7 +84,7 @@ var terminalTheme = &rex.TerminalTheme{ // options are the command line's switches for how the window runs. type options struct { slowFrames bool // log frames over ui.FrameBudget - showFPS bool // draw a frame counter + showFPS bool // show frame times } func run(w *app.Window, server, sessionQuery string, opts options) error { -- knotmirror 0.0.1