Try to optimize

This commit is contained in:
2026-01-29 11:53:47 +01:00
parent 4d0d837e14
commit 845c1134fe
14 changed files with 764 additions and 214 deletions

View File

@@ -1,4 +1,6 @@
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::{Arc, Mutex};
use std::time::Instant;
@@ -15,7 +17,7 @@ use crate::app::App;
use crate::engine::{LinkState, SequencerSnapshot};
use crate::model::{SourceSpan, StepContext, Value};
use crate::page::Page;
use crate::state::{FlashKind, Modal, PanelFocus, PatternField, SidePanel};
use crate::state::{FlashKind, Modal, PanelFocus, PatternField, SidePanel, StackCache};
use crate::views::highlight::{self, highlight_line, highlight_line_with_runtime};
use crate::widgets::{
ConfirmModal, ModalFrame, NavMinimap, NavTile, SampleBrowser, TextInputModal,
@@ -25,43 +27,67 @@ use super::{
dict_view, engine_view, help_view, main_view, options_view, patterns_view, title_view,
};
fn compute_stack_display(lines: &[String], editor: &cagire_ratatui::Editor) -> String {
fn compute_stack_display(lines: &[String], editor: &cagire_ratatui::Editor, cache: &std::cell::RefCell<Option<StackCache>>) -> String {
let cursor_line = editor.cursor().0;
let mut hasher = DefaultHasher::new();
for (i, line) in lines.iter().enumerate() {
if i > cursor_line {
break;
}
line.hash(&mut hasher);
}
let lines_hash = hasher.finish();
if let Some(ref c) = *cache.borrow() {
if c.cursor_line == cursor_line && c.lines_hash == lines_hash {
return c.result.clone();
}
}
let partial: Vec<&str> = lines.iter().take(cursor_line + 1).map(|s| s.as_str()).collect();
let script = partial.join("\n");
if script.trim().is_empty() {
return "Stack: []".to_string();
}
let result = if script.trim().is_empty() {
"Stack: []".to_string()
} else {
let vars = Arc::new(Mutex::new(HashMap::new()));
let dict = Arc::new(Mutex::new(HashMap::new()));
let rng = Arc::new(Mutex::new(StdRng::seed_from_u64(42)));
let forth = Forth::new(vars, dict, rng);
let vars = Arc::new(Mutex::new(HashMap::new()));
let dict = Arc::new(Mutex::new(HashMap::new()));
let rng = Arc::new(Mutex::new(StdRng::seed_from_u64(42)));
let forth = Forth::new(vars, dict, rng);
let ctx = StepContext {
step: 0,
beat: 0.0,
bank: 0,
pattern: 0,
tempo: 120.0,
phase: 0.0,
slot: 0,
runs: 0,
iter: 0,
speed: 1.0,
fill: false,
nudge_secs: 0.0,
};
let ctx = StepContext {
step: 0,
beat: 0.0,
bank: 0,
pattern: 0,
tempo: 120.0,
phase: 0.0,
slot: 0,
runs: 0,
iter: 0,
speed: 1.0,
fill: false,
nudge_secs: 0.0,
match forth.evaluate(&script, &ctx) {
Ok(_) => {
let stack = forth.stack();
let formatted: Vec<String> = stack.iter().map(format_value).collect();
format!("Stack: [{}]", formatted.join(" "))
}
Err(e) => format!("Error: {e}"),
}
};
match forth.evaluate(&script, &ctx) {
Ok(_) => {
let stack = forth.stack();
let formatted: Vec<String> = stack.iter().map(format_value).collect();
format!("Stack: [{}]", formatted.join(" "))
}
Err(e) => format!("Error: {e}"),
}
*cache.borrow_mut() = Some(StackCache {
cursor_line,
lines_hash,
result: result.clone(),
});
result
}
fn format_value(v: &Value) -> String {
@@ -740,13 +766,13 @@ fn render_modal(frame: &mut Frame, app: &App, snapshot: &SequencerSnapshot, term
]);
frame.render_widget(Paragraph::new(hint).alignment(Alignment::Right), hint_area);
} else if app.editor_ctx.show_stack {
let stack_text = compute_stack_display(text_lines, &app.editor_ctx.editor);
let stack_text = compute_stack_display(text_lines, &app.editor_ctx.editor, &app.editor_ctx.stack_cache);
let hint = Line::from(vec![
Span::styled("Esc", key),
Span::styled(" save ", dim),
Span::styled("C-e", key),
Span::styled(" eval ", dim),
Span::styled("C-k", key),
Span::styled("C-s", key),
Span::styled(" hide", dim),
]);
let [hint_left, stack_right] = Layout::horizontal([
@@ -767,7 +793,7 @@ fn render_modal(frame: &mut Frame, app: &App, snapshot: &SequencerSnapshot, term
Span::styled(" eval ", dim),
Span::styled("C-f", key),
Span::styled(" find ", dim),
Span::styled("C-k", key),
Span::styled("C-s", key),
Span::styled(" stack ", dim),
Span::styled("C-u", key),
Span::styled("/", dim),