From c511afdad1f3d2794e221ca4579807dcdbdc18fc Mon Sep 17 00:00:00 2001 From: liangsheng12333 Date: Sun, 22 Nov 2026 00:45:37 +0800 Subject: [PATCH 1/2] perf(scopes): avoid cloning full-frame samples on draw (#81) --- crates/oak-app/src/panels/program_viewer.rs | 25 +++++++------ .../src/panels/program_viewer/tests.rs | 37 ++++++++++++++++++- crates/oak-app/src/widgets/scopes/mod.rs | 27 +++++++++----- 3 files changed, 66 insertions(+), 23 deletions(-) diff --git a/crates/oak-app/src/panels/program_viewer.rs b/crates/oak-app/src/panels/program_viewer.rs index 42b88e1657..ba23d8f13d 100644 --- a/crates/oak-app/src/panels/program_viewer.rs +++ b/crates/oak-app/src/panels/program_viewer.rs @@ -43,6 +43,7 @@ use crate::oakui::timecode::{format_fps, format_resolution}; use crate::oakui::{AppEngine, Monitor}; use crate::panels::commands::{self as panel_commands, PanelCommandHandler}; use crate::panels::{chip, viewer_title}; +use std::sync::Arc; /// Width of the audio level strip, per the design (26px). const METER_WIDTH: f32 = 26.0; @@ -64,15 +65,15 @@ struct ScopeState { width: u32, /// Frame height in pixels. height: u32, - /// Per-pixel luma of the current frame (`0..=1`). - luma: Vec, - /// Per-pixel chroma `(Cb, Cr)` of the current frame (`0..=1`). - chroma: Vec<(f32, f32)>, + /// Per-pixel luma of the current frame (`0..=1`), shared via [`Arc`]. + luma: Arc>, + /// Per-pixel chroma `(Cb, Cr)` of the current frame (`0..=1`), shared via [`Arc`]. + chroma: Arc>, } impl LumaDataSource for ScopeState { - fn luma_samples(&self) -> Vec { - self.luma.clone() + fn luma_samples(&self) -> &[f32] { + &self.luma } fn luma_dimensions(&self) -> (usize, usize) { @@ -81,8 +82,8 @@ impl LumaDataSource for ScopeState { } impl ChromaDataSource for ScopeState { - fn chroma_samples(&self) -> Vec<(f32, f32)> { - self.chroma.clone() + fn chroma_samples(&self) -> &[(f32, f32)] { + &self.chroma } } @@ -232,8 +233,8 @@ impl ProgramViewerPanel { let scope_state = cx.new(|_cx| ScopeState { width: 0, height: 0, - luma: Vec::new(), - chroma: Vec::new(), + luma: Arc::default(), + chroma: Arc::default(), }); let histogram = cx.new(|cx| Histogram::new(41, scope_state.clone(), window, cx)); let waveform = cx.new(|cx| Waveform::new(42, scope_state.clone(), window, cx)); @@ -405,8 +406,8 @@ impl ProgramViewerPanel { self.scope_state.update(cx, |state, cx| { state.width = scope.width; state.height = scope.height; - state.luma = (*scope.luma).clone(); - state.chroma = (*scope.chroma).clone(); + state.luma = scope.luma; + state.chroma = scope.chroma; cx.notify(); }); } diff --git a/crates/oak-app/src/panels/program_viewer/tests.rs b/crates/oak-app/src/panels/program_viewer/tests.rs index 2f784d3eb0..8333c43ba2 100644 --- a/crates/oak-app/src/panels/program_viewer/tests.rs +++ b/crates/oak-app/src/panels/program_viewer/tests.rs @@ -5,6 +5,8 @@ use crate::oakui::graphops; use crate::widgets::timeline::Frame; use crate::widgets::viewer::PlaybackClock; use gpui_kit::{Keystroke, Modifiers, Rgba, TestAppContext, VisualTestContext, size}; +use std::ptr; +use std::sync::Arc; /// Serializes the tests that mutate process-global state (HOME, the /// debug env var) and the undo stack. @@ -378,14 +380,32 @@ async fn scopes_tab_renders_from_the_current_frame(cx: &mut TestAppContext) { window.draw(cx).clear(cx); }); - let (scope_width, scope_height, luma_len, chroma_len, bins, envelope) = cx.read(|app| { + let ( + scope_width, + scope_height, + luma_len, + chroma_len, + luma_arc, + chroma_arc, + engine_scope, + luma_ptrs_match, + chroma_ptrs_match, + bins, + envelope, + ) = cx.read(|app| { let panel = panel.read(app); let state = panel.scope_state.read(app); + let engine_scope = panel.engine.read(app).scope_data(Monitor::Program, app); ( state.width, state.height, state.luma.len(), state.chroma.len(), + state.luma.clone(), + state.chroma.clone(), + engine_scope, + ptr::eq(state.luma_samples().as_ptr(), state.luma.as_ptr()), + ptr::eq(state.chroma_samples().as_ptr(), state.chroma.as_ptr()), panel.histogram.read(app).bins(app), panel.waveform.read(app).envelope(app), ) @@ -396,6 +416,21 @@ async fn scopes_tab_renders_from_the_current_frame(cx: &mut TestAppContext) { assert_eq!(scope_height, crate::oakui::frames::SYNTH_FRAME_HEIGHT); assert_eq!(luma_len, pixels); assert_eq!(chroma_len, pixels); + assert!(Arc::ptr_eq(&luma_arc, &engine_scope.luma)); + assert!(Arc::ptr_eq(&chroma_arc, &engine_scope.chroma)); + assert!(luma_ptrs_match); + assert!(chroma_ptrs_match); assert_eq!(bins.iter().sum::() as usize, pixels); assert_eq!(envelope.len(), 128); + + // Redrawing while paused must keep sharing the exact same buffers without + // allocating or copying samples. + cx.update(|window, cx| { + window.draw(cx).clear(cx); + }); + cx.read(|app| { + let state = panel.read(app).scope_state.read(app); + assert!(Arc::ptr_eq(&state.luma, &luma_arc)); + assert!(Arc::ptr_eq(&state.chroma, &chroma_arc)); + }); } diff --git a/crates/oak-app/src/widgets/scopes/mod.rs b/crates/oak-app/src/widgets/scopes/mod.rs index fae6a992e3..dfec51cf12 100644 --- a/crates/oak-app/src/widgets/scopes/mod.rs +++ b/crates/oak-app/src/widgets/scopes/mod.rs @@ -17,7 +17,7 @@ use gpui_kit::{ /// Provides luma samples (`0..1`) for the histogram and waveform scopes. pub trait LumaDataSource: 'static { /// Luma samples for the current frame, row-major. - fn luma_samples(&self) -> Vec; + fn luma_samples(&self) -> &[f32]; /// Dimensions `(width, height)` of the row-major frame in [`Self::luma_samples`]. fn luma_dimensions(&self) -> (usize, usize); @@ -26,7 +26,7 @@ pub trait LumaDataSource: 'static { /// Provides chroma samples for the vectorscope. pub trait ChromaDataSource: 'static { /// `(u, v)` samples in `0..1` (centered on `0.5`). - fn chroma_samples(&self) -> Vec<(f32, f32)>; + fn chroma_samples(&self) -> &[(f32, f32)]; } /// A luminance histogram. @@ -63,7 +63,7 @@ impl Histogram { /// than an empty vector, so the render pass and hosts can index it /// directly. Also used by tests to assert the mapping. pub fn bins(&self, cx: &App) -> Vec { - histogram_bins(&self.data.read(cx).luma_samples(), 64) + histogram_bins(self.data.read(cx).luma_samples(), 64) } } @@ -136,7 +136,7 @@ impl Waveform { pub fn envelope(&self, cx: &App) -> Vec<(f32, f32)> { let data = self.data.read(cx); let (width, height) = data.luma_dimensions(); - waveform_envelope(&data.luma_samples(), width, height, 128) + waveform_envelope(data.luma_samples(), width, height, 128) } } @@ -207,7 +207,7 @@ impl Vectorscope { /// subtracting the 0.5 center and clamping; the render pass maps these /// onto the canvas. Empty when the source has no samples. pub fn points(&self, cx: &App) -> Vec<(f32, f32)> { - vectorscope_points(&self.data.read(cx).chroma_samples()) + vectorscope_points(self.data.read(cx).chroma_samples()) } } @@ -264,6 +264,7 @@ impl Render for Vectorscope { mod tests { use super::*; use gpui_kit::{Entity, Render, TestAppContext, Window, div, px, size}; + use std::ptr; struct MockLuma { width: usize, @@ -271,8 +272,8 @@ mod tests { samples: Vec, } impl LumaDataSource for MockLuma { - fn luma_samples(&self) -> Vec { - self.samples.clone() + fn luma_samples(&self) -> &[f32] { + &self.samples } fn luma_dimensions(&self) -> (usize, usize) { @@ -281,8 +282,8 @@ mod tests { } struct MockChroma(Vec<(f32, f32)>); impl ChromaDataSource for MockChroma { - fn chroma_samples(&self) -> Vec<(f32, f32)> { - self.0.clone() + fn chroma_samples(&self) -> &[(f32, f32)] { + &self.0 } } @@ -330,14 +331,20 @@ mod tests { window.draw(cx).clear(cx); }); - let (bins, envelope, points) = cx.read(|app| { + let (bins, envelope, points, luma_ptrs_match, chroma_ptrs_match) = cx.read(|app| { let host = host.read(app); + let luma_ref = luma.read(app); + let chroma_ref = chroma.read(app); ( host.histogram.read(app).bins(app), host.waveform.read(app).envelope(app), host.vectorscope.read(app).points(app), + ptr::eq(luma_ref.luma_samples().as_ptr(), luma_ref.samples.as_ptr()), + ptr::eq(chroma_ref.chroma_samples().as_ptr(), chroma_ref.0.as_ptr()), ) }); + assert!(luma_ptrs_match); + assert!(chroma_ptrs_match); assert_eq!(bins.len(), 64); let total: u32 = bins.iter().sum(); assert_eq!(total, 100); From 0b63c6e284c6ae43876884f5233fe1f1562bcdd2 Mon Sep 17 00:00:00 2001 From: liangsheng12333 Date: Mon, 23 Nov 2026 17:12:21 +0800 Subject: [PATCH 2/2] test(scopes): read mock sources via Host widget entities --- crates/oak-app/src/widgets/scopes/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/oak-app/src/widgets/scopes/mod.rs b/crates/oak-app/src/widgets/scopes/mod.rs index dfec51cf12..81ba8fd4ee 100644 --- a/crates/oak-app/src/widgets/scopes/mod.rs +++ b/crates/oak-app/src/widgets/scopes/mod.rs @@ -333,8 +333,8 @@ mod tests { let (bins, envelope, points, luma_ptrs_match, chroma_ptrs_match) = cx.read(|app| { let host = host.read(app); - let luma_ref = luma.read(app); - let chroma_ref = chroma.read(app); + let luma_ref = host.histogram.read(app).data.read(app); + let chroma_ref = host.vectorscope.read(app).data.read(app); ( host.histogram.read(app).bins(app), host.waveform.read(app).envelope(app),