paulofmandown / rotLove

Roguelike Toolkit in Love. A Love2D/lua port of rot.js
http://paulofmandown.github.io/rotLove/
Other
258 stars 25 forks source link

Slow RNG #29

Closed airstruck closed 7 years ago

airstruck commented 7 years ago

Summary:

It takes a while to generate large maps. RNG uses some hand-rolled bit ops that are probably to blame. Using LuaJIT bit ops when available and falling back to hand-rolled ones will speed things up a lot.

In rot.js, an implementation of Alea is used as the sole RNG. Alea is interesting because it doesn't seem to need bitwise operators at all.*

I've ported the original Alea implementation to Lua and tested in Love against rotLove's Twister; on my machine Alea is about 500 times faster with jit on and 100 times faster with jit off. In other words, with jit on, Alea can generate in an eighth of a second what would take the current Twister a full minute.

To be fair, I tested again with LuaJIT's bit ops patched into Twister, and it's much faster, but still 10x slower than Alea.


*The author uses >>>0 and |0 in some places, but the only apparent effect is flooring; the truncating to 32 bits doesn't appear to affect the outcome (tested millions of numbers with a handful of seeds, Lua port with math.floor in place of >>>0 and |0 gave same results as original JS).

airstruck commented 7 years ago

MWC has speed comparable to Alea, since it doesn't use bit ops either (Alea is based on MWC). Unless anyone is particularly attached to LCG or Twister, I'd propose something like this:

Another Alea implementation is here, with more detailed explanation of some parts in the comments.

paulofmandown commented 7 years ago

Fixed with 5d939011ff93dd796f6cfa4db73de686a0820c4c

Again, thanks for all of your contributions!