mlua/src/conversion.rs

333 lines
9.0 KiB
Rust
Raw Normal View History

2017-10-23 16:42:20 -04:00
use std::collections::{BTreeMap, HashMap};
use std::hash::{BuildHasher, Hash};
use std::string::String as StdString;
2017-05-21 19:50:59 -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::{Error, Result};
2018-08-05 09:51:39 -04:00
use function::Function;
use lua::Lua;
use string::String;
use table::Table;
use thread::Thread;
2018-08-05 09:51:39 -04:00
use types::{Integer, LightUserData, Number};
use userdata::{AnyUserData, UserData};
use value::{FromLua, Nil, ToLua, Value};
2017-05-21 19:50:59 -04:00
impl<'lua> ToLua<'lua> for Value<'lua> {
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
2017-05-21 19:50:59 -04:00
Ok(self)
}
}
impl<'lua> FromLua<'lua> for Value<'lua> {
fn from_lua(lua_value: Value<'lua>, _: &'lua Lua) -> Result<Self> {
2017-05-21 19:50:59 -04:00
Ok(lua_value)
}
}
impl<'lua> ToLua<'lua> for String<'lua> {
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::String(self))
2017-05-21 19:50:59 -04:00
}
}
impl<'lua> FromLua<'lua> for String<'lua> {
fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<String<'lua>> {
2017-05-21 19:50:59 -04:00
lua.coerce_string(value)
}
}
impl<'lua> ToLua<'lua> for Table<'lua> {
2018-03-19 14:35:46 -04:00
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::Table(self))
2017-05-21 19:50:59 -04:00
}
}
impl<'lua> FromLua<'lua> for Table<'lua> {
fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result<Table<'lua>> {
2017-05-21 19:50:59 -04:00
match value {
Value::Table(table) => Ok(table),
2017-08-01 13:36:26 -04:00
_ => Err(Error::FromLuaConversionError {
from: value.type_name(),
to: "table",
message: None,
}),
2017-05-21 19:50:59 -04:00
}
}
}
impl<'lua> ToLua<'lua> for Function<'lua> {
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::Function(self))
2017-05-21 19:50:59 -04:00
}
}
impl<'lua> FromLua<'lua> for Function<'lua> {
fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result<Function<'lua>> {
2017-05-21 19:50:59 -04:00
match value {
Value::Function(table) => Ok(table),
2017-08-01 13:36:26 -04:00
_ => Err(Error::FromLuaConversionError {
from: value.type_name(),
to: "function",
message: None,
}),
2017-05-21 19:50:59 -04:00
}
}
}
impl<'lua> ToLua<'lua> for Thread<'lua> {
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::Thread(self))
2017-05-21 19:50:59 -04:00
}
}
impl<'lua> FromLua<'lua> for Thread<'lua> {
fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result<Thread<'lua>> {
2017-05-21 19:50:59 -04:00
match value {
Value::Thread(t) => Ok(t),
2017-08-01 13:36:26 -04:00
_ => Err(Error::FromLuaConversionError {
from: value.type_name(),
to: "thread",
message: None,
}),
2017-05-21 19:50:59 -04:00
}
}
}
impl<'lua> ToLua<'lua> for AnyUserData<'lua> {
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::UserData(self))
}
}
impl<'lua> FromLua<'lua> for AnyUserData<'lua> {
fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result<AnyUserData<'lua>> {
match value {
Value::UserData(ud) => Ok(ud),
2017-08-01 13:36:26 -04:00
_ => Err(Error::FromLuaConversionError {
from: value.type_name(),
to: "userdata",
message: None,
}),
}
}
}
impl<'lua, T: Send + UserData> ToLua<'lua> for T {
fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::UserData(lua.create_userdata(self)?))
2017-05-21 19:50:59 -04:00
}
}
impl<'lua, T: UserData + Clone> FromLua<'lua> for T {
fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result<T> {
2017-05-21 19:50:59 -04:00
match value {
Value::UserData(ud) => Ok(ud.borrow::<T>()?.clone()),
2017-08-01 13:36:26 -04:00
_ => Err(Error::FromLuaConversionError {
from: value.type_name(),
to: "userdata",
message: None,
}),
2017-05-21 19:50:59 -04:00
}
}
}
impl<'lua> ToLua<'lua> for Error {
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::Error(self))
}
}
impl<'lua> FromLua<'lua> for Error {
fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<Error> {
match value {
Value::Error(err) => Ok(err),
val => Ok(Error::RuntimeError(
lua.coerce_string(val)
.and_then(|s| Ok(s.to_str()?.to_owned()))
.unwrap_or_else(|_| "<unprintable error>".to_owned()),
)),
}
}
}
2017-05-21 19:50:59 -04:00
impl<'lua> ToLua<'lua> for bool {
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::Boolean(self))
2017-05-21 19:50:59 -04:00
}
}
impl<'lua> FromLua<'lua> for bool {
fn from_lua(v: Value, _: &'lua Lua) -> Result<Self> {
2017-05-21 19:50:59 -04:00
match v {
Value::Nil => Ok(false),
Value::Boolean(b) => Ok(b),
2017-05-21 19:50:59 -04:00
_ => Ok(true),
}
}
}
impl<'lua> ToLua<'lua> for LightUserData {
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::LightUserData(self))
}
}
impl<'lua> FromLua<'lua> for LightUserData {
2017-08-01 13:36:26 -04:00
fn from_lua(value: Value, _: &'lua Lua) -> Result<Self> {
match value {
Value::LightUserData(ud) => Ok(ud),
2017-08-01 13:36:26 -04:00
_ => Err(Error::FromLuaConversionError {
from: value.type_name(),
to: "light userdata",
message: None,
}),
}
}
}
impl<'lua> ToLua<'lua> for StdString {
fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::String(lua.create_string(&self)?))
2017-05-21 19:50:59 -04:00
}
}
impl<'lua> FromLua<'lua> for StdString {
fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<Self> {
Ok(lua.coerce_string(value)?.to_str()?.to_owned())
2017-05-21 19:50:59 -04:00
}
}
impl<'lua, 'a> ToLua<'lua> for &'a str {
fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::String(lua.create_string(self)?))
2017-05-21 19:50:59 -04:00
}
}
macro_rules! lua_convert_int {
2018-08-05 09:51:39 -04:00
($x:ty) => {
2017-05-21 19:50:59 -04:00
impl<'lua> ToLua<'lua> for $x {
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::Integer(self as Integer))
2017-05-21 19:50:59 -04:00
}
}
impl<'lua> FromLua<'lua> for $x {
fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<Self> {
2017-05-21 19:50:59 -04:00
Ok(lua.coerce_integer(value)? as $x)
}
}
2018-08-05 09:51:39 -04:00
};
2017-05-21 19:50:59 -04:00
}
lua_convert_int!(i8);
lua_convert_int!(u8);
lua_convert_int!(i16);
lua_convert_int!(u16);
lua_convert_int!(i32);
lua_convert_int!(u32);
lua_convert_int!(i64);
lua_convert_int!(u64);
lua_convert_int!(isize);
lua_convert_int!(usize);
macro_rules! lua_convert_float {
2018-08-05 09:51:39 -04:00
($x:ty) => {
2017-05-21 19:50:59 -04:00
impl<'lua> ToLua<'lua> for $x {
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::Number(self as Number))
2017-05-21 19:50:59 -04:00
}
}
impl<'lua> FromLua<'lua> for $x {
fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<Self> {
2017-05-21 19:50:59 -04:00
Ok(lua.coerce_number(value)? as $x)
}
}
2018-08-05 09:51:39 -04:00
};
2017-05-21 19:50:59 -04:00
}
lua_convert_float!(f32);
lua_convert_float!(f64);
impl<'lua, T: ToLua<'lua>> ToLua<'lua> for Vec<T> {
fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::Table(lua.create_sequence_from(self)?))
2017-05-21 19:50:59 -04:00
}
}
impl<'lua, T: FromLua<'lua>> FromLua<'lua> for Vec<T> {
fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result<Self> {
if let Value::Table(table) = value {
table.sequence_values().collect()
2017-05-21 19:50:59 -04:00
} else {
2017-08-01 13:36:26 -04:00
Err(Error::FromLuaConversionError {
from: value.type_name(),
to: "Vec",
message: Some("expected table".to_string()),
2017-08-01 13:36:26 -04:00
})
2017-05-21 19:50:59 -04:00
}
}
}
impl<'lua, K: Eq + Hash + ToLua<'lua>, V: ToLua<'lua>, S: BuildHasher> ToLua<'lua>
2018-03-19 14:35:46 -04:00
for HashMap<K, V, S>
{
fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::Table(lua.create_table_from(self)?))
2017-05-21 19:50:59 -04:00
}
}
impl<'lua, K: Eq + Hash + FromLua<'lua>, V: FromLua<'lua>, S: BuildHasher + Default> FromLua<'lua>
2018-03-19 14:35:46 -04:00
for HashMap<K, V, S>
{
fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result<Self> {
if let Value::Table(table) = value {
table.pairs().collect()
2017-05-21 19:50:59 -04:00
} else {
2017-08-01 13:36:26 -04:00
Err(Error::FromLuaConversionError {
from: value.type_name(),
to: "HashMap",
message: Some("expected table".to_string()),
2017-08-01 13:36:26 -04:00
})
2017-05-21 19:50:59 -04:00
}
}
}
impl<'lua, K: Ord + ToLua<'lua>, V: ToLua<'lua>> ToLua<'lua> for BTreeMap<K, V> {
fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::Table(lua.create_table_from(self)?))
2017-05-21 19:50:59 -04:00
}
}
impl<'lua, K: Ord + FromLua<'lua>, V: FromLua<'lua>> FromLua<'lua> for BTreeMap<K, V> {
fn from_lua(value: Value<'lua>, _: &'lua Lua) -> Result<Self> {
if let Value::Table(table) = value {
table.pairs().collect()
2017-05-21 19:50:59 -04:00
} else {
2017-08-01 13:36:26 -04:00
Err(Error::FromLuaConversionError {
from: value.type_name(),
to: "BTreeMap",
message: Some("expected table".to_string()),
2017-08-01 13:36:26 -04:00
})
2017-05-21 19:50:59 -04:00
}
}
}
impl<'lua, T: ToLua<'lua>> ToLua<'lua> for Option<T> {
fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
2017-05-21 19:50:59 -04:00
match self {
Some(val) => val.to_lua(lua),
None => Ok(Nil),
2017-05-21 19:50:59 -04:00
}
}
}
impl<'lua, T: FromLua<'lua>> FromLua<'lua> for Option<T> {
fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<Self> {
2017-05-21 19:50:59 -04:00
match value {
Nil => Ok(None),
2017-05-21 19:50:59 -04:00
value => Ok(Some(T::from_lua(value, lua)?)),
}
}
}