-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rune: Switch to storing values inline
- Loading branch information
Showing
55 changed files
with
3,643 additions
and
2,399 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use rune::{BuildError, Context, Diagnostics, Options, Source, Sources, Vm}; | ||
use std::any::Any; | ||
use std::sync::Arc; | ||
|
||
pub(crate) fn vm( | ||
context: &Context, | ||
sources: &mut Sources, | ||
diagnostics: &mut Diagnostics, | ||
) -> Result<Vm, BuildError> { | ||
let mut options = Options::default(); | ||
options | ||
.parse_option("function-body=true") | ||
.expect("failed to parse option"); | ||
|
||
let unit = rune::prepare(sources) | ||
.with_context(context) | ||
.with_diagnostics(diagnostics) | ||
.with_options(&options) | ||
.build()?; | ||
|
||
let context = Arc::new(context.runtime()?); | ||
Ok(Vm::new(context, Arc::new(unit))) | ||
} | ||
|
||
pub(crate) fn sources(source: &str) -> Sources { | ||
let mut sources = Sources::new(); | ||
|
||
sources | ||
.insert(Source::new("main", source).expect("Failed to construct source")) | ||
.expect("Failed to insert source"); | ||
|
||
sources | ||
} | ||
|
||
macro_rules! rune_vm { | ||
($($tt:tt)*) => {{ | ||
let context = rune::Context::with_default_modules().expect("Failed to build context"); | ||
let mut diagnostics = Default::default(); | ||
let mut sources = $crate::sources(stringify!($($tt)*)); | ||
$crate::vm(&context, &mut sources, &mut diagnostics).expect("Program to compile successfully") | ||
}}; | ||
} | ||
|
||
macro_rules! rhai_ast { | ||
($($tt:tt)*) => {{ | ||
let mut engine = $crate::rhai::Engine::new(); | ||
engine.set_optimization_level($crate::rhai::OptimizationLevel::Full); | ||
let ast = engine.compile(stringify!($($tt)*)).unwrap(); | ||
$crate::RhaiRunner { engine, ast } | ||
}}; | ||
} | ||
|
||
pub(crate) struct RhaiRunner { | ||
pub(crate) engine: crate::rhai::Engine, | ||
pub(crate) ast: crate::rhai::AST, | ||
} | ||
|
||
impl RhaiRunner { | ||
fn eval<T: Any + Clone>(&self) -> T { | ||
self.engine.eval_ast(&self.ast).unwrap() | ||
} | ||
} | ||
|
||
pub(crate) mod rhai { | ||
pub(crate) use ::rhai::{Engine, OptimizationLevel, AST}; | ||
} | ||
|
||
mod comparisons { | ||
pub mod primes; | ||
} | ||
|
||
criterion::criterion_main! { | ||
comparisons::primes::benches, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use criterion::Criterion; | ||
use rune::Hash; | ||
|
||
criterion::criterion_group!(benches, entry); | ||
|
||
fn entry(b: &mut Criterion) { | ||
let mut group = b.benchmark_group("primes"); | ||
|
||
group.bench_function("rhai", |b| { | ||
let ast = rhai_ast! { | ||
const MAX_NUMBER_TO_CHECK = 10_000; | ||
|
||
let prime_mask = []; | ||
prime_mask.pad(MAX_NUMBER_TO_CHECK, true); | ||
|
||
prime_mask[0] = false; | ||
prime_mask[1] = false; | ||
|
||
let total_primes_found = 0; | ||
|
||
for p in 2..MAX_NUMBER_TO_CHECK { | ||
if prime_mask[p] { | ||
total_primes_found += 1; | ||
let i = 2 * p; | ||
|
||
while i < MAX_NUMBER_TO_CHECK { | ||
prime_mask[i] = false; | ||
i += p; | ||
} | ||
} | ||
} | ||
|
||
total_primes_found | ||
}; | ||
|
||
b.iter(|| { | ||
let value = ast.eval::<i64>(); | ||
assert_eq!(value, 1229); | ||
value | ||
}); | ||
}); | ||
|
||
group.bench_function("rune", |b| { | ||
let mut vm = rune_vm! { | ||
const MAX_NUMBER_TO_CHECK = 10_000; | ||
|
||
let prime_mask = []; | ||
prime_mask.resize(MAX_NUMBER_TO_CHECK, true); | ||
|
||
prime_mask[0] = false; | ||
prime_mask[1] = false; | ||
|
||
let total_primes_found = 0; | ||
|
||
for p in 2..MAX_NUMBER_TO_CHECK { | ||
if prime_mask[p] { | ||
total_primes_found += 1; | ||
let i = 2 * p; | ||
|
||
while i < MAX_NUMBER_TO_CHECK { | ||
prime_mask[i] = false; | ||
i += p; | ||
} | ||
} | ||
} | ||
|
||
total_primes_found | ||
}; | ||
|
||
b.iter(|| { | ||
let value = vm.call(Hash::EMPTY, ()).unwrap(); | ||
let value: i64 = rune::from_value(value).unwrap(); | ||
assert_eq!(value, 1229); | ||
value | ||
}) | ||
}); | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.