WIP: optimizations for linux
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use parking_lot::Mutex;
|
||||
use rand::rngs::StdRng;
|
||||
use rand::{Rng as RngTrait, SeedableRng};
|
||||
use std::borrow::Cow;
|
||||
@@ -19,7 +20,7 @@ pub struct Forth {
|
||||
impl Forth {
|
||||
pub fn new(vars: Variables, dict: Dictionary, rng: Rng) -> Self {
|
||||
Self {
|
||||
stack: std::sync::Arc::new(std::sync::Mutex::new(Vec::new())),
|
||||
stack: Arc::new(Mutex::new(Vec::new())),
|
||||
vars,
|
||||
dict,
|
||||
rng,
|
||||
@@ -28,12 +29,12 @@ impl Forth {
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn stack(&self) -> Vec<Value> {
|
||||
self.stack.lock().unwrap().clone()
|
||||
self.stack.lock().clone()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn clear_stack(&self) {
|
||||
self.stack.lock().unwrap().clear();
|
||||
self.stack.lock().clear();
|
||||
}
|
||||
|
||||
pub fn evaluate(&self, script: &str, ctx: &StepContext) -> Result<Vec<String>, String> {
|
||||
@@ -69,7 +70,7 @@ impl Forth {
|
||||
ctx: &StepContext,
|
||||
trace: Option<&mut ExecutionTrace>,
|
||||
) -> Result<Vec<String>, String> {
|
||||
let mut stack = self.stack.lock().unwrap();
|
||||
let mut stack = self.stack.lock();
|
||||
let mut outputs: Vec<String> = Vec::with_capacity(8);
|
||||
let mut cmd = CmdRegister::new();
|
||||
|
||||
@@ -474,7 +475,7 @@ impl Forth {
|
||||
Op::Get => {
|
||||
let name = stack.pop().ok_or("stack underflow")?;
|
||||
let name = name.as_str()?;
|
||||
let vars = self.vars.lock().unwrap();
|
||||
let vars = self.vars.lock();
|
||||
let val = vars.get(name).cloned().unwrap_or(Value::Int(0, None));
|
||||
stack.push(val);
|
||||
}
|
||||
@@ -482,7 +483,7 @@ impl Forth {
|
||||
let name = stack.pop().ok_or("stack underflow")?;
|
||||
let name = name.as_str()?.to_string();
|
||||
let val = stack.pop().ok_or("stack underflow")?;
|
||||
self.vars.lock().unwrap().insert(name, val);
|
||||
self.vars.lock().insert(name, val);
|
||||
}
|
||||
|
||||
Op::GetContext(name) => {
|
||||
@@ -520,7 +521,7 @@ impl Forth {
|
||||
} else {
|
||||
(*b_i, *a_i)
|
||||
};
|
||||
let val = self.rng.lock().unwrap().gen_range(lo..=hi);
|
||||
let val = self.rng.lock().gen_range(lo..=hi);
|
||||
stack.push(Value::Int(val, None));
|
||||
}
|
||||
_ => {
|
||||
@@ -530,7 +531,7 @@ impl Forth {
|
||||
let val = if (hi - lo).abs() < f64::EPSILON {
|
||||
lo
|
||||
} else {
|
||||
self.rng.lock().unwrap().gen_range(lo..hi)
|
||||
self.rng.lock().gen_range(lo..hi)
|
||||
};
|
||||
stack.push(Value::Float(val, None));
|
||||
}
|
||||
@@ -543,7 +544,7 @@ impl Forth {
|
||||
return Err("exprand requires positive values".into());
|
||||
}
|
||||
let (lo, hi) = if lo <= hi { (lo, hi) } else { (hi, lo) };
|
||||
let u: f64 = self.rng.lock().unwrap().gen();
|
||||
let u: f64 = self.rng.lock().gen();
|
||||
let val = lo * (hi / lo).powf(u);
|
||||
stack.push(Value::Float(val, None));
|
||||
}
|
||||
@@ -554,13 +555,13 @@ impl Forth {
|
||||
return Err("logrand requires positive values".into());
|
||||
}
|
||||
let (lo, hi) = if lo <= hi { (lo, hi) } else { (hi, lo) };
|
||||
let u: f64 = self.rng.lock().unwrap().gen();
|
||||
let u: f64 = self.rng.lock().gen();
|
||||
let val = hi * (lo / hi).powf(u);
|
||||
stack.push(Value::Float(val, None));
|
||||
}
|
||||
Op::Seed => {
|
||||
let s = stack.pop().ok_or("stack underflow")?.as_int()?;
|
||||
*self.rng.lock().unwrap() = StdRng::seed_from_u64(s as u64);
|
||||
*self.rng.lock() = StdRng::seed_from_u64(s as u64);
|
||||
}
|
||||
|
||||
Op::Cycle | Op::PCycle => {
|
||||
@@ -580,14 +581,14 @@ impl Forth {
|
||||
if count == 0 {
|
||||
return Err("choose count must be > 0".into());
|
||||
}
|
||||
let idx = self.rng.lock().unwrap().gen_range(0..count);
|
||||
let idx = self.rng.lock().gen_range(0..count);
|
||||
drain_select_run(count, idx, stack, outputs, cmd)?;
|
||||
}
|
||||
|
||||
Op::ChanceExec | Op::ProbExec => {
|
||||
let threshold = stack.pop().ok_or("stack underflow")?.as_float()?;
|
||||
let quot = stack.pop().ok_or("stack underflow")?;
|
||||
let val: f64 = self.rng.lock().unwrap().gen();
|
||||
let val: f64 = self.rng.lock().gen();
|
||||
let limit = match &ops[pc] {
|
||||
Op::ChanceExec => threshold,
|
||||
_ => threshold / 100.0,
|
||||
@@ -598,7 +599,7 @@ impl Forth {
|
||||
}
|
||||
|
||||
Op::Coin => {
|
||||
let val: f64 = self.rng.lock().unwrap().gen();
|
||||
let val: f64 = self.rng.lock().gen();
|
||||
stack.push(Value::Int(if val < 0.5 { 1 } else { 0 }, None));
|
||||
}
|
||||
|
||||
@@ -711,7 +712,6 @@ impl Forth {
|
||||
let clamped = tempo.clamp(20.0, 300.0);
|
||||
self.vars
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert("__tempo__".to_string(), Value::Float(clamped, None));
|
||||
}
|
||||
|
||||
@@ -720,7 +720,6 @@ impl Forth {
|
||||
let clamped = speed.clamp(0.125, 8.0);
|
||||
self.vars
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(ctx.speed_key.to_string(), Value::Float(clamped, None));
|
||||
}
|
||||
|
||||
@@ -736,7 +735,7 @@ impl Forth {
|
||||
use std::fmt::Write;
|
||||
let mut val = String::with_capacity(8);
|
||||
let _ = write!(&mut val, "{bank}:{pattern}");
|
||||
self.vars.lock().unwrap().insert(ctx.chain_key.to_string(), Value::Str(Arc::from(val), None));
|
||||
self.vars.lock().insert(ctx.chain_key.to_string(), Value::Str(Arc::from(val), None));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -852,7 +851,6 @@ impl Forth {
|
||||
for i in 0..count {
|
||||
self.vars
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert("i".to_string(), Value::Int(i, None));
|
||||
run_quotation(quot.clone(), stack, outputs, cmd)?;
|
||||
}
|
||||
@@ -954,7 +952,7 @@ impl Forth {
|
||||
}
|
||||
Op::Forget => {
|
||||
let name = stack.pop().ok_or("stack underflow")?.as_str()?.to_string();
|
||||
self.dict.lock().unwrap().remove(&name);
|
||||
self.dict.lock().remove(&name);
|
||||
}
|
||||
}
|
||||
pc += 1;
|
||||
|
||||
Reference in New Issue
Block a user