2019-10-17 11:04:43 -04:00
|
|
|
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
|
2021-02-22 15:13:56 -05:00
|
|
|
use std::time::Duration;
|
2021-02-21 10:38:50 -05:00
|
|
|
use tokio::runtime::Runtime;
|
2021-03-03 18:21:56 -05:00
|
|
|
use tokio::task;
|
2021-02-21 10:38:50 -05:00
|
|
|
|
2019-10-17 11:04:43 -04:00
|
|
|
use mlua::prelude::*;
|
2018-03-10 10:31:57 -05:00
|
|
|
|
2021-02-21 10:38:50 -05:00
|
|
|
fn collect_gc_twice(lua: &Lua) {
|
|
|
|
lua.gc_collect().unwrap();
|
|
|
|
lua.gc_collect().unwrap();
|
|
|
|
}
|
|
|
|
|
2018-03-11 14:26:26 -04:00
|
|
|
fn create_table(c: &mut Criterion) {
|
2021-02-21 10:38:50 -05:00
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
c.bench_function("create [table empty]", |b| {
|
|
|
|
b.iter_batched(
|
|
|
|
|| collect_gc_twice(&lua),
|
|
|
|
|_| {
|
2018-03-11 14:26:26 -04:00
|
|
|
lua.create_table().unwrap();
|
|
|
|
},
|
2019-10-17 11:04:43 -04:00
|
|
|
BatchSize::SmallInput,
|
2018-03-11 14:26:26 -04:00
|
|
|
);
|
|
|
|
});
|
2018-03-10 10:31:57 -05:00
|
|
|
}
|
|
|
|
|
2018-03-11 14:26:26 -04:00
|
|
|
fn create_array(c: &mut Criterion) {
|
2021-02-21 10:38:50 -05:00
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
c.bench_function("create [array] 10", |b| {
|
|
|
|
b.iter_batched(
|
|
|
|
|| collect_gc_twice(&lua),
|
|
|
|
|_| {
|
2019-10-17 11:04:43 -04:00
|
|
|
let table = lua.create_table().unwrap();
|
2021-02-21 10:38:50 -05:00
|
|
|
for i in 1..=10 {
|
2019-10-17 11:04:43 -04:00
|
|
|
table.set(i, i).unwrap();
|
2018-03-11 14:26:26 -04:00
|
|
|
}
|
|
|
|
},
|
2019-10-17 11:04:43 -04:00
|
|
|
BatchSize::SmallInput,
|
2018-03-11 14:26:26 -04:00
|
|
|
);
|
|
|
|
});
|
2018-03-10 10:31:57 -05:00
|
|
|
}
|
|
|
|
|
2018-03-11 14:26:26 -04:00
|
|
|
fn create_string_table(c: &mut Criterion) {
|
2021-02-21 10:38:50 -05:00
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
c.bench_function("create [table string] 10", |b| {
|
|
|
|
b.iter_batched(
|
|
|
|
|| collect_gc_twice(&lua),
|
|
|
|
|_| {
|
2019-10-17 11:04:43 -04:00
|
|
|
let table = lua.create_table().unwrap();
|
|
|
|
for &s in &["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] {
|
|
|
|
let s = lua.create_string(s).unwrap();
|
|
|
|
table.set(s.clone(), s).unwrap();
|
2018-03-11 14:26:26 -04:00
|
|
|
}
|
|
|
|
},
|
2019-10-17 11:04:43 -04:00
|
|
|
BatchSize::SmallInput,
|
2018-03-11 14:26:26 -04:00
|
|
|
);
|
|
|
|
});
|
2018-03-10 10:31:57 -05:00
|
|
|
}
|
|
|
|
|
2021-06-30 11:50:50 -04:00
|
|
|
fn create_function(c: &mut Criterion) {
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
c.bench_function("create [function] 10", |b| {
|
|
|
|
b.iter_batched(
|
|
|
|
|| collect_gc_twice(&lua),
|
|
|
|
|_| {
|
|
|
|
for i in 0..10 {
|
|
|
|
lua.create_function(move |_, ()| Ok(i)).unwrap();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
BatchSize::SmallInput,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-21 10:38:50 -05:00
|
|
|
fn call_lua_function(c: &mut Criterion) {
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
c.bench_function("call Lua function [sum] 3 10", |b| {
|
2019-10-17 11:04:43 -04:00
|
|
|
b.iter_batched_ref(
|
2018-03-11 14:26:26 -04:00
|
|
|
|| {
|
2021-02-21 10:38:50 -05:00
|
|
|
collect_gc_twice(&lua);
|
|
|
|
lua.load("function(a, b, c) return a + b + c end")
|
|
|
|
.eval::<LuaFunction>()
|
|
|
|
.unwrap()
|
|
|
|
},
|
|
|
|
|function| {
|
2019-10-17 11:04:43 -04:00
|
|
|
for i in 0..10 {
|
2021-02-21 10:38:50 -05:00
|
|
|
let _result: i64 = function.call((i, i + 1, i + 2)).unwrap();
|
2018-03-11 14:26:26 -04:00
|
|
|
}
|
|
|
|
},
|
2019-10-17 11:04:43 -04:00
|
|
|
BatchSize::SmallInput,
|
2018-03-11 14:26:26 -04:00
|
|
|
);
|
2018-03-10 10:31:57 -05:00
|
|
|
});
|
2018-03-11 14:26:26 -04:00
|
|
|
}
|
|
|
|
|
2021-02-21 10:38:50 -05:00
|
|
|
fn call_sum_callback(c: &mut Criterion) {
|
|
|
|
let lua = Lua::new();
|
|
|
|
let callback = lua
|
|
|
|
.create_function(|_, (a, b, c): (i64, i64, i64)| Ok(a + b + c))
|
|
|
|
.unwrap();
|
|
|
|
lua.globals().set("callback", callback).unwrap();
|
|
|
|
|
|
|
|
c.bench_function("call Rust callback [sum] 3 10", |b| {
|
2019-10-17 11:04:43 -04:00
|
|
|
b.iter_batched_ref(
|
2018-03-11 14:26:26 -04:00
|
|
|
|| {
|
2021-02-21 10:38:50 -05:00
|
|
|
collect_gc_twice(&lua);
|
|
|
|
lua.load("function() for i = 1,10 do callback(i, i+1, i+2) end end")
|
|
|
|
.eval::<LuaFunction>()
|
|
|
|
.unwrap()
|
|
|
|
},
|
|
|
|
|function| {
|
|
|
|
function.call::<_, ()>(()).unwrap();
|
2018-03-11 14:26:26 -04:00
|
|
|
},
|
2019-10-17 11:04:43 -04:00
|
|
|
BatchSize::SmallInput,
|
2018-03-11 14:26:26 -04:00
|
|
|
);
|
2018-03-10 10:31:57 -05:00
|
|
|
});
|
2018-03-11 14:26:26 -04:00
|
|
|
}
|
|
|
|
|
2021-02-21 10:38:50 -05:00
|
|
|
fn call_async_sum_callback(c: &mut Criterion) {
|
2021-11-14 07:57:49 -05:00
|
|
|
let options = LuaOptions::new().thread_cache_size(1024);
|
|
|
|
let lua = Lua::new_with(LuaStdLib::ALL_SAFE, options).unwrap();
|
2021-02-21 10:38:50 -05:00
|
|
|
let callback = lua
|
2021-03-03 18:21:56 -05:00
|
|
|
.create_async_function(|_, (a, b, c): (i64, i64, i64)| async move {
|
|
|
|
task::yield_now().await;
|
|
|
|
Ok(a + b + c)
|
|
|
|
})
|
2021-02-21 10:38:50 -05:00
|
|
|
.unwrap();
|
|
|
|
lua.globals().set("callback", callback).unwrap();
|
|
|
|
|
|
|
|
c.bench_function("call async Rust callback [sum] 3 10", |b| {
|
|
|
|
let rt = Runtime::new().unwrap();
|
|
|
|
b.to_async(rt).iter_batched(
|
|
|
|
|| {
|
|
|
|
collect_gc_twice(&lua);
|
|
|
|
lua.load("function() for i = 1,10 do callback(i, i+1, i+2) end end")
|
|
|
|
.eval::<LuaFunction>()
|
|
|
|
.unwrap()
|
|
|
|
},
|
|
|
|
|function| async move {
|
|
|
|
function.call_async::<_, ()>(()).await.unwrap();
|
|
|
|
},
|
|
|
|
BatchSize::SmallInput,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_concat_callback(c: &mut Criterion) {
|
|
|
|
let lua = Lua::new();
|
|
|
|
let callback = lua
|
|
|
|
.create_function(|_, (a, b): (LuaString, LuaString)| {
|
|
|
|
Ok(format!("{}{}", a.to_str()?, b.to_str()?))
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
lua.globals().set("callback", callback).unwrap();
|
|
|
|
|
|
|
|
c.bench_function("call Rust callback [concat string] 10", |b| {
|
2019-10-17 11:04:43 -04:00
|
|
|
b.iter_batched_ref(
|
2018-03-11 14:26:26 -04:00
|
|
|
|| {
|
2021-02-21 10:38:50 -05:00
|
|
|
collect_gc_twice(&lua);
|
|
|
|
lua.load("function() for i = 1,10 do callback('a', tostring(i)) end end")
|
|
|
|
.eval::<LuaFunction>()
|
|
|
|
.unwrap()
|
|
|
|
},
|
|
|
|
|function| {
|
|
|
|
function.call::<_, ()>(()).unwrap();
|
2018-03-11 14:26:26 -04:00
|
|
|
},
|
2019-10-17 11:04:43 -04:00
|
|
|
BatchSize::SmallInput,
|
2018-03-11 14:26:26 -04:00
|
|
|
);
|
2018-03-10 10:31:57 -05:00
|
|
|
});
|
|
|
|
}
|
2018-03-11 17:50:17 -04:00
|
|
|
|
|
|
|
fn create_registry_values(c: &mut Criterion) {
|
2021-02-21 10:38:50 -05:00
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
c.bench_function("create [registry value] 10", |b| {
|
|
|
|
b.iter_batched(
|
|
|
|
|| collect_gc_twice(&lua),
|
|
|
|
|_| {
|
2018-03-11 17:50:17 -04:00
|
|
|
for _ in 0..10 {
|
|
|
|
lua.create_registry_value(lua.pack(true).unwrap()).unwrap();
|
|
|
|
}
|
|
|
|
lua.expire_registry_values();
|
|
|
|
},
|
2019-10-17 11:04:43 -04:00
|
|
|
BatchSize::SmallInput,
|
2018-03-11 17:50:17 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-12 12:48:20 -04:00
|
|
|
fn create_userdata(c: &mut Criterion) {
|
2018-03-12 13:13:44 -04:00
|
|
|
struct UserData(i64);
|
2018-03-12 12:48:20 -04:00
|
|
|
impl LuaUserData for UserData {}
|
|
|
|
|
2021-02-21 10:38:50 -05:00
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
c.bench_function("create [table userdata] 10", |b| {
|
|
|
|
b.iter_batched(
|
|
|
|
|| collect_gc_twice(&lua),
|
|
|
|
|_| {
|
2019-10-17 11:04:43 -04:00
|
|
|
let table: LuaTable = lua.create_table().unwrap();
|
|
|
|
for i in 1..11 {
|
|
|
|
table.set(i, UserData(i)).unwrap();
|
2018-03-12 12:48:20 -04:00
|
|
|
}
|
|
|
|
},
|
2019-10-17 11:04:43 -04:00
|
|
|
BatchSize::SmallInput,
|
2018-03-12 12:48:20 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-19 10:07:51 -04:00
|
|
|
fn userdata_index(c: &mut Criterion) {
|
|
|
|
struct UserData(i64);
|
|
|
|
impl LuaUserData for UserData {
|
|
|
|
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
|
|
|
|
methods.add_meta_method(mlua::MetaMethod::Index, move |_, _, index: String| {
|
|
|
|
Ok(index)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let lua = Lua::new();
|
|
|
|
lua.globals().set("userdata", UserData(10)).unwrap();
|
|
|
|
|
|
|
|
c.bench_function("index [table userdata] 10", |b| {
|
|
|
|
b.iter_batched_ref(
|
|
|
|
|| {
|
|
|
|
collect_gc_twice(&lua);
|
|
|
|
lua.load(
|
|
|
|
r#"
|
|
|
|
function()
|
|
|
|
for i = 1,10 do
|
|
|
|
local v = userdata.test
|
|
|
|
end
|
|
|
|
end"#,
|
|
|
|
)
|
|
|
|
.eval::<LuaFunction>()
|
|
|
|
.unwrap()
|
|
|
|
},
|
|
|
|
|function| {
|
|
|
|
function.call::<_, ()>(()).unwrap();
|
|
|
|
},
|
|
|
|
BatchSize::SmallInput,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-21 10:38:50 -05:00
|
|
|
fn call_userdata_method(c: &mut Criterion) {
|
|
|
|
struct UserData(i64);
|
|
|
|
impl LuaUserData for UserData {
|
|
|
|
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
|
|
|
|
methods.add_method("method", |_, this, ()| Ok(this.0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let lua = Lua::new();
|
|
|
|
lua.globals().set("userdata", UserData(10)).unwrap();
|
|
|
|
|
|
|
|
c.bench_function("call [userdata method] 10", |b| {
|
|
|
|
b.iter_batched_ref(
|
|
|
|
|| {
|
|
|
|
collect_gc_twice(&lua);
|
|
|
|
lua.load("function() for i = 1,10 do userdata:method() end end")
|
|
|
|
.eval::<LuaFunction>()
|
|
|
|
.unwrap()
|
|
|
|
},
|
|
|
|
|function| {
|
|
|
|
function.call::<_, ()>(()).unwrap();
|
|
|
|
},
|
|
|
|
BatchSize::SmallInput,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_async_userdata_method(c: &mut Criterion) {
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
struct UserData(i64);
|
|
|
|
impl LuaUserData for UserData {
|
|
|
|
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
|
|
|
|
methods.add_async_method("method", |_, this, ()| async move { Ok(this.0) });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-14 07:57:49 -05:00
|
|
|
let options = LuaOptions::new().thread_cache_size(1024);
|
|
|
|
let lua = Lua::new_with(LuaStdLib::ALL_SAFE, options).unwrap();
|
2021-02-21 10:38:50 -05:00
|
|
|
lua.globals().set("userdata", UserData(10)).unwrap();
|
|
|
|
|
|
|
|
c.bench_function("call async [userdata method] 10", |b| {
|
|
|
|
let rt = Runtime::new().unwrap();
|
|
|
|
b.to_async(rt).iter_batched(
|
|
|
|
|| {
|
|
|
|
collect_gc_twice(&lua);
|
|
|
|
lua.load("function() for i = 1,10 do userdata:method() end end")
|
|
|
|
.eval::<LuaFunction>()
|
|
|
|
.unwrap()
|
|
|
|
},
|
|
|
|
|function| async move {
|
|
|
|
function.call_async::<_, ()>(()).await.unwrap();
|
|
|
|
},
|
|
|
|
BatchSize::SmallInput,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-11 14:26:26 -04:00
|
|
|
criterion_group! {
|
|
|
|
name = benches;
|
|
|
|
config = Criterion::default()
|
2021-02-21 10:38:50 -05:00
|
|
|
.sample_size(300)
|
|
|
|
.measurement_time(Duration::from_secs(10))
|
2018-03-11 14:26:26 -04:00
|
|
|
.noise_threshold(0.02);
|
|
|
|
targets =
|
|
|
|
create_table,
|
|
|
|
create_array,
|
|
|
|
create_string_table,
|
2021-06-30 11:50:50 -04:00
|
|
|
create_function,
|
2021-02-21 10:38:50 -05:00
|
|
|
call_lua_function,
|
|
|
|
call_sum_callback,
|
|
|
|
call_async_sum_callback,
|
|
|
|
call_concat_callback,
|
2018-03-12 12:48:20 -04:00
|
|
|
create_registry_values,
|
2019-10-17 11:04:43 -04:00
|
|
|
create_userdata,
|
2022-03-19 10:07:51 -04:00
|
|
|
userdata_index,
|
2021-02-21 10:38:50 -05:00
|
|
|
call_userdata_method,
|
|
|
|
call_async_userdata_method,
|
2018-03-11 14:26:26 -04:00
|
|
|
}
|
2018-03-10 10:31:57 -05:00
|
|
|
|
|
|
|
criterion_main!(benches);
|