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

Reduce allocations per LuaClosure call #76

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

LobbyDivinus
Copy link

@LobbyDivinus LobbyDivinus commented Sep 1, 2020

Every call/invoke of a LuaClosure issues creation of a new stack frame for the function execution. Since this stack is usually thrown away after execution pooling them can help to reduce garbage collection overhead (there's one exception to this: the stack can be used as part of the returned varargs object, in that case it cannot be reused since that could cause conflicts). In my use case this change was useful for calling a lot of Lua functions on mobile devices.

Re-use stack frames in LuaClosure if possible to reduce number of allocations per function call
Every call of a LuaClosure issues creation of a new stack frame for the function execution. Since this stack is usually thrown away after execution pooling them can help to reduce garbage collection overhead (there's one exception to this: the stack can be used as part of the returned varargs object, in that case it cannot be reused since that could cause conflicts). In my use case this change was a useful for calling a lot of Lua functions on a mobile device.
@wagyourtail
Copy link

wagyourtail commented Nov 5, 2023

this is not thread safe...
image
also, stackpool not really clearing seems leaky to me...

@LobbyDivinus
Copy link
Author

To my knowledge luaj is not thread safe at all (and so is Lua itself). However, it should be fine if you make calls from different threads if they are not concurrent. If you need concurrent execution you may have to use mutliple environments.

The stackpool should be released once the associated LuaClosure gets garbage collected. Doing it earlier would hurt the intention of this PR.

@SquidDev
Copy link

SquidDev commented Nov 6, 2023

This stack pool is per-LuaClosure, so this should be safe under LuaJ's guarantees - LuaClosures shouldn't be shared across Global instances or called from multiple threads at once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants