Feat: optimizations

This commit is contained in:
2026-02-05 23:15:46 +01:00
parent 2c98a915fa
commit 51f52be4ce
22 changed files with 175 additions and 215 deletions

View File

@@ -22,7 +22,7 @@ pub struct Forth {
impl Forth {
pub fn new(vars: Variables, dict: Dictionary, rng: Rng) -> Self {
Self {
stack: Arc::new(Mutex::new(Vec::new())),
stack: Mutex::new(Vec::new()),
vars,
dict,
rng,
@@ -227,7 +227,7 @@ impl Forth {
Some(v) => Some(v.as_str()?.to_string()),
None => None,
};
let resolved_params: Vec<(String, String)> = params
let resolved_params: Vec<(&str, String)> = params
.iter()
.map(|(k, v)| {
let resolved = resolve_cycling(v, emit_idx);
@@ -238,7 +238,7 @@ impl Forth {
}
}
}
(k.clone(), resolved.to_param_string())
(*k, resolved.to_param_string())
})
.collect();
emit_output(
@@ -555,7 +555,7 @@ impl Forth {
} else {
Value::CycleList(Arc::from(values))
};
cmd.set_param(param.clone(), val);
cmd.set_param(param, val);
}
Op::Emit => {
@@ -613,7 +613,7 @@ impl Forth {
}
Op::GetContext(name) => {
let val = match name.as_str() {
let val = match *name {
"step" => Value::Int(ctx.step as i64, None),
"beat" => Value::Float(ctx.beat, None),
"bank" => Value::Int(ctx.bank as i64, None),
@@ -879,8 +879,8 @@ impl Forth {
return Err("tempo and speed must be non-zero".into());
}
let dur = beats * 60.0 / ctx.tempo / ctx.speed;
cmd.set_param("fit".into(), Value::Float(dur, None));
cmd.set_param("dur".into(), Value::Float(dur, None));
cmd.set_param("fit", Value::Float(dur, None));
cmd.set_param("dur", Value::Float(dur, None));
}
Op::At => {
@@ -896,18 +896,18 @@ impl Forth {
let s = stack.pop().ok_or("stack underflow")?;
let d = stack.pop().ok_or("stack underflow")?;
let a = stack.pop().ok_or("stack underflow")?;
cmd.set_param("attack".into(), a);
cmd.set_param("decay".into(), d);
cmd.set_param("sustain".into(), s);
cmd.set_param("release".into(), r);
cmd.set_param("attack", a);
cmd.set_param("decay", d);
cmd.set_param("sustain", s);
cmd.set_param("release", r);
}
Op::Ad => {
let d = stack.pop().ok_or("stack underflow")?;
let a = stack.pop().ok_or("stack underflow")?;
cmd.set_param("attack".into(), a);
cmd.set_param("decay".into(), d);
cmd.set_param("sustain".into(), Value::Int(0, None));
cmd.set_param("attack", a);
cmd.set_param("decay", d);
cmd.set_param("sustain", Value::Int(0, None));
}
Op::Apply => {
@@ -1055,14 +1055,14 @@ impl Forth {
params
.iter()
.rev()
.find(|(k, _)| k == name)
.find(|(k, _)| *k == name)
.and_then(|(_, v)| v.as_int().ok())
};
let get_float = |name: &str| -> Option<f64> {
params
.iter()
.rev()
.find(|(k, _)| k == name)
.find(|(k, _)| *k == name)
.and_then(|(_, v)| v.as_float().ok())
};
let chan = get_int("chan")
@@ -1140,11 +1140,11 @@ impl Forth {
}
}
fn extract_dev_param(params: &[(String, Value)]) -> u8 {
fn extract_dev_param(params: &[(&str, Value)]) -> u8 {
params
.iter()
.rev()
.find(|(k, _)| k == "dev")
.find(|(k, _)| *k == "dev")
.and_then(|(_, v)| v.as_int().ok())
.map(|d| d.clamp(0, 3) as u8)
.unwrap_or(0)
@@ -1181,7 +1181,7 @@ fn is_tempo_scaled_param(name: &str) -> bool {
fn emit_output(
sound: Option<&str>,
params: &[(String, String)],
params: &[(&str, String)],
step_duration: f64,
nudge_secs: f64,
outputs: &mut Vec<String>,
@@ -1190,8 +1190,8 @@ fn emit_output(
let mut out = String::with_capacity(128);
out.push('/');
let has_dur = params.iter().any(|(k, _)| k == "dur");
let delaytime_idx = params.iter().position(|(k, _)| k == "delaytime");
let has_dur = params.iter().any(|(k, _)| *k == "dur");
let delaytime_idx = params.iter().position(|(k, _)| *k == "delaytime");
if let Some(s) = sound {
let _ = write!(&mut out, "sound/{s}");