Mapgen memory optimisations

This page is based on Paramat's forum topic and has been updated for Minetest 5.0.0.

Lua map generators can use excessive memory if they are not using these 3 optimisations. Not using these optimizations can eventually lead to OOM (out-of-memory) errors because the Lua mapgen wasted way too much memory. Applying this advice is strongly recommended, and should make OOM errors much less likely. But there is no guarantee this will fix all OOM errors, your mapgen might still use excessive memory for other reasons, or the computer just has very limited memory.

Perlin noise objects: Only create once
The noise object is created by. It has to be created inside  to be usable, but only needs to be created once, many mapgen mods create it for every mapchunk, this consumes memory unnecessarily.

Localise the noise object outside  and initialise it to. See the code below for how to create it once only. The creation of the perlin noise tables with  etc. is done per mapchunk

Perlin noise tables: Re-use a single table
The Lua table that stores the noise values for a mapchunk is big, especially for 3D noise (803 = 512000 values). Many Lua mapgens are creating a new table for every mapchunk, while the previous tables are only cleared out slowly by garbage collection, resulting in a large and unnecessary memory use.

A  parameter was added in 0.4.13 to avoid this, a single table is re-used by overwriting the former values.

For each of the functions with an optional `buffer` parameter: If `buffer` is not nil, this table will be used to store the result instead of creating a new table. * `get_2d_map_flat(pos, buffer)`: returns a flat `` element array of 2D noise with values starting at `pos={x=,y=}` * `get_3d_map_flat(pos, buffer)`: Same as `get_2d_map_flat`, but 3D noise
 * 1) Methods

Localise the noise buffer outside Use the  parameter in   etc.

Lua voxelmanip table: Re-use a single table
The Lua table that stores the node content IDs for a mapchunk plus the mapblock shell is big (1123 = 1404928 values). Many Lua mapgens are creating a new table for every mapchunk, while the previous tables are only cleared out slowly by garbage collection, resulting in a large and unnecessary memory use.

A  parameter was added in 0.4.13 to avoid this, a single table is re-used by overwriting the former values.

In Minetest 0.4.15, a  parameter was also added to.

* `get_data([buffer])`: Retrieves the node content data loaded into the `VoxelManip` object * returns raw node data in the form of an array of node content IDs * if the param `buffer` is present, this table will be used to store the result instead * `get_param2_data([buffer])`: Gets the raw `param2` data read into the `VoxelManip` object * Returns an array (indices 1 to volume) of integers ranging from `0` to `255` * If the param `buffer` is present, this table will be used to store the result instead

Localise the data buffer outside. Use the buffer in  or.