License: LGPL v3
Dependancies: None of mods but needs engine to be patched
Source: https://github.com/EvidenceBKidscode/ffiopt
Description
Lua API methods that transmit large amount of data (VoxelManip:get_data, PerlinNoiseMap:get3dMap_flat) copies data from C++ structures to Lua tables. This copy is quite slow and consumes a lot of Lua memory.
FFIopt replaces this copy by a direct access from Lua to C++ data using FFI library. The engine has to be patched to expose some C++ pointers to Lua API.
If engine correctly patched, FFIopt hacks VoxelManip and PerlinNoiseMap to use FFI rather than Lua tables.
Result is :
- Faster data retreiving and storing ;
- Slower table access (essencially due to use of metatable avoiding mods code change) ;
- Lower Lua memory footprint (avoiding many OOM errors) ;
- Use LuaJIT.
- Patch the engine to add VoxelManip:get_data_ptr, PerlinNoiseMap:get_result_size and PerlinNoiseMap:get_result_ptr methods to the API.
- Install and enable ffiopt mod.
- On starting Minetest, ffiopt will tell if it is able to optimise voxel manips and perlin noise.
- Nothing to change in mods code.
Experiments
FFIopt can be tested through ffiopt_demo mod. Here is what we obtained.
Voxel Manip
Without FFIopt, in the demo, 30 % of the time is spend in get_data and another 30 % is spent in set_data. Lua processing takes 20 % and read/write the remaining 20 %.
So most of the time is spent in converting C++ data to Lua table.
With FFIopt, Lua takes 68 % of the time and read/write 32 % (get_data and set_data don’t do anything).
Lua processing time is increased by 75 %, but total time is reduced by 50 %.
Furthermore, without FFIopt, launching demo 9 times leaded to 3 OOM crash. With FFIopt, we have been able to launch more than 20 times the demo with no crash.
Perlin noise
With FFIopt, Lua processing increased by 50 % but total time reduced by 15 %.
Lua time is increased mostly because of the use of a metatable. This can be avoided but the mod code has to be changed (C++ indexes starts from 0, not 1) and secured to avoid out of range indexes.