lua: use non-member functions for interop #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, Tilemaker uses member functions for interop:
This PR changes Tilemaker to use global functions:
The chief rationale is performance. Every member function call needs to push an extra pointer onto the stack when crossing the Lua/C++ boundary.
Kaguya serializes this pointer as a Lua userdata. That means every call into Lua has to malloc some memory, and every call back from Lua has to dereference through this pointer.
And there are a lot of calls! For OMT on the GB extract, I counted ~1.4B calls from Lua into C++.
A secondary rationale is that a global function is a bit more honest. A user might believe that this is currently permissible:
But in reality, the OSM objects we pass into Lua don't behave quite like Lua objects. They're backed by OsmLuaProcessing, who will move on, invalidating whatever the user thinks they've got a reference to.
This PR has a noticeable decrease in reading time for me, measured on the OMT profile for GB, on a 16-core computer:
Before:
After:
The tradeoffs:
anyone with a custom Lua profile will need to update it, although the changes are fairly mechanical
Tilemaker now reserves several functions in the global namespace, causing the potential for conflicts