Skip to content

minimizing overhead for accessing rust structs from lua #283

Answered by khvzak
brentp asked this question in Q&A
Discussion options

You must be logged in to vote

I assume the difference is more when there are more fields vs more methods?

It should not really matter, the complexity is O(1), fields and methods are stored in a hashmap.
However Luau at compile time can predict hashmap slot when userdata has only methods. Whereas with fields(+ methods) lookup is always dynamic.

is there anyway to keep the former property syntax with the performance of a method?

You can try to set __index metamethod for your case:

fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
    fields.add_meta_field_with("__index", |lua| {
        lua.create_function(
            |lua, (this, key): (mlua::UserDataRef<Self>, mlua::String)| match key.as_bytes(

Replies: 8 comments 18 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
5 replies
@brentp
Comment options

@brentp
Comment options

@khvzak
Comment options

@brentp
Comment options

@khvzak
Comment options

Answer selected by brentp
Comment options

You must be logged in to vote
10 replies
@brentp
Comment options

@khvzak
Comment options

@brentp
Comment options

@khvzak
Comment options

@brentp
Comment options

Comment options

You must be logged in to vote
1 reply
@khvzak
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@khvzak
Comment options

@brentp
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants