2017-08-01 13:55:08 -04:00
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
use std::iter::FromIterator;
|
2017-07-24 10:40:00 -04:00
|
|
|
use std::result::Result as StdResult;
|
2017-07-24 07:30:29 -04:00
|
|
|
|
A lot of performance changes.
Okay, so this is kind of a mega-commit of a lot of performance related changes
to rlua, some of which are pretty complicated.
There are some small improvements here and there, but most of the benefits of
this change are from a few big changes. The simplest big change is that there
is now `protect_lua` as well as `protect_lua_call`, which allows skipping a
lightuserdata parameter and some stack manipulation in some cases. Second
simplest is the change to use Vec instead of VecDeque for MultiValue, and to
have MultiValue be used as a sort of "backwards-only" Vec so that ToLuaMulti /
FromLuaMulti still work correctly.
The most complex change, though, is a change to the way LuaRef works, so that
LuaRef can optionally point into the Lua stack instead of only registry values.
At state creation a set number of stack slots is reserved for the first N LuaRef
types (currently 16), and space for these are also allocated separately
allocated at callback time. There is a huge breaking change here, which is that
now any LuaRef types MUST only be used with the Lua on which they were created,
and CANNOT be used with any other Lua callback instance. This mostly will
affect people using LuaRef types from inside a scope callback, but hopefully in
those cases `Function::bind` will be a suitable replacement. On the plus side,
the rules for LuaRef types are easier to state now.
There is probably more easy-ish perf on the table here, but here's the
preliminary results, based on my very limited benchmarks:
create table time: [314.13 ns 315.71 ns 317.44 ns]
change: [-36.154% -35.670% -35.205%] (p = 0.00 < 0.05)
create array 10 time: [2.9731 us 2.9816 us 2.9901 us]
change: [-16.996% -16.600% -16.196%] (p = 0.00 < 0.05)
Performance has improved.
create string table 10 time: [5.6904 us 5.7164 us 5.7411 us]
change: [-53.536% -53.309% -53.079%] (p = 0.00 < 0.05)
Performance has improved.
call add function 3 10 time: [5.1134 us 5.1222 us 5.1320 us]
change: [-4.1095% -3.6910% -3.1781%] (p = 0.00 < 0.05)
Performance has improved.
call callback add 2 10 time: [5.4408 us 5.4480 us 5.4560 us]
change: [-6.4203% -5.7780% -5.0013%] (p = 0.00 < 0.05)
Performance has improved.
call callback append 10 time: [9.8243 us 9.8410 us 9.8586 us]
change: [-26.937% -26.702% -26.469%] (p = 0.00 < 0.05)
Performance has improved.
create registry 10 time: [3.7005 us 3.7089 us 3.7174 us]
change: [-8.4965% -8.1042% -7.6926%] (p = 0.00 < 0.05)
Performance has improved.
I think that a lot of these benchmarks are too "easy", and most API usage is
going to be more like the 'create string table 10' benchmark, where there are a
lot of handles and tables and strings, so I think that 25%-50% improvement is a
good guess for most use cases.
2018-03-11 23:20:10 -04:00
|
|
|
use error::Result;
|
|
|
|
use value::{FromLua, FromLuaMulti, MultiValue, Nil, ToLua, ToLuaMulti};
|
|
|
|
use lua::Lua;
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-09-14 16:59:59 -04:00
|
|
|
/// Result is convertible to `MultiValue` following the common Lua idiom of returning the result
|
|
|
|
/// on success, or in the case of an error, returning `nil` and an error message.
|
2017-07-24 07:30:29 -04:00
|
|
|
impl<'lua, T: ToLua<'lua>, E: ToLua<'lua>> ToLuaMulti<'lua> for StdResult<T, E> {
|
2017-07-18 16:21:12 -04:00
|
|
|
fn to_lua_multi(self, lua: &'lua Lua) -> Result<MultiValue<'lua>> {
|
|
|
|
let mut result = MultiValue::new();
|
2017-06-13 19:17:41 -04:00
|
|
|
|
|
|
|
match self {
|
A lot of performance changes.
Okay, so this is kind of a mega-commit of a lot of performance related changes
to rlua, some of which are pretty complicated.
There are some small improvements here and there, but most of the benefits of
this change are from a few big changes. The simplest big change is that there
is now `protect_lua` as well as `protect_lua_call`, which allows skipping a
lightuserdata parameter and some stack manipulation in some cases. Second
simplest is the change to use Vec instead of VecDeque for MultiValue, and to
have MultiValue be used as a sort of "backwards-only" Vec so that ToLuaMulti /
FromLuaMulti still work correctly.
The most complex change, though, is a change to the way LuaRef works, so that
LuaRef can optionally point into the Lua stack instead of only registry values.
At state creation a set number of stack slots is reserved for the first N LuaRef
types (currently 16), and space for these are also allocated separately
allocated at callback time. There is a huge breaking change here, which is that
now any LuaRef types MUST only be used with the Lua on which they were created,
and CANNOT be used with any other Lua callback instance. This mostly will
affect people using LuaRef types from inside a scope callback, but hopefully in
those cases `Function::bind` will be a suitable replacement. On the plus side,
the rules for LuaRef types are easier to state now.
There is probably more easy-ish perf on the table here, but here's the
preliminary results, based on my very limited benchmarks:
create table time: [314.13 ns 315.71 ns 317.44 ns]
change: [-36.154% -35.670% -35.205%] (p = 0.00 < 0.05)
create array 10 time: [2.9731 us 2.9816 us 2.9901 us]
change: [-16.996% -16.600% -16.196%] (p = 0.00 < 0.05)
Performance has improved.
create string table 10 time: [5.6904 us 5.7164 us 5.7411 us]
change: [-53.536% -53.309% -53.079%] (p = 0.00 < 0.05)
Performance has improved.
call add function 3 10 time: [5.1134 us 5.1222 us 5.1320 us]
change: [-4.1095% -3.6910% -3.1781%] (p = 0.00 < 0.05)
Performance has improved.
call callback add 2 10 time: [5.4408 us 5.4480 us 5.4560 us]
change: [-6.4203% -5.7780% -5.0013%] (p = 0.00 < 0.05)
Performance has improved.
call callback append 10 time: [9.8243 us 9.8410 us 9.8586 us]
change: [-26.937% -26.702% -26.469%] (p = 0.00 < 0.05)
Performance has improved.
create registry 10 time: [3.7005 us 3.7089 us 3.7174 us]
change: [-8.4965% -8.1042% -7.6926%] (p = 0.00 < 0.05)
Performance has improved.
I think that a lot of these benchmarks are too "easy", and most API usage is
going to be more like the 'create string table 10' benchmark, where there are a
lot of handles and tables and strings, so I think that 25%-50% improvement is a
good guess for most use cases.
2018-03-11 23:20:10 -04:00
|
|
|
Ok(v) => result.push_front(v.to_lua(lua)?),
|
2017-06-13 19:17:41 -04:00
|
|
|
Err(e) => {
|
A lot of performance changes.
Okay, so this is kind of a mega-commit of a lot of performance related changes
to rlua, some of which are pretty complicated.
There are some small improvements here and there, but most of the benefits of
this change are from a few big changes. The simplest big change is that there
is now `protect_lua` as well as `protect_lua_call`, which allows skipping a
lightuserdata parameter and some stack manipulation in some cases. Second
simplest is the change to use Vec instead of VecDeque for MultiValue, and to
have MultiValue be used as a sort of "backwards-only" Vec so that ToLuaMulti /
FromLuaMulti still work correctly.
The most complex change, though, is a change to the way LuaRef works, so that
LuaRef can optionally point into the Lua stack instead of only registry values.
At state creation a set number of stack slots is reserved for the first N LuaRef
types (currently 16), and space for these are also allocated separately
allocated at callback time. There is a huge breaking change here, which is that
now any LuaRef types MUST only be used with the Lua on which they were created,
and CANNOT be used with any other Lua callback instance. This mostly will
affect people using LuaRef types from inside a scope callback, but hopefully in
those cases `Function::bind` will be a suitable replacement. On the plus side,
the rules for LuaRef types are easier to state now.
There is probably more easy-ish perf on the table here, but here's the
preliminary results, based on my very limited benchmarks:
create table time: [314.13 ns 315.71 ns 317.44 ns]
change: [-36.154% -35.670% -35.205%] (p = 0.00 < 0.05)
create array 10 time: [2.9731 us 2.9816 us 2.9901 us]
change: [-16.996% -16.600% -16.196%] (p = 0.00 < 0.05)
Performance has improved.
create string table 10 time: [5.6904 us 5.7164 us 5.7411 us]
change: [-53.536% -53.309% -53.079%] (p = 0.00 < 0.05)
Performance has improved.
call add function 3 10 time: [5.1134 us 5.1222 us 5.1320 us]
change: [-4.1095% -3.6910% -3.1781%] (p = 0.00 < 0.05)
Performance has improved.
call callback add 2 10 time: [5.4408 us 5.4480 us 5.4560 us]
change: [-6.4203% -5.7780% -5.0013%] (p = 0.00 < 0.05)
Performance has improved.
call callback append 10 time: [9.8243 us 9.8410 us 9.8586 us]
change: [-26.937% -26.702% -26.469%] (p = 0.00 < 0.05)
Performance has improved.
create registry 10 time: [3.7005 us 3.7089 us 3.7174 us]
change: [-8.4965% -8.1042% -7.6926%] (p = 0.00 < 0.05)
Performance has improved.
I think that a lot of these benchmarks are too "easy", and most API usage is
going to be more like the 'create string table 10' benchmark, where there are a
lot of handles and tables and strings, so I think that 25%-50% improvement is a
good guess for most use cases.
2018-03-11 23:20:10 -04:00
|
|
|
result.push_front(e.to_lua(lua)?);
|
|
|
|
result.push_front(Nil);
|
2017-06-13 19:17:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 19:50:59 -04:00
|
|
|
impl<'lua, T: ToLua<'lua>> ToLuaMulti<'lua> for T {
|
2017-07-18 16:21:12 -04:00
|
|
|
fn to_lua_multi(self, lua: &'lua Lua) -> Result<MultiValue<'lua>> {
|
|
|
|
let mut v = MultiValue::new();
|
A lot of performance changes.
Okay, so this is kind of a mega-commit of a lot of performance related changes
to rlua, some of which are pretty complicated.
There are some small improvements here and there, but most of the benefits of
this change are from a few big changes. The simplest big change is that there
is now `protect_lua` as well as `protect_lua_call`, which allows skipping a
lightuserdata parameter and some stack manipulation in some cases. Second
simplest is the change to use Vec instead of VecDeque for MultiValue, and to
have MultiValue be used as a sort of "backwards-only" Vec so that ToLuaMulti /
FromLuaMulti still work correctly.
The most complex change, though, is a change to the way LuaRef works, so that
LuaRef can optionally point into the Lua stack instead of only registry values.
At state creation a set number of stack slots is reserved for the first N LuaRef
types (currently 16), and space for these are also allocated separately
allocated at callback time. There is a huge breaking change here, which is that
now any LuaRef types MUST only be used with the Lua on which they were created,
and CANNOT be used with any other Lua callback instance. This mostly will
affect people using LuaRef types from inside a scope callback, but hopefully in
those cases `Function::bind` will be a suitable replacement. On the plus side,
the rules for LuaRef types are easier to state now.
There is probably more easy-ish perf on the table here, but here's the
preliminary results, based on my very limited benchmarks:
create table time: [314.13 ns 315.71 ns 317.44 ns]
change: [-36.154% -35.670% -35.205%] (p = 0.00 < 0.05)
create array 10 time: [2.9731 us 2.9816 us 2.9901 us]
change: [-16.996% -16.600% -16.196%] (p = 0.00 < 0.05)
Performance has improved.
create string table 10 time: [5.6904 us 5.7164 us 5.7411 us]
change: [-53.536% -53.309% -53.079%] (p = 0.00 < 0.05)
Performance has improved.
call add function 3 10 time: [5.1134 us 5.1222 us 5.1320 us]
change: [-4.1095% -3.6910% -3.1781%] (p = 0.00 < 0.05)
Performance has improved.
call callback add 2 10 time: [5.4408 us 5.4480 us 5.4560 us]
change: [-6.4203% -5.7780% -5.0013%] (p = 0.00 < 0.05)
Performance has improved.
call callback append 10 time: [9.8243 us 9.8410 us 9.8586 us]
change: [-26.937% -26.702% -26.469%] (p = 0.00 < 0.05)
Performance has improved.
create registry 10 time: [3.7005 us 3.7089 us 3.7174 us]
change: [-8.4965% -8.1042% -7.6926%] (p = 0.00 < 0.05)
Performance has improved.
I think that a lot of these benchmarks are too "easy", and most API usage is
going to be more like the 'create string table 10' benchmark, where there are a
lot of handles and tables and strings, so I think that 25%-50% improvement is a
good guess for most use cases.
2018-03-11 23:20:10 -04:00
|
|
|
v.push_front(self.to_lua(lua)?);
|
2017-05-21 19:50:59 -04:00
|
|
|
Ok(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'lua, T: FromLua<'lua>> FromLuaMulti<'lua> for T {
|
2017-07-18 16:21:12 -04:00
|
|
|
fn from_lua_multi(mut values: MultiValue<'lua>, lua: &'lua Lua) -> Result<Self> {
|
2017-07-24 07:12:52 -04:00
|
|
|
Ok(T::from_lua(values.pop_front().unwrap_or(Nil), lua)?)
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
impl<'lua> ToLuaMulti<'lua> for MultiValue<'lua> {
|
|
|
|
fn to_lua_multi(self, _: &'lua Lua) -> Result<MultiValue<'lua>> {
|
2017-05-21 19:50:59 -04:00
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
impl<'lua> FromLuaMulti<'lua> for MultiValue<'lua> {
|
|
|
|
fn from_lua_multi(values: MultiValue<'lua>, _: &'lua Lua) -> Result<Self> {
|
2017-05-21 19:50:59 -04:00
|
|
|
Ok(values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 16:59:59 -04:00
|
|
|
/// Wraps a variable number of `T`s.
|
|
|
|
///
|
|
|
|
/// Can be used to work with variadic functions more easily. Using this type as the last argument of
|
|
|
|
/// a Rust callback will accept any number of arguments from Lua and convert them to the type `T`
|
|
|
|
/// using [`FromLua`]. `Variadic<T>` can also be returned from a callback, returning a variable
|
|
|
|
/// number of values to Lua.
|
|
|
|
///
|
|
|
|
/// The [`MultiValue`] type is equivalent to `Variadic<Value>`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # extern crate rlua;
|
|
|
|
/// # use rlua::{Lua, Variadic, Result};
|
|
|
|
/// # fn try_main() -> Result<()> {
|
|
|
|
/// let lua = Lua::new();
|
|
|
|
///
|
|
|
|
/// let add = lua.create_function(|_, vals: Variadic<f64>| -> Result<f64> {
|
|
|
|
/// Ok(vals.iter().sum())
|
2017-12-03 23:45:00 -05:00
|
|
|
/// }).unwrap();
|
2017-09-14 16:59:59 -04:00
|
|
|
/// lua.globals().set("add", add)?;
|
|
|
|
/// assert_eq!(lua.eval::<f64>("add(3, 2, 5)", None)?, 10.0);
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// # fn main() {
|
|
|
|
/// # try_main().unwrap();
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2017-09-15 14:56:46 -04:00
|
|
|
///
|
|
|
|
/// [`FromLua`]: trait.FromLua.html
|
|
|
|
/// [`MultiValue`]: struct.MultiValue.html
|
2017-08-01 13:55:08 -04:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Variadic<T>(Vec<T>);
|
|
|
|
|
|
|
|
impl<T> Variadic<T> {
|
2017-09-14 16:59:59 -04:00
|
|
|
/// Creates an empty `Variadic` wrapper containing no values.
|
2017-08-01 13:55:08 -04:00
|
|
|
pub fn new() -> Variadic<T> {
|
|
|
|
Variadic(Vec::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> FromIterator<T> for Variadic<T> {
|
|
|
|
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
|
|
|
|
Variadic(Vec::from_iter(iter))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IntoIterator for Variadic<T> {
|
|
|
|
type Item = T;
|
|
|
|
type IntoIter = <Vec<T> as IntoIterator>::IntoIter;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.0.into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Deref for Variadic<T> {
|
|
|
|
type Target = Vec<T>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> DerefMut for Variadic<T> {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
impl<'lua, T: ToLua<'lua>> ToLuaMulti<'lua> for Variadic<T> {
|
|
|
|
fn to_lua_multi(self, lua: &'lua Lua) -> Result<MultiValue<'lua>> {
|
2017-05-21 19:50:59 -04:00
|
|
|
self.0.into_iter().map(|e| e.to_lua(lua)).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
impl<'lua, T: FromLua<'lua>> FromLuaMulti<'lua> for Variadic<T> {
|
|
|
|
fn from_lua_multi(values: MultiValue<'lua>, lua: &'lua Lua) -> Result<Self> {
|
2017-05-21 19:50:59 -04:00
|
|
|
values
|
|
|
|
.into_iter()
|
|
|
|
.map(|e| T::from_lua(e, lua))
|
2017-07-18 16:21:12 -04:00
|
|
|
.collect::<Result<Vec<T>>>()
|
|
|
|
.map(Variadic)
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 01:21:41 -04:00
|
|
|
macro_rules! impl_tuple {
|
|
|
|
() => (
|
|
|
|
impl<'lua> ToLuaMulti<'lua> for () {
|
|
|
|
fn to_lua_multi(self, _: &'lua Lua) -> Result<MultiValue> {
|
|
|
|
Ok(MultiValue::new())
|
|
|
|
}
|
|
|
|
}
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-07-31 01:21:41 -04:00
|
|
|
impl<'lua> FromLuaMulti<'lua> for () {
|
|
|
|
fn from_lua_multi(_: MultiValue, _: &'lua Lua) -> Result<Self> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
($last:ident $($name:ident)*) => (
|
|
|
|
impl<'lua, $($name,)* $last> ToLuaMulti<'lua> for ($($name,)* $last,)
|
|
|
|
where $($name: ToLua<'lua>,)*
|
|
|
|
$last: ToLuaMulti<'lua>
|
|
|
|
{
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
fn to_lua_multi(self, lua: &'lua Lua) -> Result<MultiValue<'lua>> {
|
|
|
|
let ($($name,)* $last,) = self;
|
|
|
|
|
|
|
|
let mut results = $last.to_lua_multi(lua)?;
|
|
|
|
push_reverse!(results, $($name.to_lua(lua)?,)*);
|
|
|
|
Ok(results)
|
|
|
|
}
|
|
|
|
}
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-07-31 01:21:41 -04:00
|
|
|
impl<'lua, $($name,)* $last> FromLuaMulti<'lua> for ($($name,)* $last,)
|
|
|
|
where $($name: FromLua<'lua>,)*
|
|
|
|
$last: FromLuaMulti<'lua>
|
|
|
|
{
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
fn from_lua_multi(mut values: MultiValue<'lua>, lua: &'lua Lua) -> Result<Self> {
|
|
|
|
$(let $name = values.pop_front().unwrap_or(Nil);)*
|
|
|
|
let $last = FromLuaMulti::from_lua_multi(values, lua)?;
|
|
|
|
Ok(($(FromLua::from_lua($name, lua)?,)* $last,))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
|
|
|
|
2017-07-31 01:21:41 -04:00
|
|
|
macro_rules! push_reverse {
|
|
|
|
($multi_value:expr, $first:expr, $($rest:expr,)*) => (
|
|
|
|
push_reverse!($multi_value, $($rest,)*);
|
|
|
|
$multi_value.push_front($first);
|
|
|
|
);
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-07-31 01:21:41 -04:00
|
|
|
($multi_value:expr, $first:expr) => (
|
|
|
|
$multi_value.push_front($first);
|
|
|
|
);
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-07-31 01:21:41 -04:00
|
|
|
($multi_value:expr,) => ();
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
2017-07-31 01:21:41 -04:00
|
|
|
|
2017-08-01 13:55:08 -04:00
|
|
|
impl_tuple!{}
|
2017-08-01 13:57:51 -04:00
|
|
|
impl_tuple!{A}
|
|
|
|
impl_tuple!{A B}
|
|
|
|
impl_tuple!{A B C}
|
|
|
|
impl_tuple!{A B C D}
|
|
|
|
impl_tuple!{A B C D E}
|
|
|
|
impl_tuple!{A B C D E F}
|
|
|
|
impl_tuple!{A B C D E F G}
|
|
|
|
impl_tuple!{A B C D E F G H}
|
|
|
|
impl_tuple!{A B C D E F G H I}
|
|
|
|
impl_tuple!{A B C D E F G H I J}
|
|
|
|
impl_tuple!{A B C D E F G H I J K}
|
|
|
|
impl_tuple!{A B C D E F G H I J K L}
|