Short short answer for your question: You're doing it wrong.
The longer answer:
Your bottleneck is most likely file reading and parsing, something you should only do once.
So rather than loading the file and runningI think part of the code directly, you should instead define callbacks in your Lua code, which are then executedproblem (rather thanwhich also caused me to misinterpret the whole scriptquestion at first).
For example, you might have some lua script to apply movement to some entity:
this_x = this_x + this_vx * dt
this_y = this_y + this_vy * dt
To run this script, you could simply run it using is the lual_dofile() over and over again, but this isn't very efficient (as you've noticed)calls.
So instead, wrap that code in a function:
function onUpdate(this, deltaTime)
this.x = this.x + this.vx * deltaTime;
this.y = this.y + this.vy * deltaTime;
end
You'll once again load and execute this code once to actually initialize/load that script. This won't have any noticeable effect or change other than defining the functions (or any locals)sounds like all scripts are always loaded in your current contextthe same global state, overwriting each other, so loading/parsing causes a significant overhead.
Then, once every frameInstead, you'd just callI'd suggest you use onUpdatelua_newthread() to create a separated context once every update rather than reloading/reparsing the wholeper script (and simply don't call it, if it isn't defined; a state which you can save for later, so you don't have to keep trying)file loaded.
This should improveallows you to load all your engine's performance dramaticallyscript files once and then simply switch between them before calling update() or any other Lua function, without having to reload/recompile anything.