Skip to content

Commit

Permalink
try starlark
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanos committed Oct 27, 2022
1 parent 3f7051e commit c008f59
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ mlua = { version = "0.8.4", optional = true }
rlua = { version = "0.19.4", optional = true }
rhai = { version = "1.10.1", optional = true }
rune = { version = "0.12.0", optional = true }
starlark = { version = "0.8.0", optional = true }
itertools = "0.10"
anyhow = "1.0.66"

[dev-dependencies]
criterion = { version = "0.4" }
Expand Down Expand Up @@ -53,3 +55,8 @@ required-features = ["rhai"]
name = "rune"
harness = false
required-features = ["rune"]

[[bench]]
name = "starlark"
harness = false
required-features = ["starlark"]
Binary file modified Sort userdata.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions benches/rhai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ const PROG: &'static str = r#"
for i in 0..100000 {
array.push(RustData_new(generate_string.call(rand(16) + 1)));
}
array.sort(|a, b| return if a < b { -1 } else if b < a { 1 } else { 0 });
"#;

fn benchmark(c: &mut Criterion) {
Expand Down
2 changes: 0 additions & 2 deletions benches/rune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ pub fn main() {
for i in 0..100000 {
array.push(RustData::new(generate_string(rand(16) + 1)));
}
array.sort_by(|a, b| a.cmp(b));
}"#;

pub fn module() -> Result<Module, ContextError> {
Expand Down
2 changes: 0 additions & 2 deletions benches/sort_userdata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@ local array = {}
for i = 1, 100000 do
table.insert(array, RustData.new(generate_string(rand(16) + 1)))
end

table.sort(array)
57 changes: 57 additions & 0 deletions benches/starlark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#[macro_use]
extern crate starlark;

use starlark::environment::{GlobalsBuilder, Module};
use starlark::eval::Evaluator;
use starlark::syntax::{AstModule, Dialect};

use criterion::{criterion_group, criterion_main, BatchSize, Criterion};

#[starlark_module]
fn functions(builder: &mut GlobalsBuilder) {
fn rand(n: i32) -> anyhow::Result<i32> {
Ok(rand::random::<i32>() % n)
}
}

const PROG: &'static str = r#"
charset = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
def generate_string(len):
data = []
for i in range(len):
data.append(charset[rand(len)])
return ' '.join(data)
array = []
for i in range(100000):
array.append(generate_string(rand(16) + 1));
"#;

fn benchmark(c: &mut Criterion) {
let module = Module::new();
let globals = GlobalsBuilder::standard().with(functions).build();

c.bench_function("Sort userdata", |b| {
b.iter_batched(
|| {
let ast =
AstModule::parse("sort_userdata.star", PROG.to_owned(), &Dialect::Extended)
.expect("parse");
let eval = Evaluator::new(&module);
(eval, ast)
},
|(mut eval, ast)| {
eval.eval_module(ast, &globals).expect("eval module");
},
BatchSize::SmallInput,
);
});
}

criterion_group! {
name = benches;
config = Criterion::default().sample_size(10);
targets = benchmark,
}

criterion_main!(benches);
2 changes: 2 additions & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"

0 comments on commit c008f59

Please sign in to comment.