Feat: optimizations

This commit is contained in:
2026-03-07 11:38:49 +01:00
parent 20d72c9b21
commit 3104a61490
7 changed files with 35 additions and 53 deletions

View File

@@ -30,6 +30,7 @@ pub mod transform;
use ratatui::style::Color; use ratatui::style::Color;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc;
/// Entry in the theme registry: id, display label, and palette constructor. /// Entry in the theme registry: id, display label, and palette constructor.
pub struct ThemeEntry { pub struct ThemeEntry {
@@ -66,17 +67,17 @@ pub const THEMES: &[ThemeEntry] = &[
]; ];
thread_local! { thread_local! {
static CURRENT_THEME: RefCell<ThemeColors> = RefCell::new(build::build(&(THEMES[0].palette)())); static CURRENT_THEME: RefCell<Rc<ThemeColors>> = RefCell::new(Rc::new(build::build(&(THEMES[0].palette)())));
} }
/// Return the current thread-local theme. /// Return the current thread-local theme (cheap Rc clone, not a deep copy).
pub fn get() -> ThemeColors { pub fn get() -> Rc<ThemeColors> {
CURRENT_THEME.with(|t| t.borrow().clone()) CURRENT_THEME.with(|t| Rc::clone(&t.borrow()))
} }
/// Set the current thread-local theme. /// Set the current thread-local theme.
pub fn set(theme: ThemeColors) { pub fn set(theme: ThemeColors) {
CURRENT_THEME.with(|t| *t.borrow_mut() = theme); CURRENT_THEME.with(|t| *t.borrow_mut() = Rc::new(theme));
} }
/// Complete set of resolved colors for all UI components. /// Complete set of resolved colors for all UI components.

View File

@@ -14,7 +14,7 @@ use arc_swap::ArcSwap;
use parking_lot::Mutex; use parking_lot::Mutex;
use rand::rngs::StdRng; use rand::rngs::StdRng;
use rand::SeedableRng; use rand::SeedableRng;
use std::collections::HashMap; use std::collections::{HashMap, HashSet};
use std::sync::{Arc, LazyLock}; use std::sync::{Arc, LazyLock};
use cagire_ratatui::CompletionCandidate; use cagire_ratatui::CompletionCandidate;
@@ -69,6 +69,7 @@ pub struct App {
pub sample_browser: Option<SampleBrowserState>, pub sample_browser: Option<SampleBrowserState>,
pub midi: MidiState, pub midi: MidiState,
pub plugin_mode: bool, pub plugin_mode: bool,
pub dict_keys: HashSet<String>,
} }
impl Default for App { impl Default for App {
@@ -126,6 +127,7 @@ impl App {
sample_browser: None, sample_browser: None,
midi: MidiState::new(), midi: MidiState::new(),
plugin_mode, plugin_mode,
dict_keys: HashSet::new(),
} }
} }

View File

@@ -149,7 +149,9 @@ impl App {
} }
let ctx = self.create_step_context(0, link); let ctx = self.create_step_context(0, link);
match self.script_engine.evaluate(prelude, &ctx) { match self.script_engine.evaluate(prelude, &ctx) {
Ok(_) => {} Ok(_) => {
self.dict_keys = self.dict.lock().keys().cloned().collect();
}
Err(e) => { Err(e) => {
let fallback = format!("Bank {}", bank + 1); let fallback = format!("Bank {}", bank + 1);
let bank_name = self.project_state.project.banks[bank] let bank_name = self.project_state.project.banks[bank]
@@ -202,6 +204,7 @@ impl App {
} }
} }
} }
self.dict_keys = self.dict.lock().keys().cloned().collect();
self.ui.flash("Preludes evaluated", 150, FlashKind::Info); self.ui.flash("Preludes evaluated", 150, FlashKind::Info);
} }

View File

