r/lua • u/VeraDeveloper • Aug 03 '25
Help Roadblocks trying to make a very fast Lua Virtual Machine
I'm using a modified version of FiOne so it runs even faster. However i am hitting roadblocks which i have no idea how to solve. My benchmark gives me 0.077 (The lower, the better) however this is quite slow for my needs. (Example: Software Rendering, Dyno's, Heavy usage of math and etc)
I have tried creating a custom instruction set to try optimizing it however it barely made it faster. (0.001s improvement?)
So my question is: How can i make a LuaVM that runs even faster? For the provided benchmark, i want to see below 0.01 at best, 0.05 at worst
NOTES:
- I have tried other interpreters which are LuaInLua and LBI however not only were they slower, they're also way more buggier.
- I do NOT have access to FFI, loadstring and load due to being in a sand boxed environment (Bit library is accessible)
- I must do this in just Lua, No C or C++
- I am running this inside a game which means lua will run roughly 3x slower due to the layers it adds.
- I cannot post my LBI's source code as i would be then leaking unreleased code from my project which i cannot do. It's roughly 80-90% same as the original code.
- It might not be possible to optimize this any further but i want to try anyways.
What i know are damaging performance:
- The amount of table indexing Lua interpreters are doing including FiOne.
- The amount of if statements used inside run_lua_func for FiOne.
Benchmark code if needed (Yes, i know its quite basic but the more advanced one wouldn't fit here)
local x = os.clock()
local s = 0
for i=1,1000000, 1 do s = s + i end
print(string.format("elapsed time: %.5f", os.clock() - x))
4
u/HelioDex Aug 04 '25
If you can't share the source code for your interpreter I don't think it will be possible to give specific optimisation tips, though one general technique I'm using in my VM implementation is 'jets': find out what functions/statements/pathways in the VM are most intensive, and reimplement them as native Lua code (a jet). Before running (or at runtime) you can parse through the bytecode or deserialised representation and replace any portions of slow code with references to the native implementation.
At this point you can keep adding optimised jets until the performance requirements are reached, as the process doesn't have much overhead. Do take pains to make sure the jets function identically to their bytecode representations though.
Other than it might be beneficial to remove features that are slow/difficult to optimise, for example I removed global variables, environment access, and metatables from my VM, which will prevent dynamic deoptimisation from happening in the host interpreter and make more peephole optimisations doable. Good luck with your performance improvements!
2
u/coverdr1 Aug 04 '25
I've written a Javascript VM inside Squirrel before. Squirrel has a number of benefits over Lua for getting indexed values faster. Looking through FiOne, if your implementation is similar, try avoiding key look ups as much as possible. Since everything seems to have a predictible number of fields, just put them in a lua array and use integer indexes to get what you want. The function stm_inst_list() creates a really complicated table with very predictable contents. Replace it with a lua array where data.A, data.B and other fields are placed at specific integer indexes in the array. Better still, if the value are with small ranges, just squash them into a string and use string.byte(str,pos) to fetch them. Even if you make these changes, you'll only get small savings. Running a VM in a VM will always be slow.
1
u/EvilBadMadRetarded Aug 04 '25
Try google 'memoization'. It is cacheing for pure function, which return same output for same inputs, trading space for speed. I think those rd_* function are candidate.
0
u/[deleted] Aug 04 '25
roblox trying to make a very fast lua virtual machine