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 14, 2024
1 parent 276c338 commit 332001f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
16 changes: 2 additions & 14 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ rand_distr = "0.4"
regex = "1.5"
rune = "0.13"
rust-embed = "8"
scylla = { git = "https://github.com/pkolaczk/scylla-rust-driver.git", branch = "vector", features = ["ssl"] }
# scylla = { git = "https://github.com/pkolaczk/scylla-rust-driver.git", branch = "vector", features = ["ssl"] }
scylla = { path = "../scylla-rust-driver/scylla", features = ["ssl"] }
search_path = "0.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down Expand Up @@ -70,7 +71,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 332001f

Please sign in to comment.