The original theory was that preallocating the array forced lua
to use the "array part" of a table instead of the "hash part",
which should be faster since lookups are a simple linear offset
calculation and not a hash with collision check.
It turns out, however, that in empirical testing, whether the
array-preallocated or the naive-allocated version is faster depends
on circumstances, such as how sparse the array is, how uniform
access is, or how much other load the system is under. This seems
to suggest that arrays are more computationally-efficient but less
cache-efficient, so cache evictions caused by other concurrent
processes may slow this method down significantly.
Worst-case performance will be the most noticeable here, since a
system that's heavily loaded would have the most need for speed
in the first place, so it's probably better generally to use the
naive allocation.
In all, the naive allocation method also seemed to be the least
sensitive to external factors. In addition to the array method,
attempts to pre-compile the dynamic checks into a binary tree of
if/then statements (which could be JITted and optimized) failed
as well.
Two optimizations that added up to a significant
time savings in on_generated:
- Replacing calls to area:index(x,y,z) to precalculating
an offset and then using stride math inside the loops
gives us a huge cost savings.
- Prefilling lookup-by-ID tables to force them into
sequential arrays instead of hashmaps speeds up
lookups, for a more moderate savings.
This should cause maps that have the same
seeds to have much more similar results
than before, e.g. sponge deposits will be in
the same places determined by seed and not
random each regeneration of the map.
- Mapgen shared now provides an RNG which
will be deterministic when feasible, for
repeatable mapgen results.
- Make existing rng-using mapgen hooks use
the new deterministic RNG.
- Mapgen shared hooks are also run in
deterministic order too.
- Tidy up mapgen_shared API a little more.
- Register def tables instead of bare funcs.
- Add labels for mod logic use.
- Automatically skip for singlenode mapgens,
e.g. for the skyblock modpack.
- Apply stratification last in shared mapgens, so it can see all
already-placed ores.
- Support stratification for nodes other than plain stone.
- Register lode stone and lode ore for stratification.