mirror of
https://github.com/dlang/phobos.git
synced 2025-05-02 08:00:48 +03:00
![]() This patch updates MersenneTwisterEngine's implementation to use the new and much faster implementation Ilya prepared for `mir.random`. The original Mersenne Twister algorithm (as per the reference C code by Matsumoto and Nishimura) first regenerates the contents of the entire state array, before looping over the resulting values and tempering them in order to generate the actual variates. Once the entire state array has been iterated over, the array's values are then regenerated, and so on and so forth. Ilya's implementation reworks this idea to intertwine update of a single entry in the state array with the tempering of another value to produce the next variate. This ensures that all the registers concerned stay 'hot' in the CPU's memory and hence ensures significant speedup. (Just as a mention: as an experiment while adapting this code for phobos I tried splitting apart the lines responsible for updating the internal state array from those responsible for tempering the next variate, so that they could be run sequentially instead of mixed up together; this resulted in a fairly significant speed hit.) In contrast to most (all?) other Mersenne Twister implementations, this one iterates backwards through the state array, which allows for a few extra compiler optimizations that speed up the algorithm. It has thus been necessary to rework the range-based `seed` method to take account of this. Besides the algorithmic change, this patch introduces two new template variables: an extra tempering variable `d`, and the initialization multiplier `f`, which brings the template implementation in line with that of the C++11 standard. These extra template variables are needed in order to effectively implement the standard 64-bit version of the Mersenne Twister, which will be added in a follow-up patch. Finally, this implementation introduces corrections to the handling of the word size `w` to ensure that the right sequences are produced when the word size is less than the number of bits in `UIntType`. Unittests will be added for this in a follow-up patch. ------------------------------------------------------------------------ Where this implementation differs from that in `mir.random`: Ilya's original design has been reworked as a drop-in replacement for the existing Phobos implementation, which is currently implemented as a forward range (although it should ideally be an input range, but that design flaw should be fixed as a separate issue). There appears to be no significant speed hit from reworking Ilya's code as a range rather than as a functor. However, some other aspects of the original design have required rather intrusive changes in order to get the full speed benefits without introducing breaking change. The original MersenneTwisterEngine allows for implicit instantiation of generator instances without providing a seed. This is handled by checking in the `front` and `popFront` method whether the generator has been properly initialized, and seeding it with the default seed if not. However, these runtime checks on every single call result in a massive speed hit. The current implementation therefore takes a number of steps in order to ensure that the internal state of the generator can be set to its default-seeded values at compile time: * all the internal state variables have been wrapped up in a nested `State` struct to facilitate generation; * the internals of the `seed` and `popFront` methods have been separated out into CTFE'able private static methods (`seedImpl` and `popFrontImpl`) which take a reference to a `State` instance as input; * a CTFE'able private static `defaultState` method has been added which generates a `State` instance matching that generated by instantiating the generator with the `defaultSeed`. The `defaultState` method is then used to initialize the default value of the internal `State` instance at compile-time, replicating the effect of the original runtime seeding check. These latter workarounds could be removed in future if the generator were updated to `@disable this();` and therefore always require explicit seeding, but this would be a breaking change and so is avoided for now. |
||
---|---|---|
.. | ||
algorithm | ||
c | ||
container | ||
digest | ||
experimental | ||
internal | ||
net | ||
range | ||
regex | ||
windows | ||
array.d | ||
ascii.d | ||
base64.d | ||
bigint.d | ||
bitmanip.d | ||
compiler.d | ||
complex.d | ||
concurrency.d | ||
concurrencybase.d | ||
conv.d | ||
csv.d | ||
datetime.d | ||
demangle.d | ||
encoding.d | ||
exception.d | ||
file.d | ||
format.d | ||
functional.d | ||
getopt.d | ||
json.d | ||
math.d | ||
mathspecial.d | ||
meta.d | ||
mmfile.d | ||
numeric.d | ||
outbuffer.d | ||
parallelism.d | ||
path.d | ||
process.d | ||
random.d | ||
signals.d | ||
socket.d | ||
stdint.d | ||
stdio.d | ||
stdiobase.d | ||
string.d | ||
system.d | ||
traits.d | ||
typecons.d | ||
typetuple.d | ||
uni.d | ||
uri.d | ||
utf.d | ||
uuid.d | ||
variant.d | ||
xml.d | ||
zip.d | ||
zlib.d |