Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lua_tolightuserdata, optimized lua_topointer #496

Merged
merged 2 commits into from
May 24, 2022

Conversation

petrihakkinen
Copy link
Contributor

This change adds lua_tolightuserdata to the public API. Rationale:

First, it's faster than lua_touserdata when working purely with lightuserdata values. In our codebase we use lightuserdata for opaque 64-bit object handles (handles to objects managed by C++ code). With lua_touserlightdata we are seeing up to 8% performance improvement in Lua to C++ calls that use these handles. I believe this is a common use-case for lightuserdata.

Secondly, since the API already has lua_pushlightuserdata and lua_islightuserdata, it would make sense to have lua_tolightuserdata for completeness.

I also optimized lua_topointer a bit by reducing branching in userdata case.

@zeux
Copy link
Collaborator

zeux commented May 23, 2022

This looks good! Do you think it's worth it to convert lua_touserdata to use two ifs so that for that function we prioritize regular userdata as a fast path?

@petrihakkinen
Copy link
Contributor Author

Makes sense. Made the changes and a benchmark calling lua_touserdata 10 million times is now about 8% faster.

@zeux zeux merged commit fb9c431 into luau-lang:master May 24, 2022
@petrihakkinen petrihakkinen deleted the petrih-tolightuserdata branch June 13, 2022 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants