Skip to content

Commit

Permalink
Optimize normal_vec and uniform_vec functions even more
Browse files Browse the repository at this point in the history
Directly create Rune vector, instead of creating
Rust stdlib vector and forcing Rune to convert it.
  • Loading branch information
pkolaczk committed Aug 15, 2024
1 parent 276c338 commit 1ecc0ec
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
19 changes: 5 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ panic = "abort"

[profile.dev-opt]
inherits = "dev"
opt-level = 2
opt-level = 3

[package.metadata.deb]
name = "latte"
Expand Down
18 changes: 12 additions & 6 deletions src/scripting/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,25 @@ pub fn uniform(i: i64, min: f64, max: f64) -> VmResult<f64> {
}

#[rune::function]
pub fn uniform_vec(i: i64, len: usize, min: f64, max: f64) -> VmResult<Vec<f64>> {
pub fn uniform_vec(i: i64, len: usize, min: f64, max: f64) -> VmResult<Value> {
let mut rng = SmallRng::seed_from_u64(i as u64);
let vec: Vec<f64> = (0..len).map(|_| rng.gen_range(min..max)).collect();
VmResult::Ok(vec)
let mut vec = vm_try!(rune::alloc::Vec::try_with_capacity(len));
for _ in 0..len {
vm_try!(vec.try_push(Value::Float(rng.gen_range(min..max))));
}
Value::vec(vec)
}

#[rune::function]
pub fn normal_vec(i: i64, len: usize, mean: f64, std_dev: f64) -> VmResult<Vec<f64>> {
pub fn normal_vec(i: i64, len: usize, mean: f64, std_dev: f64) -> VmResult<Value> {
let mut rng = SmallRng::seed_from_u64(i as u64);
let distribution =
vm_try!(Normal::new(mean, std_dev).map_err(|e| VmError::panic(format!("{e}"))));
let vec: Vec<f64> = (0..len).map(|_| rng.sample(distribution)).collect();
VmResult::Ok(vec)
let mut vec = vm_try!(rune::alloc::Vec::try_with_capacity(len));
for _ in 0..len {
vm_try!(vec.try_push(Value::Float(rng.sample(distribution))));
}
Value::vec(vec)
}

/// Generates random blob of data of given length.
Expand Down

0 comments on commit 1ecc0ec

Please sign in to comment.