@@ -264,10 +264,6 @@ use cpal::Stream;
use crossbeam_channel::{Receiver, Sender}; use crossbeam_channel::{Receiver, Sender};
#[cfg(feature = "cli")] #[cfg(feature = "cli")]
use doux::{Engine, EngineMetrics}; use doux::{Engine, EngineMetrics};
#[cfg(feature = "cli")]
use std::collections::VecDeque;
#[cfg(feature = "cli")]
use std::sync::Mutex;
#[cfg(feature = "cli")] #[cfg(feature = "cli")]
use super::AudioCommand; use super::AudioCommand;
@@ -360,8 +356,7 @@ pub fn build_stream(
let registry = Arc::clone(&engine.sample_registry); let registry = Arc::clone(&engine.sample_registry);
const INPUT_BUFFER_SIZE: usize = 8192; const INPUT_BUFFER_SIZE: usize = 8192;
let input_buffer: Arc<Mutex<VecDeque<f32>>> = let (input_producer, input_consumer) = HeapRb::<f32>::new(INPUT_BUFFER_SIZE).split();
Arc::new(Mutex::new(VecDeque::with_capacity(INPUT_BUFFER_SIZE)));
let input_device = config let input_device = config
.input_device .input_device
@@ -399,17 +394,12 @@ pub fn build_stream(
input_cfg.channels(), input_cfg.channels(),
input_cfg.sample_rate() input_cfg.sample_rate()
); );
let buf = Arc::clone(&input_buffer); let mut input_producer = input_producer;
let stream = dev let stream = dev
.build_input_stream( .build_input_stream(
&input_cfg.into(), &input_cfg.into(),
move |data: &[f32], _| { move |data: &[f32], _| {
let mut b = buf.lock().unwrap(); input_producer.push_slice(data);
b.extend(data.iter().copied());
let excess = b.len().saturating_sub(INPUT_BUFFER_SIZE);
if excess > 0 {
drop(b.drain(..excess));
}
}, },
{ {
let device_lost = Arc::clone(&device_lost); let device_lost = Arc::clone(&device_lost);
@@ -436,7 +426,7 @@ pub fn build_stream(
let mut cmd_buffer = String::with_capacity(256); let mut cmd_buffer = String::with_capacity(256);
let mut rt_set = false; let mut rt_set = false;
let mut live_scratch = vec![0.0f32; 4096]; let mut live_scratch = vec![0.0f32; 4096];
let input_buf_clone = Arc::clone(&input_buffer); let mut input_consumer = input_consumer;
let stream = device let stream = device
.build_output_stream( .build_output_stream(
@@ -491,29 +481,28 @@ pub fn build_stream(
if live_scratch.len() < stereo_len { if live_scratch.len() < stereo_len {
live_scratch.resize(stereo_len, 0.0); live_scratch.resize(stereo_len, 0.0);
} }
let mut buf = input_buf_clone.lock().unwrap();
match input_channels { match input_channels {
0 => { 0 => {
live_scratch[..stereo_len].fill(0.0); live_scratch[..stereo_len].fill(0.0);
} }
1 => { 1 => {
for i in 0..buffer_samples { for i in 0..buffer_samples {
let s = buf.pop_front().unwrap_or(0.0); let s = input_consumer.try_pop().unwrap_or(0.0);
live_scratch[i * 2] = s; live_scratch[i * 2] = s;
live_scratch[i * 2 + 1] = s; live_scratch[i * 2 + 1] = s;
} }
} }
2 => { 2 => {
for sample in &mut live_scratch[..stereo_len] { for sample in &mut live_scratch[..stereo_len] {
*sample = buf.pop_front().unwrap_or(0.0); *sample = input_consumer.try_pop().unwrap_or(0.0);
} }
} }
_ => { _ => {
for i in 0..buffer_samples { for i in 0..buffer_samples {
let l = buf.pop_front().unwrap_or(0.0); let l = input_consumer.try_pop().unwrap_or(0.0);
let r = buf.pop_front().unwrap_or(0.0); let r = input_consumer.try_pop().unwrap_or(0.0);
for _ in 2..input_channels { for _ in 2..input_channels {
buf.pop_front(); input_consumer.try_pop();
} }
live_scratch[i * 2] = l; live_scratch[i * 2] = l;
live_scratch[i * 2 + 1] = r; live_scratch[i * 2 + 1] = r;
@@ -521,11 +510,10 @@ pub fn build_stream(
} }
} }
// Discard excess if input produced more than we consumed // Discard excess if input produced more than we consumed
let excess = buf.len().saturating_sub(INPUT_BUFFER_SIZE / 2); let excess = input_consumer.occupied_len().saturating_sub(INPUT_BUFFER_SIZE / 2);
if excess > 0 { for _ in 0..excess {
drop(buf.drain(..excess)); input_consumer.try_pop();
} }
drop(buf);
engine.metrics.load.set_buffer_time(buffer_time_ns); engine.metrics.load.set_buffer_time(buffer_time_ns);
engine.process_block(data, &[], &live_scratch[..stereo_len]); engine.process_block(data, &[], &live_scratch[..stereo_len]);

View File

@@ -75,7 +75,6 @@ fn render_top_layout(
idx += 1; idx += 1;
} }
if has_preview { if has_preview {
let user_words: HashSet<String> = app.dict.lock().keys().cloned().collect();
let has_prelude = !app.project_state.project.prelude.trim().is_empty() let has_prelude = !app.project_state.project.prelude.trim().is_empty()
|| !app.project_state.project.banks[app.editor_ctx.bank] || !app.project_state.project.banks[app.editor_ctx.bank]
.prelude .prelude
@@ -84,10 +83,10 @@ fn render_top_layout(
if has_prelude { if has_prelude {
let [script_area, prelude_area] = let [script_area, prelude_area] =
Layout::horizontal([Constraint::Fill(1), Constraint::Fill(1)]).areas(areas[idx]); Layout::horizontal([Constraint::Fill(1), Constraint::Fill(1)]).areas(areas[idx]);
render_script_preview(frame, app, snapshot, &user_words, script_area); render_script_preview(frame, app, snapshot, &app.dict_keys, script_area);
render_prelude_preview(frame, app, &user_words, prelude_area); render_prelude_preview(frame, app, &app.dict_keys, prelude_area);
} else { } else {
render_script_preview(frame, app, snapshot, &user_words, areas[idx]); render_script_preview(frame, app, snapshot, &app.dict_keys, areas[idx]);
} }
idx += 1; idx += 1;
} }
@@ -186,19 +185,12 @@ fn render_viz_area(
Orientation::Horizontal Orientation::Horizontal
}; };
let user_words_once: Option<HashSet<String>> = if panels.iter().any(|p| matches!(p, VizPanel::Preview)) {
Some(app.dict.lock().keys().cloned().collect())
} else {
None
};
for (panel, panel_area) in panels.iter().zip(areas.iter()) { for (panel, panel_area) in panels.iter().zip(areas.iter()) {
match panel { match panel {
VizPanel::Scope => render_scope(frame, app, *panel_area, orientation), VizPanel::Scope => render_scope(frame, app, *panel_area, orientation),
VizPanel::Spectrum => render_spectrum(frame, app, *panel_area), VizPanel::Spectrum => render_spectrum(frame, app, *panel_area),
VizPanel::Lissajous => render_lissajous(frame, app, *panel_area), VizPanel::Lissajous => render_lissajous(frame, app, *panel_area),
VizPanel::Preview => { VizPanel::Preview => {
let user_words = user_words_once.as_ref().expect("user_words initialized");
let has_prelude = !app.project_state.project.prelude.trim().is_empty() let has_prelude = !app.project_state.project.prelude.trim().is_empty()
|| !app.project_state.project.banks[app.editor_ctx.bank] || !app.project_state.project.banks[app.editor_ctx.bank]
.prelude .prelude
@@ -212,10 +204,10 @@ fn render_viz_area(
Layout::horizontal([Constraint::Fill(1), Constraint::Fill(1)]) Layout::horizontal([Constraint::Fill(1), Constraint::Fill(1)])
.areas(*panel_area) .areas(*panel_area)
}; };
render_script_preview(frame, app, snapshot, user_words, script_area); render_script_preview(frame, app, snapshot, &app.dict_keys, script_area);
render_prelude_preview(frame, app, user_words, prelude_area); render_prelude_preview(frame, app, &app.dict_keys, prelude_area);
} else { } else {
render_script_preview(frame, app, snapshot, user_words, *panel_area); render_script_preview(frame, app, snapshot, &app.dict_keys, *panel_area);
} }
} }
} }

View File

@@ -675,8 +675,7 @@ fn render_modal(
.render_centered(frame, term) .render_centered(frame, term)
} }
Modal::Editor => { Modal::Editor => {
let user_words: HashSet<String> = app.dict.lock().keys().cloned().collect(); render_modal_editor(frame, app, snapshot, &app.dict_keys, term)
render_modal_editor(frame, app, snapshot, &user_words, term)
} }
Modal::PatternProps { Modal::PatternProps {
bank, bank,

View File

@@ -1,5 +1,3 @@
use std::collections::HashSet;
use ratatui::layout::{Alignment, Constraint, Layout, Rect}; use ratatui::layout::{Alignment, Constraint, Layout, Rect};
use ratatui::style::Style; use ratatui::style::Style;
use ratatui::widgets::{Block, Borders, Paragraph}; use ratatui::widgets::{Block, Borders, Paragraph};
@@ -47,7 +45,7 @@ fn render_editor(frame: &mut Frame, app: &App, snapshot: &SequencerSnapshot, are
let editor_area = Rect::new(inner.x, inner.y, inner.width, editor_height); let editor_area = Rect::new(inner.x, inner.y, inner.width, editor_height);
let hint_area = Rect::new(inner.x, inner.y + editor_height, inner.width, 1); let hint_area = Rect::new(inner.x, inner.y + editor_height, inner.width, 1);
let user_words: HashSet<String> = app.dict.lock().keys().cloned().collect(); let user_words = &app.dict_keys;
let trace = if app.ui.runtime_highlight && app.playback.playing { let trace = if app.ui.runtime_highlight && app.playback.playing {
snapshot.script_trace() snapshot.script_trace()
@@ -77,7 +75,7 @@ fn render_editor(frame: &mut Frame, app: &App, snapshot: &SequencerSnapshot, are
), ),
None => (Vec::new(), Vec::new(), Vec::new()), None => (Vec::new(), Vec::new(), Vec::new()),
}; };
highlight::highlight_line_with_runtime(line, &exec, &sel, &res, &user_words) highlight::highlight_line_with_runtime(line, &exec, &sel, &res, user_words)
}; };
app.script_editor.editor.render(frame, editor_area, &highlighter); app.script_editor.editor.render(frame, editor_area, &highlighter);
@@ -142,7 +140,6 @@ fn render_sidebar(frame: &mut Frame, app: &App, area: Rect) {
idx += 1; idx += 1;
} }
if has_prelude { if has_prelude {
let user_words: HashSet<String> = app.dict.lock().keys().cloned().collect(); super::main_view::render_prelude_preview(frame, app, &app.dict_keys, areas[idx]);
super::main_view::render_prelude_preview(frame, app, &user_words, areas[idx]);
} }
} }